👻
Goの雛形をBlueprintで作成してみる
はじめに
Railsをやっているとrails new app_name
で簡単に雛形を作成できてしまいます。
Goでも同じようなことができないかなと思っていたので、探してみるとgo-blueprintがあったので作成してみることにしました
実行してみる
- まずgo-blueprintをインストールする
$ go install github.com/melkeydev/go-blueprint@latest
- createコマンド
$ go-blueprint create
createコマンド後に以下のような画像が表示されます。
プロジェクト名を求められるのでsample_app
を指定します
今回はGinを選択しました
[f:id:yoshioka0101:20250227213918p:plain]
Ginを選択した後に、databaseの選択を要求されます
Postgresを選択する
[f:id:yoshioka0101:20250227213939p:plain]
ここまでで基本的なファイルなどが作成されます
Makefile
も自動的に作成されるので、make run
で起動することができます
$ make run
[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.
[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
- using env: export GIN_MODE=release
- using code: gin.SetMode(gin.ReleaseMode)
[GIN-debug] GET / --> sample_app/internal/server.(*Server).HelloWorldHandler-fm (4 handlers)
[GIN-debug] GET /health --> sample_app/internal/server.(*Server).healthHandler-fm (4 handlers)
おまけ:具体的な実装
- sample_app/cmd/api/main.go
package main
import (
"context"
"fmt"
"log"
"net/http"
"os/signal"
"syscall"
"time"
"sample_app/internal/server"
)
func gracefulShutdown(apiServer *http.Server, done chan bool) {
// Create context that listens for the interrupt signal from the OS.
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()
// Listen for the interrupt signal.
<-ctx.Done()
log.Println("shutting down gracefully, press Ctrl+C again to force")
// The context is used to inform the server it has 5 seconds to finish
// the request it is currently handling
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := apiServer.Shutdown(ctx); err != nil {
log.Printf("Server forced to shutdown with error: %v", err)
}
log.Println("Server exiting")
// Notify the main goroutine that the shutdown is complete
done <- true
}
func main() {
server := server.NewServer()
// Create a done channel to signal when the shutdown is complete
done := make(chan bool, 1)
// Run graceful shutdown in a separate goroutine
go gracefulShutdown(server, done)
err := server.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
panic(fmt.Sprintf("http server error: %s", err))
}
// Wait for the graceful shutdown to complete
<-done
log.Println("Graceful shutdown complete.")
}
- sample_app/internal/database/database.go
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()
}
- sample_app/internal/server/routes.go
package server
import (
"net/http"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
)
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)
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())
}
- docker-compose.yml
services:
psql_bp:
image: postgres:latest
restart: unless-stopped
environment:
POSTGRES_DB: ${BLUEPRINT_DB_DATABASE}
POSTGRES_USER: ${BLUEPRINT_DB_USERNAME}
POSTGRES_PASSWORD: ${BLUEPRINT_DB_PASSWORD}
ports:
- "${BLUEPRINT_DB_PORT}:5432"
volumes:
- psql_volume_bp:/var/lib/postgresql/data
volumes:
psql_volume_bp:
これでdockerを立ち上げれば完成です
Discussion