Skip to content

ragoncsa/todo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

todo

Overview

This is a sample application for REST service development with golang. Libraries used:

  • gorm as ORM library
  • viper for configuration management
  • gin as web framework
  • gin-swagger to generate OpenAPI spec from go comments

Simple REST webservice

Set up database

Also see: https://hub.docker.com/_/postgres

docker run -it --rm -p 5432:5432 --name pg -e POSTGRES_PASSWORD=password postgres

$ docker exec -it pg /bin/bash                               
root@187961c81d2e:/# psql -U postgres
psql (14.2 (Debian 14.2-1.pgdg110+1))
Type "help" for help.

Start the server

go run main.go

Test the service

Create a task

curl -X POST -H 'Content-Type: application/json' localhost:8080/tasks/ -d '{"name": "hello1"}'

http POST localhost:8080/tasks/ name=hello1

Get a task

curl -X GET localhost:8080/tasks/1

http GET localhost:8080/tasks/1

Get all tasks

curl -X GET localhost:8080/tasks/

http GET localhost:8080/tasks/

Delete a task

curl -X DELETE localhost:8080/tasks/1

http DELETE localhost:8080/tasks/1

Delete all tasks

curl -X DELETE localhost:8080/tasks/

http DELETE localhost:8080/tasks/

Run tests

go test ./...

Generate OpenAPI spec

swag init

To access swagger UI go to http://localhost:8080/swagger/index.html

For more see: https://github.com/swaggo/gin-swagger

Containerization

To build

docker build -t todo .

Docker-compose starts the built container with a database

docker-compose up

The you can access swagger UI http://localhost:8080/swagger/index.html

Reset the database

docker-compose down --volumes