This is a simple todo app built using Go. It uses the Gorilla mux router and Gorm ORM.
The app has the following endpoints:
Returns a JSON array of all todos.
Creates a new todo. The todo data should be sent in the request body as JSON.
Returns the todo with the given ID.
Updates the todo with the given ID. The new todo data should be sent in the request body as JSON.
Deletes the todo with the given ID.
- create a todoModel struct
- create a todos slice of todoModel
- create a getTodos function
- create a createTodo function
- create a getTodo function
- create a updateTodo function
- create a deleteTodo function
- create a router
- create a main function
- create a db connection
- migrate the db
- run the server
- set Environment variable:
export DATABASE_URL= < url >
Database url example:
postgres://username:password@localhost:5432/todo?sslmode=disable
go run todo.go
or
./bin/todo
-
curl -X POST -H "Content-Type: application/json" -d '{"title":"Buy milk","completed":false,"due":"0001-01-01T00:00:00Z"}' http://localhost:8000/todos
-
curl -X GET -H "Content-Type: application/json" http://localhost:8000/todos
-
curl -X GET -H "Content-Type: application/json" http://localhost:8000/todos/1
-
curl -X PUT -H "Content-Type: application/json" -d '{"title":"Buy milk","completed":true,"due":"0001-01-01T00:00:00Z"}' http://localhost:8000/todos/1
-
curl -X DELETE -H "Content-Type: application/json" http://localhost:8000/todos/1