Starter implementation of todo and auth REST APIs with OpenAPI (Swagger) documentation in Go.
Access other branches to find more modular implementations of authentication and todo APIs:
- rest-auth: Auth API
- rest-todo: Todo API
- rest-auth-openapi: Auth API with OpenAPI UI
- rest-todo-openapi: Todo API with OpenAPI UI
- User registration
- User login
- User logout
- Create a todo
- Get all todos
- Get a todo by ID
- Update a todo by ID
- Delete a todo by ID
- OpenAPI documentation
POST /register
- Register a new userPOST /login
- Login a userPOST /logout
- Logout a user
POST /todos
- Create a new todoGET /todos
- Get all todosGET /todos/{id}
- Get a todo by IDPUT /todos/{id}
- Update a todo by IDDELETE /todos/{id}
- Delete a todo by ID
GET /docs/openapi.yaml
- OpenAPI specificationGET /swagger
- Swagger UI
-
Clone the repository:
git clone https://github.com/afutofu/go-api-starter.git cd go-api-starter
-
Install dependencies:
go mod tidy
-
Run the server:
go run main.go
Register a user:
curl -X POST http://localhost:8000/register -H "Content-Type: application/json" -d '{"username":"testuser", "password":"password123"}'
Login:
curl -X POST http://localhost:8000/login -H "Content-Type: application/json" -d '{"username":"testuser", "password":"password123"}'
Logout user:
curl -X POST http://localhost:8000/logout
For each of the following, include the header "Authorization: Bearer x". Where x is the access token received from the server via the /auth/login route
Create a Todo:
curl -X POST http://localhost:8000/todos -H "Content-Type: application/json" -d '{"text":"Test Todo", "completed":false}'
Get All Todos:
curl -X GET http://localhost:8000/todos
Get a Todo by ID:
curl -X GET http://localhost:8000/todos/1
Update a Todo by ID:
curl -X PUT http://localhost:8000/todos/1 -H "Content-Type: application/json" -d '{"text":"Updated Todo", "completed":true}'
Delete a Todo by ID:
curl -X DELETE http://localhost:8000/todos/1
Navigate to:
http://localhost:8000/swagger
- Afuza: Create and maintain repository