Coder Social home page Coder Social logo

api's Introduction

Example API service in Go

This is an example of an API service application written in Go. The application uses Gin for the REST API framework.

The application designed keeping in mind the 12-factor app principles.

Docker image

The application is available as a Docker image on Docker Hub. To build one locally, clone the repository and run make command:

make image

Deployment

This application us deployable to Kubernetes cluster using Pulumi IaaC (Infrastructure as Code). To deploy the application using Pulumi, you need to have Pulumi installed on your machine.

After installing Pulumi, run the following commands to deploy the application:

pulumi up --stack dev --config "api:kube-context=kubernetes-admin@kubernetes"

Use the --config flag and specify the Kubernetes context to use for deployment.

The kubernetes-admin@kubernetes default k8s context which is used in the above command is deploying the application to the Kubernetes cluster running in raspberry pi configured using ansible script @binodluitel/rpi-ansible.

Run using Docker

If you wish to run using Docker, you can do that too. Simply docker run the image.

docker run bluitel/api:latest
[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
 - using env:	export GIN_MODE=release
 - using code:	gin.SetMode(gin.ReleaseMode)
[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
 - using env:	export GIN_MODE=release
 - using code:	gin.SetMode(gin.ReleaseMode)
[GIN-debug] Listening and serving HTTP on :8080
{"level":"info","ts":1719970919.3356018,"caller":"api/main.go:32","msg":" ----- Welcome to the API service example ----- "}
{"level":"debug","ts":1719970919.3363056,"caller":"api/main.go:39","msg":"Application build information","name":"api-service","version":"5ce1db0","build_time":"2024-07-02T23:16:14Z","ref_name":"main","ref_sha":"5ce1db0d5d6ab557ed35756f53edba06ebe137fd"}
[GIN-debug] Listening and serving HTTP on :9090

Metrics

The application exposes metrics on /metrics endpoint. The metrics are exposed in Prometheus format.

$ curl -s http://127.0.0.1:9090/metrics

418 (I'm a teapot)

Currently, all REST verbs for / (Base) URL returns 418 (I'm a teapot) status code with no content because base route does not have any implementation and is not intended to be used.

$ curl -s -v http://127.0.0.1:8080
*   Trying 127.0.0.1:8080...
* Connected to 127.0.0.1 (127.0.0.1) port 8080
> GET / HTTP/1.1
> Host: 127.0.0.1:8080
> User-Agent: curl/8.6.0
> Accept: */*
>
< HTTP/1.1 418 I'm a teapot
< Date: Mon, 01 Jul 2024 18:12:30 GMT
< Content-Length: 0
<
* Connection #0 to host 127.0.0.1 left intact

API Endpoints

The application has the following REST API endpoints:

  • GET v1/pods/:pod_name/logs?follow=false - Gets or stream (set follow=true) logs from the k8s pod(s)
  • GET/POST/PATCH/DELETE v1/users - CRUD operations for users are placeholders and does not have any implementation yet

Port Forwarding

To access the application running in the Kubernetes cluster, you can use port forwarding since/if there is no Ingress or LoadBalancer configured.

Application API is exposed on port 8080.

kubectl port-forward --namespace api svc/api 8080

Application metrics are exposed on port 9090.

kubectl port-forward --namespace api svc/api 9090

api's People

Contributors

binodluitel avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.