add initial application structure with configuration, logging, and health check endpoints

This commit is contained in:
kever
2026-01-14 23:58:00 +08:00
commit fed727e593
31 changed files with 770 additions and 0 deletions

17
internal/infra/cache/redis.go vendored Normal file
View File

@@ -0,0 +1,17 @@
package cache
import (
"github.com/redis/go-redis/v9"
"epic-ent/internal/config"
)
func NewRedis(cfg *config.Config) (*redis.Client, error) {
rdb := redis.NewClient(&redis.Options{
Addr: cfg.Redis.Addr,
Password: cfg.Redis.Password,
DB: cfg.Redis.DB,
})
return rdb, nil
}

View File

@@ -0,0 +1,39 @@
package cron
import (
"context"
"time"
"github.com/robfig/cron/v3"
"go.uber.org/fx"
"go.uber.org/zap"
"epic-ent/internal/config"
)
func RegisterJobs(lc fx.Lifecycle, cfg *config.Config, logger *zap.Logger) {
if !cfg.Cron.Enabled {
return
}
loc, err := time.LoadLocation(cfg.Cron.Timezone)
if err != nil {
logger.Warn("invalid timezone, fallback to Local", zap.Error(err))
loc = time.Local
}
c := cron.New(cron.WithLocation(loc))
lc.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
c.Start()
logger.Info("cron started")
return nil
},
OnStop: func(ctx context.Context) error {
c.Stop()
logger.Info("cron stopped")
return nil
},
})
}

View File

@@ -0,0 +1,18 @@
package db
import (
"database/sql"
_ "github.com/go-sql-driver/mysql"
"epic-ent/internal/config"
)
func NewMySQL(cfg *config.Config) (*sql.DB, error) {
db, err := sql.Open("mysql", cfg.MySQL.DSN)
if err != nil {
return nil, err
}
return db, nil
}

View File

@@ -0,0 +1,49 @@
package http
import (
"context"
"fmt"
"net/http"
"time"
"github.com/labstack/echo/v4"
"go.uber.org/fx"
"go.uber.org/zap"
"epic-ent/internal/config"
)
func NewEcho() *echo.Echo {
return echo.New()
}
func StartServer(lc fx.Lifecycle, cfg *config.Config, e *echo.Echo, logger *zap.Logger) {
addr := fmt.Sprintf(":%d", cfg.Server.Port)
if cfg.Server.ReadTimeout != "" {
if d, err := time.ParseDuration(cfg.Server.ReadTimeout); err == nil {
e.Server.ReadTimeout = d
}
}
if cfg.Server.WriteTimeout != "" {
if d, err := time.ParseDuration(cfg.Server.WriteTimeout); err == nil {
e.Server.WriteTimeout = d
}
}
lc.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
logger.Info("http server starting", zap.String("addr", addr))
go func() {
if err := e.Start(addr); err != nil && err != http.ErrServerClosed {
logger.Error("http server stopped", zap.Error(err))
}
}()
return nil
},
OnStop: func(ctx context.Context) error {
logger.Info("http server stopping")
return e.Shutdown(ctx)
},
})
}

View File

@@ -0,0 +1,23 @@
package log
import (
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"epic-ent/internal/config"
)
func NewLogger(cfg *config.Config) (*zap.Logger, error) {
level := zapcore.InfoLevel
if err := level.Set(cfg.Log.Level); err != nil {
level = zapcore.InfoLevel
}
zapCfg := zap.NewProductionConfig()
zapCfg.Level = zap.NewAtomicLevelAt(level)
if cfg.Log.Format == "console" {
zapCfg.Encoding = "console"
}
return zapCfg.Build()
}

24
internal/infra/module.go Normal file
View File

@@ -0,0 +1,24 @@
package infra
import (
"go.uber.org/fx"
"epic-ent/internal/infra/cache"
"epic-ent/internal/infra/cron"
"epic-ent/internal/infra/db"
"epic-ent/internal/infra/http"
infraLog "epic-ent/internal/infra/log"
)
var Module = fx.Options(
fx.Provide(
infraLog.NewLogger,
db.NewMySQL,
cache.NewRedis,
http.NewEcho,
),
fx.Invoke(
cron.RegisterJobs,
http.StartServer,
),
)