├── tests └── integration │ └── todo_repository_integration_test.go ├── .gitignore ├── .dockerignore ├── app ├── customerror │ ├── unauthorized_error.go │ └── todo_not_found_error.go ├── model │ ├── todo.go │ └── user.go ├── dtos │ ├── todo.go │ └── auth.go ├── middleware │ ├── cors_middleware.go │ ├── logger_middleware.go │ └── auth_middleware.go ├── service │ ├── todo_service_test.go │ ├── todo_service.go │ └── auth_service.go ├── handler │ ├── auth_handler.go │ └── todo_handler.go ├── repository │ ├── user_repository.go │ └── todo_repository.go └── util │ └── valerr.go ├── scripts └── db │ ├── 002_insert_users.sql │ └── 001_create_tables.sql ├── Dockerfile ├── router ├── auth.go ├── todo.go └── router.go ├── database └── database.go ├── cmd └── serve.go ├── .air.toml ├── docker-compose.yml ├── go.mod ├── config └── config.go ├── README.md ├── docs └── openapi.yaml └── go.sum /tests/integration/todo_repository_integration_test.go: -------------------------------------------------------------------------------- 1 | package integration 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # IDE Files 2 | .idea/ 3 | .vscode/ 4 | 5 | # Generated mocks 6 | **/mock_*.go 7 | 8 | # Binaries 9 | bin/ 10 | tmp/ 11 | 12 | .env 13 | -------------------------------------------------------------------------------- /.dockerignore: -------------------------------------------------------------------------------- 1 | *_test.go 2 | 3 | README.md 4 | .air.toml 5 | Dockerfile 6 | .dockerignore 7 | docker-compose.yml 8 | 9 | .git/ 10 | .gitignore 11 | 12 | bin/ 13 | tmp/ 14 | 15 | docs/ 16 | scripts/ 17 | 18 | .idea/ 19 | .vscode/ 20 | -------------------------------------------------------------------------------- /app/customerror/unauthorized_error.go: -------------------------------------------------------------------------------- 1 | package customerror 2 | 3 | type unauthorizedTodoError struct { 4 | ID string 5 | } 6 | 7 | func (e *unauthorizedTodoError) Error() string { 8 | return "Can not access a todo with the ID: " + e.ID 9 | } 10 | -------------------------------------------------------------------------------- /scripts/db/002_insert_users.sql: -------------------------------------------------------------------------------- 1 | INSERT INTO todo_app.app_roles (id, name) 2 | VALUES (1, 'admin'), (2, 'user'); 3 | 4 | INSERT INTO todo_app.app_users (username, password, role_id, active) 5 | VALUES ('admin', 'admin', 1, true), ('user', 'user', 2, true); 6 | -------------------------------------------------------------------------------- /app/model/todo.go: -------------------------------------------------------------------------------- 1 | package model 2 | 3 | type Todo struct { 4 | ID int `db:"id"` 5 | Title string `db:"title"` 6 | Description string `db:"description"` 7 | Completed bool `db:"completed"` 8 | UserID int `db:"user_id"` 9 | } 10 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM golang:1.23.4-alpine3.21 AS builder 2 | 3 | WORKDIR /app 4 | 5 | COPY . . 6 | 7 | RUN go build -o ./bin/serve ./cmd/serve.go 8 | 9 | FROM alpine:3.21.2 10 | 11 | WORKDIR /app 12 | 13 | EXPOSE 8080 14 | 15 | COPY --from=builder /app/bin/serve /app/serve 16 | 17 | CMD ["./serve"] 18 | -------------------------------------------------------------------------------- /app/customerror/todo_not_found_error.go: -------------------------------------------------------------------------------- 1 | package customerror 2 | 3 | import "strconv" 4 | 5 | type TodoNotFoundError struct { 6 | ID int 7 | } 8 | 9 | func (e *TodoNotFoundError) Error() string { 10 | return "Todo with the given ID not found: " + strconv.Itoa(e.ID) 11 | } 12 | 13 | func NewTodoNotFoundError(ID int) error { 14 | return &TodoNotFoundError{ID} 15 | } 16 | -------------------------------------------------------------------------------- /app/model/user.go: -------------------------------------------------------------------------------- 1 | package model 2 | 3 | type User struct { 4 | ID int `db:"id"` 5 | Username string `db:"username"` 6 | Password string `db:"password"` 7 | RoleID int `db:"role_id"` 8 | Active bool `db:"active"` 9 | } 10 | 11 | type Role struct { 12 | ID int `db:"id"` 13 | Name string `db:"name"` 14 | } 15 | 16 | type UserWithRole struct { 17 | User 18 | Role Role `db:"role"` 19 | } 20 | -------------------------------------------------------------------------------- /app/dtos/todo.go: -------------------------------------------------------------------------------- 1 | package dtos 2 | 3 | type TodoResponse struct { 4 | ID int `json:"id"` 5 | Title string `json:"title"` 6 | Description string `json:"description"` 7 | Completed bool `json:"completed"` 8 | } 9 | 10 | type TodoCreateRequest struct { 11 | Title string `json:"title" binding:"required"` 12 | Description string `json:"description"` 13 | Completed bool `json:"completed"` 14 | } 15 | -------------------------------------------------------------------------------- /app/middleware/cors_middleware.go: -------------------------------------------------------------------------------- 1 | package middleware 2 | 3 | import ( 4 | "github.com/gin-contrib/cors" 5 | "github.com/gin-gonic/gin" 6 | "github.com/juanpicasti/go-todo-app/config" 7 | "strings" 8 | ) 9 | 10 | func GetCorsMiddleware() gin.HandlerFunc { 11 | corsConfig := cors.DefaultConfig() 12 | allowedOriginsString := config.CFG.AllowedOrigins 13 | allowedOrigins := strings.Split(allowedOriginsString, ",") 14 | corsConfig.AllowOrigins = allowedOrigins 15 | return cors.New(corsConfig) 16 | } 17 | -------------------------------------------------------------------------------- /router/auth.go: -------------------------------------------------------------------------------- 1 | package router 2 | 3 | import ( 4 | "github.com/juanpicasti/go-todo-app/app/handler" 5 | "github.com/juanpicasti/go-todo-app/app/repository" 6 | "github.com/juanpicasti/go-todo-app/app/service" 7 | ) 8 | 9 | func (r *Router) initAuthHandler() { 10 | userRepo := repository.NewUserRepository(r.db) 11 | authService := service.NewAuthService(userRepo) 12 | r.authHandler = handler.NewAuthHandler(authService) 13 | } 14 | 15 | func (r *Router) setupAuthRoutes() { 16 | r.engine.POST("/login", r.authHandler.Login) 17 | r.engine.POST("/register", r.authHandler.Register) 18 | } 19 | -------------------------------------------------------------------------------- /database/database.go: -------------------------------------------------------------------------------- 1 | package database 2 | 3 | import ( 4 | "fmt" 5 | "github.com/juanpicasti/go-todo-app/config" 6 | 7 | "github.com/jmoiron/sqlx" 8 | _ "github.com/lib/pq" 9 | ) 10 | 11 | func Connect() (*sqlx.DB, error) { 12 | constr := fmt.Sprintf( 13 | "user=%s password=%s host=%s port=%s dbname=%s sslmode=%s sslrootcert=%s", 14 | config.CFG.DatabaseUser, 15 | config.CFG.DatabasePassword, 16 | config.CFG.DatabaseHost, 17 | config.CFG.DatabasePort, 18 | config.CFG.DatabaseName, 19 | config.CFG.DatabaseSslMode, 20 | config.CFG.Sslrootcert, 21 | ) 22 | 23 | dbConnection, err := sqlx.Connect("postgres", constr) 24 | if err != nil { 25 | return nil, err 26 | } 27 | 28 | err = dbConnection.Ping() 29 | if err != nil { 30 | return nil, err 31 | } 32 | 33 | return dbConnection, err 34 | } 35 | -------------------------------------------------------------------------------- /app/middleware/logger_middleware.go: -------------------------------------------------------------------------------- 1 | package middleware 2 | 3 | import ( 4 | "time" 5 | 6 | "github.com/gin-gonic/gin" 7 | "github.com/rs/zerolog/log" 8 | ) 9 | 10 | func LoggerMiddleware() gin.HandlerFunc { 11 | return func(c *gin.Context) { 12 | start := time.Now() 13 | path := c.Request.URL.Path 14 | raw := c.Request.URL.RawQuery 15 | 16 | // Process the request 17 | c.Next() 18 | 19 | end := time.Now() 20 | latency := end.Sub(start) 21 | 22 | statusCode := c.Writer.Status() 23 | clientIP := c.ClientIP() 24 | method := c.Request.Method 25 | errorMessage := c.Errors.ByType(gin.ErrorTypePrivate).String() 26 | 27 | if raw != "" { 28 | path = path + "?" + raw 29 | } 30 | 31 | log.Info(). 32 | Int("status", statusCode). 33 | Str("method", method). 34 | Str("path", path). 35 | Str("client_ip", clientIP). 36 | Dur("latency", latency). 37 | Str("error", errorMessage). 38 | Msg("Request processed") 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /router/todo.go: -------------------------------------------------------------------------------- 1 | package router 2 | 3 | import ( 4 | "github.com/juanpicasti/go-todo-app/app/handler" 5 | "github.com/juanpicasti/go-todo-app/app/middleware" 6 | "github.com/juanpicasti/go-todo-app/app/repository" 7 | "github.com/juanpicasti/go-todo-app/app/service" 8 | ) 9 | 10 | func (r *Router) initTodoHandler() { 11 | todoRepo := repository.NewTodoRepository(r.db) 12 | todoService := service.NewTodoService(todoRepo) 13 | r.todoHandler = handler.NewTodoHandler(todoService) 14 | } 15 | 16 | func (r *Router) setupApiRoutes() { 17 | api := r.engine.Group("/api/v1") 18 | 19 | api.Use(middleware.AuthMiddleware()) 20 | api.Use(middleware.RoleMiddleware(map[string]bool{ 21 | "admin": true, 22 | "user": true, 23 | })) 24 | 25 | api.GET("/todos", r.todoHandler.GetAll) 26 | api.GET("/todos/:id", r.todoHandler.GetById) 27 | api.POST("/todos", r.todoHandler.Create) 28 | api.PUT("/todos/:id", r.todoHandler.Update) 29 | api.DELETE("/todos/:id", r.todoHandler.Delete) 30 | } 31 | -------------------------------------------------------------------------------- /cmd/serve.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "github.com/juanpicasti/go-todo-app/config" 5 | "github.com/juanpicasti/go-todo-app/database" 6 | "github.com/juanpicasti/go-todo-app/router" 7 | "github.com/rs/zerolog/log" 8 | 9 | "github.com/jmoiron/sqlx" 10 | ) 11 | 12 | func main() { 13 | // Load environment variables 14 | config.LoadConfig() 15 | // Initialize database connection 16 | db, err := database.Connect() 17 | if err != nil { 18 | log.Error().Err(err).Msg("Error connecting to database: ") 19 | panic(err) 20 | } 21 | 22 | defer func(db *sqlx.DB) { 23 | err := db.Close() 24 | if err != nil { 25 | log.Error().Err(err).Msg("Error closing database connection: ") 26 | panic(err) 27 | } 28 | }(db) 29 | 30 | // Initialize server 31 | r := router.SetupRouter(db) 32 | log.Info().Msg("Starting server on port " + config.CFG.ServerPort) 33 | err = r.Run(config.CFG.ServerPort) 34 | if err != nil { 35 | log.Error().Err(err).Msg("Error starting server: ") 36 | panic(err) 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /app/dtos/auth.go: -------------------------------------------------------------------------------- 1 | package dtos 2 | 3 | import "github.com/golang-jwt/jwt/v5" 4 | 5 | type LoginRequest struct { 6 | Username string `json:"username" binding:"required"` 7 | Password string `json:"password" binding:"required"` 8 | } 9 | 10 | type RegisterRequest struct { 11 | Username string `json:"username" binding:"required,min=5"` 12 | Password string `json:"password" binding:"required,min=8"` 13 | PasswordRepeat string `json:"password_repeat" binding:"required,eqfield=Password"` 14 | PhoneNumber string `json:"phone_number" binding:"required"` 15 | Email string `json:"email" binding:"required,email"` 16 | Name string `json:"name" binding:"required"` 17 | LastName string `json:"last_name" binding:"required"` 18 | } 19 | 20 | type RegisterResponse struct { 21 | Username string `json:"username"` 22 | } 23 | 24 | type LoginResponse struct { 25 | Token string `json:"token"` 26 | } 27 | 28 | type Claims struct { 29 | UserID int `json:"user_id"` 30 | RoleID int `json:"role_id"` 31 | RoleName string `json:"role_name"` 32 | jwt.RegisteredClaims 33 | } 34 | -------------------------------------------------------------------------------- /.air.toml: -------------------------------------------------------------------------------- 1 | root = "." 2 | testdata_dir = "testdata" 3 | tmp_dir = "tmp" 4 | 5 | [build] 6 | args_bin = [] 7 | bin = "./tmp/main" 8 | cmd = "go build -o ./tmp/main cmd/serve.go" 9 | delay = 1000 10 | exclude_dir = ["assets", "tmp", "vendor", "testdata"] 11 | exclude_file = [] 12 | exclude_regex = ["_test.go"] 13 | exclude_unchanged = false 14 | follow_symlink = false 15 | full_bin = "" 16 | include_dir = [] 17 | include_ext = ["go", "tpl", "tmpl", "html"] 18 | include_file = [] 19 | kill_delay = "0s" 20 | log = "build-errors.log" 21 | poll = false 22 | poll_interval = 0 23 | post_cmd = [] 24 | pre_cmd = [] 25 | rerun = false 26 | rerun_delay = 500 27 | send_interrupt = false 28 | stop_on_error = false 29 | 30 | [color] 31 | app = "" 32 | build = "yellow" 33 | main = "magenta" 34 | runner = "green" 35 | watcher = "cyan" 36 | 37 | [log] 38 | main_only = false 39 | silent = false 40 | time = false 41 | 42 | [misc] 43 | clean_on_exit = false 44 | 45 | [proxy] 46 | app_port = 0 47 | enabled = false 48 | proxy_port = 0 49 | 50 | [screen] 51 | clear_on_rebuild = false 52 | keep_scroll = true 53 | -------------------------------------------------------------------------------- /scripts/db/001_create_tables.sql: -------------------------------------------------------------------------------- 1 | CREATE SCHEMA IF NOT EXISTS todo_app; 2 | 3 | -- Users, Roles and Permissions 4 | 5 | CREATE TABLE IF NOT EXISTS todo_app.app_users ( 6 | id SERIAL, 7 | username VARCHAR(255) NOT NULL UNIQUE, 8 | password VARCHAR(255) NOT NULL, 9 | role_id SMALLINT NOT NULL, 10 | active BOOLEAN NOT NULL 11 | ); 12 | 13 | CREATE TABLE IF NOT EXISTS todo_app.app_roles ( 14 | id smallint NOT NULL, 15 | name VARCHAR(255) NOT NULL 16 | ); 17 | 18 | -- Domain 19 | 20 | CREATE TABLE IF NOT EXISTS todo_app.todos ( 21 | id SERIAL, 22 | title VARCHAR(255) NOT NULL, 23 | description TEXT, 24 | completed BOOLEAN DEFAULT FALSE, 25 | user_id INTEGER NOT NULL 26 | ); 27 | 28 | -- Primary key constraints 29 | 30 | ALTER TABLE todo_app.app_users ADD PRIMARY KEY (id); 31 | 32 | ALTER TABLE todo_app.app_roles ADD PRIMARY KEY (id); 33 | 34 | ALTER TABLE todo_app.todos ADD PRIMARY KEY (id); 35 | 36 | -- Foreign key constraints 37 | 38 | ALTER TABLE todo_app.app_users ADD FOREIGN KEY (role_id) REFERENCES todo_app.app_roles(id); 39 | 40 | Alter TABLE todo_app.todos ADD FOREIGN KEY (user_id) REFERENCES todo_app.app_users(id); 41 | 42 | -------------------------------------------------------------------------------- /app/service/todo_service_test.go: -------------------------------------------------------------------------------- 1 | package service 2 | 3 | import ( 4 | "github.com/juanpicasti/go-todo-app/app/dtos" 5 | "github.com/juanpicasti/go-todo-app/app/model" 6 | "github.com/juanpicasti/go-todo-app/app/repository" 7 | "testing" 8 | 9 | "github.com/golang/mock/gomock" 10 | "github.com/stretchr/testify/assert" 11 | ) 12 | 13 | func TestTodoService_GetAll(t *testing.T) { 14 | ctrl := gomock.NewController(t) 15 | defer ctrl.Finish() 16 | mockRepo := repository.NewMockTodoRepository(ctrl) 17 | service := NewTodoService(mockRepo) 18 | 19 | t.Run("Success - Returns todos", func(t *testing.T) { 20 | // Arrange 21 | mockTodos := []model.Todo{ 22 | {ID: 1, Title: "Test1", Description: "Desc1", Completed: false}, 23 | {ID: 2, Title: "Test2", Description: "Desc2", Completed: true}, 24 | } 25 | expectedResponse := []dtos.TodoResponse{ 26 | {ID: 1, Title: "Test1", Description: "Desc1", Completed: false}, 27 | {ID: 2, Title: "Test2", Description: "Desc2", Completed: true}, 28 | } 29 | mockRepo.EXPECT().GetAll().Return(mockTodos, nil) 30 | 31 | // Act 32 | result, err := service.GetAll() 33 | 34 | // Assert 35 | assert.NoError(t, err) 36 | assert.Equal(t, expectedResponse, result) 37 | }) 38 | } 39 | -------------------------------------------------------------------------------- /app/handler/auth_handler.go: -------------------------------------------------------------------------------- 1 | package handler 2 | 3 | import ( 4 | "github.com/gin-gonic/gin" 5 | "github.com/juanpicasti/go-todo-app/app/dtos" 6 | "github.com/juanpicasti/go-todo-app/app/service" 7 | "github.com/juanpicasti/go-todo-app/app/util" 8 | "net/http" 9 | ) 10 | 11 | type AuthHandler struct { 12 | authService service.AuthService 13 | } 14 | 15 | func NewAuthHandler(authService service.AuthService) *AuthHandler { 16 | return &AuthHandler{authService} 17 | } 18 | 19 | func (h *AuthHandler) Login(c *gin.Context) { 20 | var loginRequest dtos.LoginRequest 21 | if err := c.ShouldBindJSON(&loginRequest); err != nil { 22 | c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) 23 | return 24 | } 25 | 26 | token, err := h.authService.Login(loginRequest) 27 | if err != nil { 28 | c.JSON(http.StatusUnauthorized, gin.H{"error": err.Error()}) 29 | return 30 | } 31 | 32 | c.JSON(http.StatusOK, token) 33 | } 34 | 35 | func (h *AuthHandler) Register(c *gin.Context) { 36 | var registerRequest dtos.RegisterRequest 37 | if errs := util.BindJsonWithErrs(c, ®isterRequest); errs != nil { 38 | c.JSON(http.StatusBadRequest, errs) 39 | return 40 | } 41 | 42 | registerResponse, err := h.authService.Register(registerRequest, 1) 43 | if err != nil { 44 | c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) 45 | return 46 | } 47 | 48 | c.JSON(http.StatusOK, registerResponse) 49 | } 50 | -------------------------------------------------------------------------------- /router/router.go: -------------------------------------------------------------------------------- 1 | package router 2 | 3 | import ( 4 | "github.com/gin-gonic/gin" 5 | "github.com/jmoiron/sqlx" 6 | "github.com/juanpicasti/go-todo-app/app/handler" 7 | "github.com/juanpicasti/go-todo-app/app/middleware" 8 | "github.com/juanpicasti/go-todo-app/config" 9 | "github.com/rs/zerolog/log" 10 | ) 11 | 12 | type Router struct { 13 | engine *gin.Engine 14 | db *sqlx.DB 15 | todoHandler *handler.TodoHandler 16 | authHandler *handler.AuthHandler 17 | } 18 | 19 | func NewRouter(db *sqlx.DB) *Router { 20 | return &Router{ 21 | engine: gin.New(), 22 | db: db, 23 | } 24 | } 25 | 26 | func (r *Router) setupMiddleware() { 27 | r.engine.Use(middleware.LoggerMiddleware()) 28 | r.engine.Use(gin.Recovery()) 29 | r.engine.Use(middleware.GetCorsMiddleware()) 30 | } 31 | 32 | func SetupRouter(db *sqlx.DB) *gin.Engine { 33 | router := NewRouter(db) 34 | router.setTrustedProxies() 35 | router.initializeHandlers() 36 | router.setupMiddleware() 37 | router.setupRoutes() 38 | return router.engine 39 | } 40 | 41 | func (r *Router) setupRoutes() { 42 | r.setupAuthRoutes() 43 | r.setupApiRoutes() 44 | } 45 | 46 | func (r *Router) initializeHandlers() { 47 | r.initAuthHandler() 48 | r.initTodoHandler() 49 | } 50 | 51 | func (r *Router) setTrustedProxies() { 52 | err := r.engine.SetTrustedProxies(config.CFG.TrustedProxies) 53 | if err != nil { 54 | log.Error().Err(err).Msg("Could not set trusted proxies") 55 | panic(err) 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /app/repository/user_repository.go: -------------------------------------------------------------------------------- 1 | package repository 2 | 3 | //go:generate go run github.com/golang/mock/mockgen -destination=mock_user_repository.go -package=repository github.com/juanpicasti/go-todo-app/internal/app/repository UserRepository 4 | 5 | import ( 6 | "github.com/juanpicasti/go-todo-app/app/model" 7 | 8 | "github.com/jmoiron/sqlx" 9 | ) 10 | 11 | type UserRepository interface { 12 | FindByUsername(string) (*model.UserWithRole, error) 13 | CreateUser(user *model.User) error 14 | } 15 | 16 | type userRepository struct { 17 | db *sqlx.DB 18 | } 19 | 20 | func NewUserRepository(db *sqlx.DB) UserRepository { 21 | return &userRepository{db} 22 | } 23 | 24 | func (r *userRepository) FindByUsername(username string) (*model.UserWithRole, error) { 25 | query := ` 26 | SELECT 27 | u.*, 28 | r.id AS "role.id", 29 | r.name AS "role.name" 30 | FROM todo_app.app_users u 31 | JOIN todo_app.app_roles r ON u.role_id = r.id 32 | WHERE u.username = $1 33 | ` 34 | userWithRole := model.UserWithRole{} 35 | err := r.db.Get(&userWithRole, query, username) 36 | if err != nil { 37 | return nil, err 38 | } 39 | 40 | return &userWithRole, nil 41 | } 42 | 43 | func (r *userRepository) CreateUser(user *model.User) error { 44 | query := ` 45 | INSERT INTO todo_app.app_users (username, password, role_id, active) 46 | VALUES ($1, $2, $3, true) 47 | ` 48 | _, err := r.db.Exec(query, user.Username, user.Password, user.RoleID) 49 | return err 50 | } 51 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | services: 2 | ginplayground: 3 | build: 4 | context: . 5 | dockerfile: Dockerfile 6 | container_name: ginplayground-server 7 | environment: 8 | DATABASE_USER: ${DATABASE_USER} 9 | DATABASE_PASSWORD: ${DATABASE_PASSWORD} 10 | DATABASE_HOST: ${DATABASE_HOST} 11 | DATABASE_PORT: ${DATABASE_PORT} 12 | DATABASE_SSL_MODE: ${DATABASE_SSL_MODE} 13 | DATABASE_NAME: ${DATABASE_NAME} 14 | SSLROOTCERT: ${SSLROOTCERT} 15 | SERVER_PORT: ${SERVER_PORT} 16 | GO_MODE: ${GO_MODE} 17 | ALLOWED_ORIGINS: ${ALLOWED_ORIGINS} 18 | JWT_SECRET: ${JWT_SECRET} 19 | TOKEN_DURATION_MINUTES: ${TOKEN_DURATION_MINUTES} 20 | TRUSTED_PROXY_IPS: ${TRUSTED_PROXY_IPS} 21 | ports: 22 | - "${SERVER_PORT}:${SERVER_PORT}" 23 | depends_on: 24 | db: 25 | condition: service_healthy 26 | db: 27 | image: postgres:17.2-alpine3.21 28 | container_name: postgres-todo-app 29 | environment: 30 | POSTGRES_USER: ${DATABASE_USER} 31 | POSTGRES_PASSWORD: ${DATABASE_PASSWORD} 32 | POSTGRES_DB: ${DATABASE_NAME} 33 | volumes: 34 | - db-data:/var/lib/postgresql/data 35 | - ./scripts/db:/docker-entrypoint-initdb.d 36 | ports: 37 | - "${DATABASE_PORT}:${DATABASE_PORT}" 38 | healthcheck: 39 | test: [ "CMD-SHELL", "pg_isready -U ${DATABASE_USER} -d ${DATABASE_NAME}" ] 40 | interval: 5s 41 | timeout: 5s 42 | retries: 5 43 | start_period: 10s 44 | 45 | volumes: 46 | db-data: 47 | -------------------------------------------------------------------------------- /app/util/valerr.go: -------------------------------------------------------------------------------- 1 | package util 2 | 3 | import ( 4 | "encoding/json" 5 | "errors" 6 | "github.com/gin-gonic/gin" 7 | "github.com/go-playground/validator/v10" 8 | "reflect" 9 | ) 10 | 11 | // TODO: Make error messages * 12 | // TODO: Make input error struct instead of map[string]string * 13 | // TODO: implement own ShouldBindJSON to collect all unmarshalling errors *** 14 | 15 | func BuildErrorResponse(err error, bodyType reflect.Type) []map[string]string { 16 | var bindingErrors validator.ValidationErrors 17 | var jsonErr *json.UnmarshalTypeError 18 | if errors.As(err, &bindingErrors) { 19 | var errorMessages []map[string]string 20 | for _, valerr := range bindingErrors { 21 | fieldName := valerr.Field() 22 | field, _ := bodyType.Elem().FieldByName(fieldName) 23 | fieldJSONName, _ := field.Tag.Lookup("json") 24 | errorMessages = append(errorMessages, 25 | map[string]string{ 26 | "field": fieldJSONName, 27 | "msg": valerr.ActualTag(), 28 | }) 29 | } 30 | return errorMessages 31 | } else if errors.As(err, &jsonErr) { 32 | return []map[string]string{{ 33 | "field": jsonErr.Field, 34 | "msg": jsonErr.Type.String(), 35 | }, 36 | } 37 | } 38 | 39 | return []map[string]string{{ 40 | "error": err.Error(), 41 | "msg": "JSON unmarshalling error", 42 | }, 43 | } 44 | } 45 | 46 | func BindJsonWithErrs(c *gin.Context, body interface{}) []map[string]string { 47 | if err := c.ShouldBindJSON(body); err != nil { 48 | return BuildErrorResponse(err, reflect.TypeOf(body)) 49 | } 50 | return nil 51 | } 52 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/juanpicasti/go-todo-app 2 | 3 | go 1.23.4 4 | 5 | require ( 6 | github.com/gin-gonic/gin v1.10.0 7 | github.com/golang/mock v1.6.0 8 | github.com/jmoiron/sqlx v1.4.0 9 | github.com/joho/godotenv v1.5.1 10 | github.com/lib/pq v1.10.9 11 | github.com/rs/zerolog v1.33.0 12 | github.com/stretchr/testify v1.10.0 13 | ) 14 | 15 | require ( 16 | github.com/bytedance/sonic v1.12.6 // indirect 17 | github.com/bytedance/sonic/loader v0.2.1 // indirect 18 | github.com/cloudwego/base64x v0.1.4 // indirect 19 | github.com/cloudwego/iasm v0.2.0 // indirect 20 | github.com/davecgh/go-spew v1.1.1 // indirect 21 | github.com/gabriel-vasile/mimetype v1.4.7 // indirect 22 | github.com/gin-contrib/cors v1.7.3 // indirect 23 | github.com/gin-contrib/sse v1.0.0 // indirect 24 | github.com/go-playground/locales v0.14.1 // indirect 25 | github.com/go-playground/universal-translator v0.18.1 // indirect 26 | github.com/go-playground/validator/v10 v10.23.0 // indirect 27 | github.com/goccy/go-json v0.10.4 // indirect 28 | github.com/golang-jwt/jwt/v5 v5.2.1 // indirect 29 | github.com/google/go-cmp v0.6.0 // indirect 30 | github.com/json-iterator/go v1.1.12 // indirect 31 | github.com/klauspost/cpuid/v2 v2.2.9 // indirect 32 | github.com/kr/pretty v0.3.1 // indirect 33 | github.com/leodido/go-urn v1.4.0 // indirect 34 | github.com/mattn/go-colorable v0.1.13 // indirect 35 | github.com/mattn/go-isatty v0.0.20 // indirect 36 | github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect 37 | github.com/modern-go/reflect2 v1.0.2 // indirect 38 | github.com/pelletier/go-toml/v2 v2.2.3 // indirect 39 | github.com/pmezard/go-difflib v1.0.0 // indirect 40 | github.com/rogpeppe/go-internal v1.12.0 // indirect 41 | github.com/twitchyliquid64/golang-asm v0.15.1 // indirect 42 | github.com/ugorji/go/codec v1.2.12 // indirect 43 | golang.org/x/arch v0.12.0 // indirect 44 | golang.org/x/crypto v0.32.0 // indirect 45 | golang.org/x/net v0.33.0 // indirect 46 | golang.org/x/sys v0.29.0 // indirect 47 | golang.org/x/text v0.21.0 // indirect 48 | google.golang.org/protobuf v1.36.1 // indirect 49 | gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect 50 | gopkg.in/yaml.v3 v3.0.1 // indirect 51 | ) 52 | -------------------------------------------------------------------------------- /app/middleware/auth_middleware.go: -------------------------------------------------------------------------------- 1 | package middleware 2 | 3 | import ( 4 | "errors" 5 | "github.com/juanpicasti/go-todo-app/config" 6 | "net/http" 7 | "strings" 8 | 9 | "github.com/gin-gonic/gin" 10 | "github.com/golang-jwt/jwt/v5" 11 | "github.com/juanpicasti/go-todo-app/app/dtos" 12 | ) 13 | 14 | func AuthMiddleware() gin.HandlerFunc { 15 | return func(c *gin.Context) { 16 | authHeader := c.GetHeader("Authorization") 17 | if authHeader == "" { 18 | c.JSON(http.StatusUnauthorized, gin.H{"error": "Authorization header is required"}) 19 | c.Abort() 20 | return 21 | } 22 | 23 | bearerToken := strings.Split(authHeader, " ") 24 | 25 | if len(bearerToken) != 2 || bearerToken[0] != "Bearer" { 26 | c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid token"}) 27 | c.Abort() 28 | return 29 | } 30 | 31 | claims, err := validateToken(bearerToken[1]) 32 | if err != nil { 33 | c.JSON(http.StatusUnauthorized, gin.H{"error": err.Error()}) 34 | c.Abort() 35 | return 36 | } 37 | 38 | c.Set("UserID", claims.UserID) 39 | c.Set("RoleID", claims.RoleID) 40 | c.Set("RoleName", claims.RoleName) 41 | c.Next() 42 | } 43 | } 44 | 45 | func RoleMiddleware(allowed map[string]bool) gin.HandlerFunc { 46 | return func(c *gin.Context) { 47 | role, exists := c.Get("RoleName") 48 | 49 | if !exists { 50 | c.JSON(http.StatusUnauthorized, gin.H{"error": "Role not found in context"}) 51 | c.Abort() 52 | return 53 | } 54 | 55 | if !allowed[role.(string)] { 56 | c.JSON(http.StatusForbidden, gin.H{"error": "You don't have permission to access this resource"}) 57 | c.Abort() 58 | return 59 | } 60 | 61 | c.Next() 62 | } 63 | } 64 | 65 | func validateToken(tokenString string) (*dtos.Claims, error) { 66 | token, err := jwt.ParseWithClaims(tokenString, &dtos.Claims{}, func(token *jwt.Token) (interface{}, error) { 67 | if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok { 68 | return nil, errors.New("unexpected signing method") 69 | } 70 | return []byte(config.CFG.JWTSecret), nil 71 | }) 72 | if err != nil { 73 | return nil, err 74 | } 75 | 76 | claims, ok := token.Claims.(*dtos.Claims) 77 | if !ok || !token.Valid { 78 | return nil, errors.New("invalid token") 79 | } 80 | 81 | return claims, nil 82 | } 83 | -------------------------------------------------------------------------------- /config/config.go: -------------------------------------------------------------------------------- 1 | package config 2 | 3 | import ( 4 | "os" 5 | "strconv" 6 | "strings" 7 | "time" 8 | 9 | "github.com/joho/godotenv" 10 | "github.com/rs/zerolog" 11 | "github.com/rs/zerolog/log" 12 | ) 13 | 14 | type Config struct { 15 | DatabaseUser string 16 | DatabasePassword string 17 | DatabaseHost string 18 | DatabasePort string 19 | DatabaseSslMode string 20 | DatabaseName string 21 | Sslrootcert string 22 | ServerPort string 23 | JWTSecret string 24 | AllowedOrigins string 25 | TokenDurationMinutes int 26 | TrustedProxies []string 27 | } 28 | 29 | var CFG *Config 30 | 31 | func LoadConfig() { 32 | loadEnv() 33 | confLogger() 34 | } 35 | 36 | func loadEnv() { 37 | if os.Getenv("GO_MODE") != "release" { 38 | if err := godotenv.Load(); err != nil { 39 | log.Printf("Warning: .env file not found") 40 | } 41 | } 42 | 43 | CFG = &Config{ 44 | DatabaseUser: getEnv("DATABASE_USER"), 45 | DatabasePassword: getEnv("DATABASE_PASSWORD"), 46 | DatabaseHost: getEnv("DATABASE_HOST"), 47 | DatabasePort: getEnv("DATABASE_PORT"), 48 | DatabaseSslMode: getEnv("DATABASE_SSL_MODE"), 49 | DatabaseName: getEnv("DATABASE_NAME"), 50 | Sslrootcert: getEnv("SSLROOTCERT"), 51 | ServerPort: ":" + getEnv("SERVER_PORT"), 52 | JWTSecret: getEnv("JWT_SECRET"), 53 | AllowedOrigins: getEnv("ALLOWED_ORIGINS"), 54 | } 55 | 56 | tokenDurationMinutes, err := strconv.Atoi(getEnv("TOKEN_DURATION_MINUTES")) 57 | if err != nil { 58 | log.Error().Err(err) 59 | } 60 | CFG.TokenDurationMinutes = tokenDurationMinutes 61 | 62 | CFG.TrustedProxies = []string{} 63 | trustedProxies := getEnv("TRUSTED_PROXY_IPS") 64 | for _, proxy := range strings.Split(trustedProxies, ",") { 65 | CFG.TrustedProxies = append(CFG.TrustedProxies, string(proxy)) 66 | } 67 | } 68 | 69 | func confLogger() { 70 | zerolog.TimeFieldFormat = time.RFC3339 71 | log.Logger = zerolog.New(os.Stdout).With().Timestamp().Logger() 72 | } 73 | 74 | func getEnv(key string) string { 75 | if value := os.Getenv(key); value != "" { 76 | return value 77 | } 78 | 79 | log.Error().Msg("Environment variable " + key + " is not set.") 80 | panic("Environment variable " + key + " is not set.") 81 | } 82 | -------------------------------------------------------------------------------- /app/service/todo_service.go: -------------------------------------------------------------------------------- 1 | package service 2 | 3 | import ( 4 | "github.com/juanpicasti/go-todo-app/app/dtos" 5 | "github.com/juanpicasti/go-todo-app/app/model" 6 | "github.com/juanpicasti/go-todo-app/app/repository" 7 | ) 8 | 9 | type TodoService struct { 10 | repository repository.TodoRepository 11 | } 12 | 13 | func NewTodoService(repository repository.TodoRepository) *TodoService { 14 | return &TodoService{ 15 | repository: repository, 16 | } 17 | } 18 | 19 | func (s *TodoService) GetAll() ([]dtos.TodoResponse, error) { 20 | todos, err := s.repository.GetAll() 21 | return mapToResponseDtoSlice(todos), err 22 | } 23 | 24 | func (s *TodoService) Create(todoRequest dtos.TodoCreateRequest, userId int) (dtos.TodoResponse, error) { 25 | todo := mapToEntity(todoRequest) 26 | todo.UserID = userId 27 | newTodo, err := s.repository.Create(todo) 28 | if err != nil { 29 | return dtos.TodoResponse{}, err 30 | } 31 | return mapToResponseDto(newTodo), nil 32 | } 33 | 34 | func (s *TodoService) Update(todoRequest dtos.TodoCreateRequest, id int) (dtos.TodoResponse, error) { 35 | todo := mapToEntity(todoRequest) 36 | updatedTodo, err := s.repository.Update(todo, id) 37 | if err != nil { 38 | return dtos.TodoResponse{}, err 39 | } 40 | 41 | return mapToResponseDto(updatedTodo), nil 42 | } 43 | 44 | func (s *TodoService) GetById(id int) (dtos.TodoResponse, error) { 45 | todo, err := s.repository.GetById(id) 46 | if err != nil { 47 | return dtos.TodoResponse{}, err 48 | } 49 | 50 | return mapToResponseDto(todo), err 51 | } 52 | 53 | func (s *TodoService) Delete(id int) (dtos.TodoResponse, error) { 54 | todo, err := s.repository.Delete(id) 55 | if err != nil { 56 | return dtos.TodoResponse{}, err 57 | } 58 | 59 | return mapToResponseDto(todo), err 60 | } 61 | 62 | func mapToResponseDtoSlice(todos []model.Todo) []dtos.TodoResponse { 63 | if todos == nil { 64 | return []dtos.TodoResponse{} 65 | } 66 | 67 | todoResponses := make([]dtos.TodoResponse, 0, len(todos)) 68 | 69 | for _, todo := range todos { 70 | todoResponses = append(todoResponses, mapToResponseDto(todo)) 71 | } 72 | return todoResponses 73 | } 74 | 75 | func mapToResponseDto(todo model.Todo) dtos.TodoResponse { 76 | return dtos.TodoResponse{ 77 | ID: todo.ID, 78 | Title: todo.Title, 79 | Description: todo.Description, 80 | Completed: todo.Completed, 81 | } 82 | } 83 | 84 | func mapToEntity(todoRequest dtos.TodoCreateRequest) model.Todo { 85 | return model.Todo{ 86 | Title: todoRequest.Title, 87 | Description: todoRequest.Description, 88 | Completed: todoRequest.Completed, 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /app/service/auth_service.go: -------------------------------------------------------------------------------- 1 | package service 2 | 3 | import ( 4 | "errors" 5 | "github.com/juanpicasti/go-todo-app/config" 6 | "golang.org/x/crypto/bcrypt" 7 | "time" 8 | 9 | "github.com/golang-jwt/jwt/v5" 10 | "github.com/juanpicasti/go-todo-app/app/dtos" 11 | "github.com/juanpicasti/go-todo-app/app/model" 12 | "github.com/juanpicasti/go-todo-app/app/repository" 13 | ) 14 | 15 | type AuthService interface { 16 | Login(req dtos.LoginRequest) (*dtos.LoginResponse, error) 17 | Register(req dtos.RegisterRequest, roleId int) (*dtos.RegisterResponse, error) 18 | GenerateToken(user model.UserWithRole) (string, error) 19 | } 20 | 21 | type authService struct { 22 | userRepository repository.UserRepository 23 | } 24 | 25 | func NewAuthService(repository repository.UserRepository) AuthService { 26 | return &authService{ 27 | userRepository: repository, 28 | } 29 | } 30 | 31 | func (s *authService) Login(req dtos.LoginRequest) (*dtos.LoginResponse, error) { 32 | user, err := s.userRepository.FindByUsername(req.Username) 33 | if err != nil { 34 | return nil, errors.New("user not found") 35 | } 36 | 37 | if !checkPasswordHash(req.Password, user.Password) { 38 | return nil, errors.New("invalid password") 39 | } 40 | 41 | token, err := s.GenerateToken(*user) 42 | if err != nil { 43 | return nil, err 44 | } 45 | 46 | return &dtos.LoginResponse{Token: token}, nil 47 | } 48 | 49 | func (s *authService) Register(req dtos.RegisterRequest, roleId int) (*dtos.RegisterResponse, error) { 50 | hashedPassword, err := hashPassword(req.Password) 51 | 52 | if err != nil { 53 | return nil, err 54 | } 55 | 56 | user := model.User{ 57 | Username: req.Username, 58 | Password: string(hashedPassword), 59 | RoleID: roleId, 60 | } 61 | 62 | err = s.userRepository.CreateUser(&user) 63 | if err != nil { 64 | return nil, err 65 | } 66 | 67 | return &dtos.RegisterResponse{Username: user.Username}, nil 68 | } 69 | 70 | func checkPasswordHash(password, hash string) bool { 71 | err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password)) 72 | return err == nil 73 | } 74 | 75 | func hashPassword(password string) ([]byte, error) { 76 | bytes, err := bcrypt.GenerateFromPassword([]byte(password), 14) 77 | return bytes, err 78 | } 79 | 80 | func (s *authService) GenerateToken(user model.UserWithRole) (string, error) { 81 | claims := &dtos.Claims{ 82 | UserID: user.ID, 83 | RoleID: user.Role.ID, 84 | RoleName: user.Role.Name, 85 | RegisteredClaims: jwt.RegisteredClaims{ 86 | ExpiresAt: jwt.NewNumericDate(time.Now().Add(time.Minute * time.Duration(config.CFG.TokenDurationMinutes))), 87 | IssuedAt: jwt.NewNumericDate(time.Now()), 88 | NotBefore: jwt.NewNumericDate(time.Now()), 89 | }, 90 | } 91 | 92 | token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) 93 | return token.SignedString([]byte(config.CFG.JWTSecret)) 94 | } 95 | -------------------------------------------------------------------------------- /app/repository/todo_repository.go: -------------------------------------------------------------------------------- 1 | package repository 2 | 3 | //go:generate go run github.com/golang/mock/mockgen -destination=mock_todo_repository.go -package=repository github.com/juanpicasti/go-todo-app/internal/app/repository TodoRepository 4 | 5 | import ( 6 | "database/sql" 7 | "errors" 8 | 9 | "github.com/rs/zerolog/log" 10 | 11 | "github.com/juanpicasti/go-todo-app/app/customerror" 12 | "github.com/juanpicasti/go-todo-app/app/model" 13 | 14 | "github.com/jmoiron/sqlx" 15 | ) 16 | 17 | type TodoRepository interface { 18 | GetAll() ([]model.Todo, error) 19 | Create(todo model.Todo) (model.Todo, error) 20 | Update(todo model.Todo, id int) (model.Todo, error) 21 | GetById(id int) (model.Todo, error) 22 | Delete(id int) (model.Todo, error) 23 | } 24 | 25 | type todoRepository struct { 26 | db *sqlx.DB 27 | } 28 | 29 | func NewTodoRepository(db *sqlx.DB) TodoRepository { 30 | return &todoRepository{db} 31 | } 32 | 33 | func (r *todoRepository) GetAll() ([]model.Todo, error) { 34 | var todos []model.Todo 35 | err := r.db.Select(&todos, "SELECT * FROM todo_app.todos") 36 | if err != nil { 37 | return nil, err 38 | } 39 | return todos, nil 40 | } 41 | 42 | func (r *todoRepository) Create(todo model.Todo) (model.Todo, error) { 43 | var newTodo model.Todo 44 | query := ` 45 | INSERT INTO todo_app.todos (title, description, user_id) 46 | VALUES ($1, $2, $3) 47 | RETURNING id, title, description, completed 48 | ` 49 | err := r.db. 50 | QueryRow( 51 | query, 52 | todo.Title, 53 | todo.Description, 54 | todo.UserID). 55 | Scan( 56 | &newTodo.ID, 57 | &newTodo.Title, 58 | &newTodo.Description, 59 | &newTodo.Completed) 60 | if err != nil { 61 | return model.Todo{}, err 62 | } 63 | return newTodo, nil 64 | } 65 | 66 | func (r *todoRepository) Update(todo model.Todo, id int) (model.Todo, error) { 67 | var updatedTodo model.Todo 68 | query := ` 69 | UPDATE todo_app.todos 70 | SET title = $1, description = $2, completed = $3 71 | WHERE id = $4 72 | RETURNING id, title, description, completed 73 | ` 74 | 75 | err := r.db. 76 | QueryRow( 77 | query, 78 | todo.Title, 79 | todo.Description, 80 | todo.Completed, 81 | id). 82 | Scan( 83 | &updatedTodo.ID, 84 | &updatedTodo.Title, 85 | &updatedTodo.Description, 86 | &updatedTodo.Completed) 87 | if err != nil { 88 | return model.Todo{}, err 89 | } 90 | 91 | return updatedTodo, nil 92 | } 93 | 94 | func (r *todoRepository) GetById(id int) (model.Todo, error) { 95 | var todo model.Todo 96 | err := r.db.Get(&todo, "SELECT * FROM todo_app.todos WHERE id = $1", id) 97 | if errors.Is(sql.ErrNoRows, err) { 98 | return model.Todo{}, customerror.NewTodoNotFoundError(id) 99 | } 100 | if err != nil { 101 | return model.Todo{}, err 102 | } 103 | return todo, err 104 | } 105 | 106 | func (r *todoRepository) Delete(id int) (model.Todo, error) { 107 | todo, err := r.GetById(id) 108 | if err != nil { 109 | return model.Todo{}, err 110 | // Return custom error 111 | } 112 | 113 | _, err = r.db.Exec("DELETE FROM todo_app.todos WHERE id=$1", id) 114 | if err != nil { 115 | log.Error().Err(err).Msg(err.Error()) 116 | return model.Todo{}, err 117 | } 118 | 119 | return todo, nil 120 | } 121 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # go-todo-app 2 | 3 | This is a project template for a simple todo app using Go with the Gin framework and a PostgreSQL database for the persistence. 4 | 5 | The project is structured following a layered architecture, aiming to provide a clean separation of concerns and allowing easy testing and maintenance. 6 | 7 | The layers, from top to bottom, are: 8 | 9 | - **Middleware**: Contains the middleware functions that are applied to the routes, executing before the handlers. 10 | - **Handler**: Handles HTTP requests and responses. Interactions are preferably made with DTOs. 11 | - **Service**: Contains the business logic. 12 | - **Model**: Contains the domain entities. 13 | - **Repository**: Handles the data access. 14 | 15 | These layers are all found under the `/app` directory. Routing is defined in the `/router/router.go` file. 16 | 17 | Initialization scripts for the database are found under the `/scripts/db` directory. 18 | 19 | An example openapi specification is provided under the `/docs` directory, describing the example endpoints. 20 | 21 | ## Running the app 22 | 23 | The app is containerized using Docker. 24 | 25 | To run the app locally with the PostgreSQL database, just run `docker-compose up`. 26 | 27 | If you have Air installed, you can run the app with `air` for hot reloading. Remember to start the database and run the initialization scripts before, and to set up the environment variables. 28 | 29 | ## Environment variables 30 | 31 | The following environment variables are required: 32 | 33 | ``` 34 | DATABASE_USER=root 35 | DATABASE_PASSWORD=root 36 | DATABASE_HOST=localhost 37 | DATABASE_PORT=5432 38 | DATABASE_SSL_MODE=disable 39 | DATABASE_NAME=todo_app 40 | SSLROOTCERT=/ 41 | SERVER_PORT=8080 42 | GO_MODE=debug 43 | ALLOWED_ORIGINS=* 44 | JWT_SECRET=9e7e07a6d1c7a1e8b45a3ab5600c5dde307478d847b708d02698b7c0c2373367 45 | TOKEN_DURATION_MINUTES=5 46 | TRUSTED_PROXY_IPS=127.0.0.1,192.168.1.1 47 | ``` 48 | 49 | These are just examples, you should set them according to your environment. 50 | 51 | ## Most important used packages 52 | 53 | - **Gin**: Web framework. 54 | - **SQLX**: Extension for the standard `database/sql` package. 55 | - **Zerolog**: Logging library. 56 | 57 | ## Used and included middleware 58 | 59 | - **CORS**: To allow requests from the specified origins in the environment variables, separated by commas. 60 | - **Logger**: Logging of requests/responses with metadata like latency and status code. 61 | - **Auth**: Middleware for JWT authentication. It checks the Authorization header for a valid JWT token and sets claims in the context. 62 | - **Role**: Middleware for role-based authorization. It checks the claims set by the Auth middleware and compares them with the required role for the route. 63 | 64 | ## Basic entities 65 | 66 | - **User**: Represents a user of the application. It has an ID, a username, a password, a role, and an "active" field. 67 | - **Role**: Two roles are defined: `admin` and `user`. 68 | - **Todo**: Example domain entity. 69 | 70 | ## Testing 71 | 72 | `go get github.com/stretchr/testify` 73 | `go get github.com/golang/mock/mockgen` 74 | `go generate ./...` for generating mocks. 75 | 76 | Mocked interfaces should have their file annotated with `//go:generate go run github.com/golang/mock/mockgen -destination=mock_todo_repository.go -package=repository github.com/juanpicasti/go-todo-app/internal/app/repository TodoRepository` 77 | 78 | ### Run unit tests 79 | `go test ./internal/...` 80 | 81 | ### Run integration tests only 82 | `go test ./tests/integration/...` 83 | 84 | ### Run all tests 85 | `go test ./...` 86 | 87 | ### Run tests with coverage 88 | `go test -coverprofile=coverage.out ./...` 89 | `go tool cover -html=coverage.out` -------------------------------------------------------------------------------- /app/handler/todo_handler.go: -------------------------------------------------------------------------------- 1 | package handler 2 | 3 | import ( 4 | "errors" 5 | "fmt" 6 | "net/http" 7 | "strconv" 8 | 9 | "github.com/rs/zerolog/log" 10 | 11 | "github.com/juanpicasti/go-todo-app/app/customerror" 12 | "github.com/juanpicasti/go-todo-app/app/dtos" 13 | "github.com/juanpicasti/go-todo-app/app/service" 14 | 15 | "github.com/gin-gonic/gin" 16 | ) 17 | 18 | type TodoHandler struct { 19 | service *service.TodoService 20 | } 21 | 22 | func NewTodoHandler(service *service.TodoService) *TodoHandler { 23 | return &TodoHandler{ 24 | service: service, 25 | } 26 | } 27 | 28 | func (h *TodoHandler) GetAll(c *gin.Context) { 29 | todos, err := h.service.GetAll() 30 | if err != nil { 31 | c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) 32 | return 33 | } 34 | c.JSON(http.StatusOK, todos) 35 | } 36 | 37 | func (h *TodoHandler) Create(c *gin.Context) { 38 | var requestBody dtos.TodoCreateRequest 39 | 40 | if err := c.ShouldBindBodyWithJSON(&requestBody); err != nil { 41 | c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) 42 | return 43 | } 44 | 45 | userId, ok := c.Get("UserID") 46 | if !ok { 47 | c.JSON(http.StatusInternalServerError, gin.H{"error": "Could not get user ID from context."}) 48 | return 49 | } 50 | 51 | newTodo, err := h.service.Create(requestBody, userId.(int)) 52 | if err != nil { 53 | c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) 54 | return 55 | } 56 | 57 | c.JSON(http.StatusCreated, newTodo) 58 | } 59 | 60 | func (h *TodoHandler) Update(c *gin.Context) { 61 | var requestBody dtos.TodoCreateRequest 62 | 63 | idParam := c.Param("id") 64 | 65 | id, err := strconv.Atoi(idParam) 66 | 67 | if err != nil || id <= 0 { 68 | c.JSON(http.StatusBadRequest, gin.H{"error": "Todo id must be provided as a positive integer."}) 69 | return 70 | } 71 | 72 | if err := c.ShouldBindBodyWithJSON(&requestBody); err != nil { 73 | c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) 74 | return 75 | } 76 | 77 | updatedTodo, err := h.service.Update(requestBody, id) 78 | if err != nil { 79 | c.JSON(http.StatusNotFound, gin.H{"error": fmt.Sprintf("Todo with the given ID not found: %d", id)}) 80 | return 81 | } 82 | 83 | c.JSON(http.StatusOK, updatedTodo) 84 | } 85 | 86 | func (h *TodoHandler) GetById(c *gin.Context) { 87 | idParam := c.Param("id") 88 | 89 | id, err := strconv.Atoi(idParam) 90 | 91 | if err != nil || id <= 0 { 92 | c.JSON(http.StatusBadRequest, gin.H{"error": "Todo ID must be provided as a positive integer."}) 93 | return 94 | } 95 | 96 | todoResponse, err := h.service.GetById(id) 97 | 98 | var notFounderror *customerror.TodoNotFoundError 99 | 100 | if errors.As(err, ¬Founderror) { 101 | c.JSON(http.StatusNotFound, gin.H{"error": notFounderror.Error()}) 102 | return 103 | } else if err != nil { 104 | log.Error().Err(err).Msg(err.Error()) 105 | c.JSON(http.StatusInternalServerError, gin.H{"error": "Something unexpected happened when trying to get the todo."}) 106 | return 107 | } 108 | 109 | c.JSON(http.StatusOK, todoResponse) 110 | } 111 | 112 | func (h *TodoHandler) Delete(c *gin.Context) { 113 | idParam := c.Param("id") 114 | 115 | id, err := strconv.Atoi(idParam) 116 | 117 | if err != nil || id <= 0 { 118 | c.JSON(http.StatusBadRequest, gin.H{"error": "Todo ID must be provided as a positive integer."}) 119 | return 120 | } 121 | 122 | todoResponse, err := h.service.Delete(id) 123 | if err != nil { 124 | c.JSON(http.StatusNotFound, gin.H{"error": "Todo with the given ID not found."}) 125 | return 126 | } 127 | 128 | c.JSON(http.StatusOK, todoResponse) 129 | } 130 | -------------------------------------------------------------------------------- /docs/openapi.yaml: -------------------------------------------------------------------------------- 1 | openapi: 3.0.3 2 | info: 3 | title: Todo API 4 | description: A RESTful API for managing todos 5 | version: 1.0.0 6 | 7 | tags: 8 | - name: todos 9 | description: Everything about todos 10 | 11 | components: 12 | schemas: 13 | RegisterRequest: 14 | type: object 15 | required: 16 | - username 17 | - password 18 | - password_repeat 19 | - phone_number 20 | - email 21 | - name 22 | - last_name 23 | properties: 24 | username: 25 | type: string 26 | password: 27 | type: string 28 | format: password 29 | password_repeat: 30 | type: string 31 | format: password 32 | phone_number: 33 | type: string 34 | email: 35 | type: string 36 | format: email 37 | name: 38 | type: string 39 | last_name: 40 | type: string 41 | 42 | LoginRequest: 43 | type: object 44 | required: 45 | - username 46 | - password 47 | properties: 48 | username: 49 | type: string 50 | password: 51 | type: string 52 | format: password 53 | 54 | LoginResponse: 55 | type: object 56 | required: 57 | - token 58 | properties: 59 | token: 60 | type: string 61 | 62 | Todo: 63 | type: object 64 | required: 65 | - title 66 | properties: 67 | id: 68 | type: string 69 | format: uuid 70 | example: "123e4567-e89b-12d3-a456-426614174000" 71 | readOnly: true 72 | title: 73 | type: string 74 | example: "Buy groceries" 75 | minLength: 1 76 | maxLength: 100 77 | description: 78 | type: string 79 | example: "Get milk and eggs" 80 | maxLength: 500 81 | completed: 82 | type: boolean 83 | default: false 84 | created_at: 85 | type: string 86 | format: date-time 87 | readOnly: true 88 | updated_at: 89 | type: string 90 | format: date-time 91 | readOnly: true 92 | 93 | TodoCreate: 94 | type: object 95 | required: 96 | - title 97 | properties: 98 | title: 99 | type: string 100 | example: "Buy groceries" 101 | minLength: 1 102 | maxLength: 100 103 | description: 104 | type: string 105 | example: "Get milk and eggs" 106 | maxLength: 500 107 | 108 | Error: 109 | type: object 110 | properties: 111 | code: 112 | type: integer 113 | format: int32 114 | example: 400 115 | message: 116 | type: string 117 | example: "Invalid input" 118 | 119 | responses: 120 | BadRequest: 121 | description: Bad request 122 | content: 123 | application/json: 124 | schema: 125 | $ref: '#/components/schemas/Error' 126 | NotFound: 127 | description: Resource not found 128 | content: 129 | application/json: 130 | schema: 131 | $ref: '#/components/schemas/Error' 132 | 133 | paths: 134 | /register: 135 | post: 136 | summary: Register a new user 137 | security: [] 138 | requestBody: 139 | required: true 140 | content: 141 | application/json: 142 | schema: 143 | $ref: '#/components/schemas/RegisterRequest' 144 | responses: 145 | '201': 146 | description: User successfully registered 147 | '400': 148 | description: Invalid input 149 | '409': 150 | description: Username or email already exists 151 | 152 | /login: 153 | post: 154 | summary: Authenticate user and receive JWT token 155 | security: [] 156 | requestBody: 157 | required: true 158 | content: 159 | application/json: 160 | schema: 161 | $ref: '#/components/schemas/LoginRequest' 162 | responses: 163 | '200': 164 | description: Successfully authenticated 165 | content: 166 | application/json: 167 | schema: 168 | $ref: '#/components/schemas/LoginResponse' 169 | '401': 170 | description: Authentication failed 171 | 172 | /api/v1/todos: 173 | get: 174 | tags: 175 | - todos 176 | summary: List all todos 177 | description: Returns a list of todos 178 | operationId: listTodos 179 | parameters: 180 | - name: completed 181 | in: query 182 | description: Filter by completion status 183 | required: false 184 | schema: 185 | type: boolean 186 | - name: limit 187 | in: query 188 | description: Maximum number of items to return 189 | required: false 190 | schema: 191 | type: integer 192 | format: int32 193 | minimum: 1 194 | maximum: 100 195 | default: 20 196 | responses: 197 | '200': 198 | description: Successful operation 199 | content: 200 | application/json: 201 | schema: 202 | type: array 203 | items: 204 | $ref: '#/components/schemas/Todo' 205 | '400': 206 | $ref: '#/components/responses/BadRequest' 207 | 208 | post: 209 | tags: 210 | - todos 211 | summary: Create a new todo 212 | description: Creates a new todo item 213 | operationId: createTodo 214 | requestBody: 215 | description: Todo object to be created 216 | required: true 217 | content: 218 | application/json: 219 | schema: 220 | $ref: '#/components/schemas/TodoCreate' 221 | responses: 222 | '201': 223 | description: Todo created successfully 224 | content: 225 | application/json: 226 | schema: 227 | $ref: '#/components/schemas/Todo' 228 | '400': 229 | $ref: '#/components/responses/BadRequest' 230 | 231 | /api/v1/todos/{id}: 232 | parameters: 233 | - name: id 234 | in: path 235 | description: Todo ID 236 | required: true 237 | schema: 238 | type: string 239 | format: uuid 240 | 241 | get: 242 | tags: 243 | - todos 244 | summary: Get a todo by ID 245 | description: Returns a single todo 246 | operationId: getTodo 247 | responses: 248 | '200': 249 | description: Successful operation 250 | content: 251 | application/json: 252 | schema: 253 | $ref: '#/components/schemas/Todo' 254 | '404': 255 | $ref: '#/components/responses/NotFound' 256 | 257 | put: 258 | tags: 259 | - todos 260 | summary: Update a todo 261 | description: Updates an existing todo 262 | operationId: updateTodo 263 | requestBody: 264 | description: Todo object to be updated 265 | required: true 266 | content: 267 | application/json: 268 | schema: 269 | $ref: '#/components/schemas/TodoCreate' 270 | responses: 271 | '200': 272 | description: Todo updated successfully 273 | content: 274 | application/json: 275 | schema: 276 | $ref: '#/components/schemas/Todo' 277 | '404': 278 | $ref: '#/components/responses/NotFound' 279 | '400': 280 | $ref: '#/components/responses/BadRequest' 281 | 282 | delete: 283 | tags: 284 | - todos 285 | summary: Delete a todo 286 | description: Deletes a todo 287 | operationId: deleteTodo 288 | responses: 289 | '204': 290 | description: Todo deleted successfully 291 | '404': 292 | $ref: '#/components/responses/NotFound' -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= 2 | filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= 3 | github.com/bytedance/sonic v1.12.6 h1:/isNmCUF2x3Sh8RAp/4mh4ZGkcFAX/hLrzrK3AvpRzk= 4 | github.com/bytedance/sonic v1.12.6/go.mod h1:B8Gt/XvtZ3Fqj+iSKMypzymZxw/FVwgIGKzMzT9r/rk= 5 | github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= 6 | github.com/bytedance/sonic/loader v0.2.1 h1:1GgorWTqf12TA8mma4DDSbaQigE2wOgQo7iCjjJv3+E= 7 | github.com/bytedance/sonic/loader v0.2.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= 8 | github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y= 9 | github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w= 10 | github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg= 11 | github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY= 12 | github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= 13 | github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= 14 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 15 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 16 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 17 | github.com/gabriel-vasile/mimetype v1.4.7 h1:SKFKl7kD0RiPdbht0s7hFtjl489WcQ1VyPW8ZzUMYCA= 18 | github.com/gabriel-vasile/mimetype v1.4.7/go.mod h1:GDlAgAyIRT27BhFl53XNAFtfjzOkLaF35JdEG0P7LtU= 19 | github.com/gin-contrib/cors v1.7.3 h1:hV+a5xp8hwJoTw7OY+a70FsL8JkVVFTXw9EcfrYUdns= 20 | github.com/gin-contrib/cors v1.7.3/go.mod h1:M3bcKZhxzsvI+rlRSkkxHyljJt1ESd93COUvemZ79j4= 21 | github.com/gin-contrib/sse v1.0.0 h1:y3bT1mUWUxDpW4JLQg/HnTqV4rozuW4tC9eFKTxYI9E= 22 | github.com/gin-contrib/sse v1.0.0/go.mod h1:zNuFdwarAygJBht0NTKiSi3jRf6RbqeILZ9Sp6Slhe0= 23 | github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU= 24 | github.com/gin-gonic/gin v1.10.0/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y= 25 | github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= 26 | github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= 27 | github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= 28 | github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= 29 | github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= 30 | github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= 31 | github.com/go-playground/validator/v10 v10.23.0 h1:/PwmTwZhS0dPkav3cdK9kV1FsAmrL8sThn8IHr/sO+o= 32 | github.com/go-playground/validator/v10 v10.23.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM= 33 | github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y= 34 | github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg= 35 | github.com/goccy/go-json v0.10.4 h1:JSwxQzIqKfmFX1swYPpUThQZp/Ka4wzJdK0LWVytLPM= 36 | github.com/goccy/go-json v0.10.4/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M= 37 | github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= 38 | github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk= 39 | github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk= 40 | github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc= 41 | github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs= 42 | github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= 43 | github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= 44 | github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= 45 | github.com/jmoiron/sqlx v1.4.0 h1:1PLqN7S1UYp5t4SrVVnt4nUVNemrDAtxlulVe+Qgm3o= 46 | github.com/jmoiron/sqlx v1.4.0/go.mod h1:ZrZ7UsYB/weZdl2Bxg6jCRO9c3YHl8r3ahlKmRT4JLY= 47 | github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= 48 | github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= 49 | github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= 50 | github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= 51 | github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= 52 | github.com/klauspost/cpuid/v2 v2.2.9 h1:66ze0taIn2H33fBvCkXuv9BmCwDfafmiIVpKV9kKGuY= 53 | github.com/klauspost/cpuid/v2 v2.2.9/go.mod h1:rqkxqrZ1EhYM9G+hXH7YdowN5R5RGN6NK4QwQ3WMXF8= 54 | github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M= 55 | github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= 56 | github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= 57 | github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= 58 | github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= 59 | github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= 60 | github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= 61 | github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= 62 | github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= 63 | github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= 64 | github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= 65 | github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= 66 | github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= 67 | github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= 68 | github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= 69 | github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= 70 | github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= 71 | github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= 72 | github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU= 73 | github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= 74 | github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= 75 | github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= 76 | github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= 77 | github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= 78 | github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= 79 | github.com/pelletier/go-toml/v2 v2.2.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNHvL12M= 80 | github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc= 81 | github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= 82 | github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= 83 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 84 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 85 | github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= 86 | github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8= 87 | github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4= 88 | github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= 89 | github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8= 90 | github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss= 91 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 92 | github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= 93 | github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= 94 | github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= 95 | github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= 96 | github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= 97 | github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= 98 | github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= 99 | github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= 100 | github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= 101 | github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= 102 | github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= 103 | github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= 104 | github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= 105 | github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE= 106 | github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= 107 | github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= 108 | golang.org/x/arch v0.12.0 h1:UsYJhbzPYGsT0HbEdmYcqtCv8UNGvnaL561NnIUvaKg= 109 | golang.org/x/arch v0.12.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= 110 | golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= 111 | golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= 112 | golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U= 113 | golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk= 114 | golang.org/x/crypto v0.32.0 h1:euUpcYgM8WcP71gNpTqQCn6rC2t6ULUPiOzfWaXVVfc= 115 | golang.org/x/crypto v0.32.0/go.mod h1:ZnnJkOaASj8g0AjIduWNlq2NRxL0PlBrbKVyZ6V/Ugc= 116 | golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= 117 | golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= 118 | golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= 119 | golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= 120 | golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I= 121 | golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4= 122 | golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 123 | golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 124 | golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 125 | golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 126 | golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 127 | golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 128 | golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 129 | golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 130 | golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 131 | golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 132 | golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA= 133 | golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= 134 | golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU= 135 | golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= 136 | golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= 137 | golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= 138 | golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= 139 | golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo= 140 | golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ= 141 | golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= 142 | golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= 143 | golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= 144 | golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 145 | golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 146 | golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 147 | google.golang.org/protobuf v1.36.1 h1:yBPeRvTftaleIgM3PZ/WBIZ7XM/eEYAaEyCwvyjq/gk= 148 | google.golang.org/protobuf v1.36.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE= 149 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 150 | gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= 151 | gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= 152 | gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 153 | gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= 154 | gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 155 | nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50= 156 | --------------------------------------------------------------------------------