Skip to content

Commit a14ff54

Browse files
committed
feat: creation of gitignore, improvement of file structure, addition and removal of postgresql for the addition of local sqlite3, addition of migrations and tests of the application and validation, creation of pkg/ with errors and loggers.
1 parent 528ae15 commit a14ff54

File tree

23 files changed

+764
-107
lines changed

23 files changed

+764
-107
lines changed

.env.example

Whitespace-only changes.

.gitignore

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Binaries for programs and plugins
2+
*.exe
3+
*.exe~
4+
*.dll
5+
*.so
6+
*.dylib
7+
8+
# Test binary, built with `go test -c`
9+
*.test
10+
11+
# Output of the go coverage tool
12+
*.out
13+
14+
# Dependency directories
15+
vendor/
16+
17+
# Go workspace file
18+
go.work
19+
20+
# IDE specific files
21+
.idea/
22+
.vscode/
23+
*.swp
24+
*.swo
25+
26+
# Environment files
27+
.env
28+
.env.local
29+
30+
# OS generated files
31+
.DS_Store
32+
.DS_Store?
33+
._*
34+
.Spotlight-V100
35+
.Trashes
36+
ehthumbs.db
37+
Thumbs.db
38+
39+
# Project specific
40+
tmp/
41+
logs/
42+
43+
# Build
44+
build/
45+
# Binary name
46+
api-server
47+
src/database.db
48+
src/cmd/database.db

Makefile

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
.PHONY: build run test clean
2+
3+
# Build variables
4+
BINARY_NAME=api-server
5+
BUILD_DIR=build
6+
7+
# Enable CGO for SQLite
8+
export CGO_ENABLED=1
9+
10+
build:
11+
@echo "Building with CGO enabled..."
12+
@go build -o $(BUILD_DIR)/$(BINARY_NAME) ./src/cmd/main.go
13+
14+
run:
15+
@CGO_ENABLED=1 go run ./src/cmd/main.go
16+
17+
test:
18+
@go test -v ./...
19+
20+
clean:
21+
@echo "Cleaning..."
22+
@rm -rf $(BUILD_DIR)
23+
@go clean
24+
25+
dev:
26+
@CGO_ENABLED=1 go run ./src/cmd/main.go
27+
28+
install:
29+
@go mod download
30+
31+
# Cross compilation targets
32+
build-windows:
33+
@echo "Building for Windows..."
34+
@set CGO_ENABLED=1
35+
@set GOOS=windows
36+
@set GOARCH=amd64
37+
@go build -o $(BUILD_DIR)/$(BINARY_NAME).exe ./src/cmd/main.go
38+
39+
build-linux:
40+
@echo "Building for Linux..."
41+
@CGO_ENABLED=1 GOOS=linux GOARCH=amd64 \
42+
go build -o $(BUILD_DIR)/$(BINARY_NAME)-linux ./src/cmd/main.go

cmd/main.go

Lines changed: 0 additions & 51 deletions
This file was deleted.

db/conn.go

Lines changed: 0 additions & 50 deletions
This file was deleted.

go.mod

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@ require (
77
github.com/lib/pq v1.10.9
88
)
99

10+
require (
11+
github.com/mattn/go-sqlite3 v1.14.32 // indirect
12+
go.uber.org/multierr v1.11.0 // indirect
13+
go.uber.org/zap v1.27.0 // indirect
14+
)
15+
16+
require (
17+
github.com/davecgh/go-spew v1.1.1 // indirect
18+
github.com/pmezard/go-difflib v1.0.0 // indirect
19+
github.com/stretchr/testify v1.10.0
20+
)
21+
1022
require (
1123
github.com/bytedance/sonic v1.11.6 // indirect
1224
github.com/bytedance/sonic/loader v0.1.1 // indirect

go.sum

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
4242
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
4343
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
4444
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
45+
github.com/mattn/go-sqlite3 v1.14.32 h1:JD12Ag3oLy1zQA+BNn74xRgaBbdhbNIDYvQUEuuErjs=
46+
github.com/mattn/go-sqlite3 v1.14.32/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
4547
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
4648
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
4749
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
@@ -61,12 +63,17 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
6163
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
6264
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
6365
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
64-
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
6566
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
67+
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
68+
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
6669
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
6770
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
6871
github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE=
6972
github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
73+
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
74+
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
75+
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
76+
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
7077
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
7178
golang.org/x/arch v0.8.0 h1:3wRIsP3pM4yUptoR96otTUOXI367OS0+c9eeRi9doIc=
7279
golang.org/x/arch v0.8.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=

src/cmd/main.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package main
2+
3+
import (
4+
"api-go/src/config"
5+
"api-go/src/controller"
6+
"api-go/src/database"
7+
"api-go/src/middleware"
8+
"api-go/src/pkg/logger"
9+
"api-go/src/repository"
10+
"api-go/src/usecase"
11+
"context"
12+
"net/http"
13+
"os"
14+
"os/signal"
15+
"time"
16+
17+
"github.com/gin-gonic/gin"
18+
"go.uber.org/zap"
19+
)
20+
21+
func main() {
22+
// Initialize config
23+
cfg := config.LoadConfig()
24+
25+
// Initialize logger
26+
logger.Init("development")
27+
28+
// Initialize database
29+
db, err := database.InitDB()
30+
if err != nil {
31+
logger.Fatal("Failed to initialize database", zap.Error(err))
32+
}
33+
defer db.Close()
34+
35+
// Initialize repositories and services
36+
productRepository := repository.NewProductRepository(db)
37+
productUseCase := usecase.NewProductUseCase(productRepository)
38+
productController := controller.NewProductController(*productUseCase)
39+
40+
// Initialize Gin
41+
gin.SetMode(gin.ReleaseMode)
42+
server := gin.Default()
43+
44+
// Health check
45+
server.GET("/health", func(ctx *gin.Context) {
46+
ctx.JSON(http.StatusOK, gin.H{
47+
"status": "UP",
48+
})
49+
})
50+
51+
// Protected routes
52+
protected := server.Group("/")
53+
protected.Use(middleware.AuthMiddleware())
54+
{
55+
protected.GET("/products", productController.GetProducts)
56+
protected.POST("/product", productController.CreateProduct)
57+
protected.GET("/product/:productId", productController.GetProductsById)
58+
protected.PUT("/product", productController.UpdateProduct)
59+
protected.DELETE("/product/:productId", productController.DeleteProduct)
60+
}
61+
62+
// Server configuration
63+
srv := &http.Server{
64+
Addr: ":" + cfg.Server.Port,
65+
Handler: server,
66+
}
67+
68+
// Graceful shutdown
69+
go func() {
70+
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
71+
logger.Fatal("Failed to start server", zap.Error(err))
72+
}
73+
}()
74+
75+
// Wait for interrupt signal
76+
quit := make(chan os.Signal, 1)
77+
signal.Notify(quit, os.Interrupt)
78+
<-quit
79+
80+
// Shutdown server
81+
logger.Info("Shutting down server...")
82+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
83+
defer cancel()
84+
85+
if err := srv.Shutdown(ctx); err != nil {
86+
logger.Fatal("Server forced to shutdown", zap.Error(err))
87+
}
88+
89+
logger.Info("Server exiting")
90+
}

src/config/config.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package config
2+
3+
import (
4+
"os"
5+
)
6+
7+
type Config struct {
8+
Database DatabaseConfig
9+
Server ServerConfig
10+
}
11+
12+
type DatabaseConfig struct {
13+
Path string
14+
}
15+
16+
type ServerConfig struct {
17+
Port string
18+
}
19+
20+
func LoadConfig() *Config {
21+
return &Config{
22+
Database: DatabaseConfig{
23+
Path: getEnv("DB_PATH", "./database.db"),
24+
},
25+
Server: ServerConfig{
26+
Port: getEnv("SERVER_PORT", "8080"),
27+
},
28+
}
29+
}
30+
31+
func getEnv(key string, defaultValue string) string {
32+
if value, exists := os.LookupEnv(key); exists {
33+
return value
34+
}
35+
return defaultValue
36+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package controller
22

33
import (
4-
"api-go/model"
5-
"api-go/usecase"
4+
"api-go/src/model"
5+
"api-go/src/usecase"
66
"net/http"
77
"strconv"
88

0 commit comments

Comments
 (0)