Giter VIP home page Giter VIP logo

demo-go-rest's Introduction

Ejemplo de microservicio en Go (golang)

Librerías utilizadas:


Compilar:

Este comando descargará las dependencias necesarias y generará el archivo ejecutable.

go build

Ejecutar:

./app

Docker

Para ejecutar este servicio en un contenedor de docker, solo hay que construir la imagen:

docker build -t myService .

y luego para ejecutarlo:

docker run -p 8080:8080 myService

Database

Este microservicio se conecta a una base de datos de postgres, los valores de la conexión hay que modificarlos en el archivo db/db.go

El script para generar la tabla que se usa es este:

CREATE TABLE public.usuario (
	id numeric NULL,
	name varchar NULL,
	"time" timestamp NULL DEFAULT CURRENT_TIMESTAMP
);

INSERT INTO public.usuario (id, "name", "time") VALUES(0, 'user0', CURRENT_TIMESTAMP);
INSERT INTO public.usuario (id, "name", "time") VALUES(1, 'user1', CURRENT_TIMESTAMP);
INSERT INTO public.usuario (id, "name", "time") VALUES(2, 'user2', CURRENT_TIMESTAMP);

Peticiones soportadas en este microservicio:

GET /users

Obtiene todos los usuarios de la base de datos
y los retorna como un arreglo en JSON

Response:

[
    {
        "id": 0,
        "nombre": "user0",
        "time": "2019-12-20T12:06:57.286898Z"
    },
    {
        "id": 1,
        "nombre": "user1",
        "time": "2019-12-20T12:06:57.286898Z"
    },
    {
        "id": 2,
        "nombre": "user2",
        "time": "2019-12-20T12:06:57.286898Z"
    }
]

POST /user

Agrega un usuario nuevo a la base de datos

Request:

{
    "id": 3,
    "nombre": "user3"
}

Response:

{
    "id": 3,
    "nombre": "user3",
    "time": ""
}

DELETE /user/3

Elimina un usuario de la base de datos

Response status 200


TO-DO

  • Múltiples métodos (GET, POST, DELETE)
  • Múltiples rutas (/users, /user)
  • Request body en JSON
  • Response body en JSON
  • Path Params (/user/:id)
  • Conexión con Postgresql
  • Logs
  • Dockerfile
  • Dependencias con go modules
  • Variables de entorno (db, port)
  • Makefile
  • Unit Testing

demo-go-rest's People

Contributors

luisegr 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.