first commit

This commit is contained in:
2025-08-12 21:51:41 +07:00
commit f1efac98a0
27 changed files with 2170 additions and 0 deletions

76
internal/config/config.go Normal file
View File

@@ -0,0 +1,76 @@
package config
import (
"log"
"os"
"strconv"
)
type Config struct {
Server ServerConfig
Database DatabaseConfig
}
type ServerConfig struct {
Port int
Mode string
}
type DatabaseConfig struct {
Host string
Port int
Username string
Password string
Database string
Schema string
}
func LoadConfig() *Config {
config := &Config{
Server: ServerConfig{
Port: getEnvAsInt("PORT", 8080),
Mode: getEnv("GIN_MODE", "debug"),
},
Database: DatabaseConfig{
Host: getEnv("BLUEPRINT_DB_HOST", "localhost"),
Port: getEnvAsInt("BLUEPRINT_DB_PORT", 5432),
Username: getEnv("BLUEPRINT_DB_USERNAME", "postgres"),
Password: getEnv("BLUEPRINT_DB_PASSWORD", "postgres"),
Database: getEnv("BLUEPRINT_DB_DATABASE", "api_service"),
Schema: getEnv("BLUEPRINT_DB_SCHEMA", "public"),
},
}
return config
}
func getEnv(key, defaultValue string) string {
if value := os.Getenv(key); value != "" {
return value
}
return defaultValue
}
func getEnvAsInt(key string, defaultValue int) int {
valueStr := getEnv(key, "")
if value, err := strconv.Atoi(valueStr); err == nil {
return value
}
return defaultValue
}
func (c *Config) Validate() error {
if c.Database.Host == "" {
log.Fatal("Database host is required")
}
if c.Database.Username == "" {
log.Fatal("Database username is required")
}
if c.Database.Password == "" {
log.Fatal("Database password is required")
}
if c.Database.Database == "" {
log.Fatal("Database name is required")
}
return nil
}

View File

@@ -0,0 +1,115 @@
package database
import (
"context"
"database/sql"
"fmt"
"log"
"os"
"strconv"
"time"
_ "github.com/jackc/pgx/v5/stdlib"
_ "github.com/joho/godotenv/autoload"
)
// Service represents a service that interacts with a database.
type Service interface {
// Health returns a map of health status information.
// The keys and values in the map are service-specific.
Health() map[string]string
// Close terminates the database connection.
// It returns an error if the connection cannot be closed.
Close() error
}
type service struct {
db *sql.DB
}
var (
database = os.Getenv("BLUEPRINT_DB_DATABASE")
password = os.Getenv("BLUEPRINT_DB_PASSWORD")
username = os.Getenv("BLUEPRINT_DB_USERNAME")
port = os.Getenv("BLUEPRINT_DB_PORT")
host = os.Getenv("BLUEPRINT_DB_HOST")
schema = os.Getenv("BLUEPRINT_DB_SCHEMA")
dbInstance *service
)
func New() Service {
// Reuse Connection
if dbInstance != nil {
return dbInstance
}
connStr := fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable&search_path=%s", username, password, host, port, database, schema)
db, err := sql.Open("pgx", connStr)
if err != nil {
log.Fatal(err)
}
dbInstance = &service{
db: db,
}
return dbInstance
}
// Health checks the health of the database connection by pinging the database.
// It returns a map with keys indicating various health statistics.
func (s *service) Health() map[string]string {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
stats := make(map[string]string)
// Ping the database
err := s.db.PingContext(ctx)
if err != nil {
stats["status"] = "down"
stats["error"] = fmt.Sprintf("db down: %v", err)
log.Fatalf("db down: %v", err) // Log the error and terminate the program
return stats
}
// Database is up, add more statistics
stats["status"] = "up"
stats["message"] = "It's healthy"
// Get database stats (like open connections, in use, idle, etc.)
dbStats := s.db.Stats()
stats["open_connections"] = strconv.Itoa(dbStats.OpenConnections)
stats["in_use"] = strconv.Itoa(dbStats.InUse)
stats["idle"] = strconv.Itoa(dbStats.Idle)
stats["wait_count"] = strconv.FormatInt(dbStats.WaitCount, 10)
stats["wait_duration"] = dbStats.WaitDuration.String()
stats["max_idle_closed"] = strconv.FormatInt(dbStats.MaxIdleClosed, 10)
stats["max_lifetime_closed"] = strconv.FormatInt(dbStats.MaxLifetimeClosed, 10)
// Evaluate stats to provide a health message
if dbStats.OpenConnections > 40 { // Assuming 50 is the max for this example
stats["message"] = "The database is experiencing heavy load."
}
if dbStats.WaitCount > 1000 {
stats["message"] = "The database has a high number of wait events, indicating potential bottlenecks."
}
if dbStats.MaxIdleClosed > int64(dbStats.OpenConnections)/2 {
stats["message"] = "Many idle connections are being closed, consider revising the connection pool settings."
}
if dbStats.MaxLifetimeClosed > int64(dbStats.OpenConnections)/2 {
stats["message"] = "Many connections are being closed due to max lifetime, consider increasing max lifetime or revising the connection usage pattern."
}
return stats
}
// Close closes the database connection.
// It logs a message indicating the disconnection from the specific database.
// If the connection is successfully closed, it returns nil.
// If an error occurs while closing the connection, it returns the error.
func (s *service) Close() error {
log.Printf("Disconnected from database: %s", database)
return s.db.Close()
}

View File

@@ -0,0 +1,100 @@
package database
import (
"context"
"log"
"testing"
"time"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/modules/postgres"
"github.com/testcontainers/testcontainers-go/wait"
)
func mustStartPostgresContainer() (func(context.Context, ...testcontainers.TerminateOption) error, error) {
var (
dbName = "database"
dbPwd = "password"
dbUser = "user"
)
dbContainer, err := postgres.Run(
context.Background(),
"postgres:latest",
postgres.WithDatabase(dbName),
postgres.WithUsername(dbUser),
postgres.WithPassword(dbPwd),
testcontainers.WithWaitStrategy(
wait.ForLog("database system is ready to accept connections").
WithOccurrence(2).
WithStartupTimeout(5*time.Second)),
)
if err != nil {
return nil, err
}
database = dbName
password = dbPwd
username = dbUser
dbHost, err := dbContainer.Host(context.Background())
if err != nil {
return dbContainer.Terminate, err
}
dbPort, err := dbContainer.MappedPort(context.Background(), "5432/tcp")
if err != nil {
return dbContainer.Terminate, err
}
host = dbHost
port = dbPort.Port()
return dbContainer.Terminate, err
}
func TestMain(m *testing.M) {
teardown, err := mustStartPostgresContainer()
if err != nil {
log.Fatalf("could not start postgres container: %v", err)
}
m.Run()
if teardown != nil && teardown(context.Background()) != nil {
log.Fatalf("could not teardown postgres container: %v", err)
}
}
func TestNew(t *testing.T) {
srv := New()
if srv == nil {
t.Fatal("New() returned nil")
}
}
func TestHealth(t *testing.T) {
srv := New()
stats := srv.Health()
if stats["status"] != "up" {
t.Fatalf("expected status to be up, got %s", stats["status"])
}
if _, ok := stats["error"]; ok {
t.Fatalf("expected error not to be present")
}
if stats["message"] != "It's healthy" {
t.Fatalf("expected message to be 'It's healthy', got %s", stats["message"])
}
}
func TestClose(t *testing.T) {
srv := New()
if srv.Close() != nil {
t.Fatalf("expected Close() to return nil")
}
}

View File

@@ -0,0 +1,57 @@
package handlers
import (
"net/http"
"api-service/internal/models"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)
// ExampleHandler handles example GET and POST services
type ExampleHandler struct{}
// NewExampleHandler creates a new ExampleHandler
func NewExampleHandler() *ExampleHandler {
return &ExampleHandler{}
}
// GetExample godoc
// @Summary Example GET service
// @Description Returns a simple message for GET request
// @Tags example
// @Accept json
// @Produce json
// @Success 200 {object} models.ExampleGetResponse "Example GET response"
// @Router /api/v1/example [get]
func (h *ExampleHandler) GetExample(c *gin.Context) {
response := models.ExampleGetResponse{
Message: "This is a GET example response",
}
c.JSON(http.StatusOK, response)
}
// PostExample godoc
// @Summary Example POST service
// @Description Accepts a JSON payload and returns a response with an ID
// @Tags example
// @Accept json
// @Produce json
// @Param request body models.ExamplePostRequest true "Example POST request"
// @Success 200 {object} models.ExamplePostResponse "Example POST response"
// @Failure 400 {object} models.ErrorResponse "Bad request"
// @Router /api/v1/example [post]
func (h *ExampleHandler) PostExample(c *gin.Context) {
var req models.ExamplePostRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
response := models.ExamplePostResponse{
ID: uuid.NewString(),
Message: "Received data for " + req.Name,
}
c.JSON(http.StatusOK, response)
}

View File

@@ -0,0 +1,55 @@
package handlers
import (
"net/http"
"time"
"api-service/internal/models"
"github.com/gin-gonic/gin"
)
// HealthHandler handles health check endpoints
type HealthHandler struct{}
// NewHealthHandler creates a new HealthHandler
func NewHealthHandler() *HealthHandler {
return &HealthHandler{}
}
// GetHealth godoc
// @Summary Health check endpoint
// @Description Returns the health status of the API service
// @Tags health
// @Accept json
// @Produce json
// @Success 200 {object} models.HealthResponse "Health status"
// @Failure 500 {object} models.ErrorResponse "Internal server error"
// @Router /health [get]
func (h *HealthHandler) GetHealth(c *gin.Context) {
health := models.HealthResponse{
Status: "healthy",
Timestamp: time.Now().Format(time.RFC3339),
Details: map[string]string{
"service": "api-service",
"version": "1.0.0",
},
}
c.JSON(http.StatusOK, health)
}
// HelloWorld godoc
// @Summary Hello World endpoint
// @Description Returns a hello world message
// @Tags root
// @Accept json
// @Produce json
// @Success 200 {object} models.HelloWorldResponse "Hello world message"
// @Router / [get]
func (h *HealthHandler) HelloWorld(c *gin.Context) {
response := models.HelloWorldResponse{
Message: "Hello World",
Version: "1.0.0",
}
c.JSON(http.StatusOK, response)
}

View File

@@ -0,0 +1,55 @@
package middleware
import (
"net/http"
"api-service/internal/models"
"github.com/gin-gonic/gin"
)
// ErrorHandler handles errors globally
func ErrorHandler() gin.HandlerFunc {
return func(c *gin.Context) {
c.Next()
if len(c.Errors) > 0 {
err := c.Errors.Last()
status := http.StatusInternalServerError
// Determine status code based on error type
switch err.Type {
case gin.ErrorTypeBind:
status = http.StatusBadRequest
case gin.ErrorTypeRender:
status = http.StatusUnprocessableEntity
case gin.ErrorTypePrivate:
status = http.StatusInternalServerError
}
response := models.ErrorResponse{
Error: "internal_error",
Message: err.Error(),
Code: status,
}
c.JSON(status, response)
}
}
}
// CORS middleware configuration
func CORSConfig() gin.HandlerFunc {
return gin.HandlerFunc(func(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", "*")
c.Header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, PATCH")
c.Header("Access-Control-Allow-Headers", "Origin, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
if c.Request.Method == "OPTIONS" {
c.AbortWithStatus(204)
return
}
c.Next()
})
}

View File

@@ -0,0 +1,18 @@
package models
// ExampleGetResponse represents the response for the GET example service
type ExampleGetResponse struct {
Message string `json:"message"`
}
// ExamplePostRequest represents the request body for the POST example service
type ExamplePostRequest struct {
Name string `json:"name" binding:"required"`
Age int `json:"age" binding:"required"`
}
// ExamplePostResponse represents the response for the POST example service
type ExamplePostResponse struct {
ID string `json:"id"`
Message string `json:"message"`
}

42
internal/models/health.go Normal file
View File

@@ -0,0 +1,42 @@
package models
// HealthResponse represents the health check response
type HealthResponse struct {
Status string `json:"status"`
Timestamp string `json:"timestamp"`
Details map[string]string `json:"details"`
}
// HelloWorldResponse represents the hello world response
type HelloWorldResponse struct {
Message string `json:"message"`
Version string `json:"version"`
}
// ErrorResponse represents an error response
type ErrorResponse struct {
Error string `json:"error"`
Message string `json:"message"`
Code int `json:"code"`
}
// SuccessResponse represents a generic success response
type SuccessResponse struct {
Success bool `json:"success"`
Message string `json:"message"`
Data interface{} `json:"data,omitempty"`
}
// Pagination represents pagination metadata
type Pagination struct {
Page int `json:"page"`
Limit int `json:"limit"`
Total int `json:"total"`
TotalPages int `json:"total_pages"`
}
// PaginatedResponse represents a paginated response
type PaginatedResponse struct {
Data interface{} `json:"data"`
Pagination Pagination `json:"pagination"`
}

View File

@@ -0,0 +1,51 @@
package v1
import (
"net/http"
"api-service/internal/handlers"
"api-service/internal/middleware"
"github.com/gin-gonic/gin"
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
)
// RegisterRoutes registers all API routes for version 1
func RegisterRoutes() *gin.Engine {
router := gin.New()
// Add middleware
router.Use(middleware.CORSConfig())
router.Use(middleware.ErrorHandler())
router.Use(gin.Logger())
router.Use(gin.Recovery())
// Swagger UI route
router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
// API v1 group
v1 := router.Group("/api/v1")
{
// Health endpoints
healthHandler := handlers.NewHealthHandler()
v1.GET("/health", healthHandler.GetHealth)
v1.GET("/", healthHandler.HelloWorld)
// Example endpoints
exampleHandler := handlers.NewExampleHandler()
v1.GET("/example", exampleHandler.GetExample)
v1.POST("/example", exampleHandler.PostExample)
// WebSocket endpoint
v1.GET("/websocket", WebSocketHandler)
}
return router
}
// WebSocketHandler handles WebSocket connections
func WebSocketHandler(c *gin.Context) {
// This will be implemented with proper WebSocket handling
c.JSON(http.StatusOK, gin.H{"message": "WebSocket endpoint"})
}

71
internal/server/routes.go Normal file
View File

@@ -0,0 +1,71 @@
package server
import (
"net/http"
"fmt"
"log"
"time"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/coder/websocket"
)
func (s *Server) RegisterRoutes() http.Handler {
r := gin.Default()
r.Use(cors.New(cors.Config{
AllowOrigins: []string{"http://localhost:5173"}, // Add your frontend URL
AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH"},
AllowHeaders: []string{"Accept", "Authorization", "Content-Type"},
AllowCredentials: true, // Enable cookies/auth
}))
r.GET("/", s.HelloWorldHandler)
r.GET("/health", s.healthHandler)
r.GET("/websocket", s.websocketHandler)
return r
}
func (s *Server) HelloWorldHandler(c *gin.Context) {
resp := make(map[string]string)
resp["message"] = "Hello World"
c.JSON(http.StatusOK, resp)
}
func (s *Server) healthHandler(c *gin.Context) {
c.JSON(http.StatusOK, s.db.Health())
}
func (s *Server) websocketHandler(c *gin.Context) {
w := c.Writer
r := c.Request
socket, err := websocket.Accept(w, r, nil)
if err != nil {
log.Printf("could not open websocket: %v", err)
_, _ = w.Write([]byte("could not open websocket"))
w.WriteHeader(http.StatusInternalServerError)
return
}
defer socket.Close(websocket.StatusGoingAway, "server closing websocket")
ctx := r.Context()
socketCtx := socket.CloseRead(ctx)
for {
payload := fmt.Sprintf("server timestamp: %d", time.Now().UnixNano())
err := socket.Write(socketCtx, websocket.MessageText, []byte(payload))
if err != nil {
break
}
time.Sleep(time.Second * 2)
}
}

View File

@@ -0,0 +1,32 @@
package server
import (
"github.com/gin-gonic/gin"
"net/http"
"net/http/httptest"
"testing"
)
func TestHelloWorldHandler(t *testing.T) {
s := &Server{}
r := gin.New()
r.GET("/", s.HelloWorldHandler)
// Create a test HTTP request
req, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Fatal(err)
}
// Create a ResponseRecorder to record the response
rr := httptest.NewRecorder()
// Serve the HTTP request
r.ServeHTTP(rr, req)
// Check the status code
if status := rr.Code; status != http.StatusOK {
t.Errorf("Handler returned wrong status code: got %v want %v", status, http.StatusOK)
}
// Check the response body
expected := "{\"message\":\"Hello World\"}"
if rr.Body.String() != expected {
t.Errorf("Handler returned unexpected body: got %v want %v", rr.Body.String(), expected)
}
}

47
internal/server/server.go Normal file
View File

@@ -0,0 +1,47 @@
package server
import (
"fmt"
"net/http"
"os"
"strconv"
"time"
_ "github.com/joho/godotenv/autoload"
"api-service/internal/config"
"api-service/internal/database"
v1 "api-service/internal/routes/v1"
)
type Server struct {
port int
db database.Service
}
func NewServer() *http.Server {
// Load configuration
cfg := config.LoadConfig()
cfg.Validate()
port, _ := strconv.Atoi(os.Getenv("PORT"))
if port == 0 {
port = cfg.Server.Port
}
NewServer := &Server{
port: port,
db: database.New(),
}
// Declare Server config
server := &http.Server{
Addr: fmt.Sprintf(":%d", NewServer.port),
Handler: v1.RegisterRoutes(),
IdleTimeout: time.Minute,
ReadTimeout: 10 * time.Second,
WriteTimeout: 30 * time.Second,
}
return server
}