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

This commit is contained in:
kever
2026-01-15 21:39:15 +08:00
parent fed727e593
commit ed8c3d55b8
103 changed files with 39974 additions and 80 deletions

View File

@@ -1,22 +0,0 @@
package controller
import (
"net/http"
"github.com/labstack/echo/v4"
"epic-ent/internal/service"
)
type HealthController struct {
svc *service.HealthService
}
func NewHealthController(svc *service.HealthService) *HealthController {
return &HealthController{svc: svc}
}
func (h *HealthController) Health(c echo.Context) error {
status := h.svc.Check()
return c.JSON(http.StatusOK, status)
}

View File

@@ -0,0 +1,94 @@
package controller
import (
"database/sql"
"net/http"
"strconv"
"github.com/labstack/echo/v4"
"epic-ent/internal/domain/dto"
"epic-ent/internal/domain/vo"
"epic-ent/internal/service"
)
type HeroController struct {
svc *service.HeroService
}
func NewHeroController(svc *service.HeroService) *HeroController {
return &HeroController{svc: svc}
}
func (h *HeroController) Create(c echo.Context) error {
var req dto.HeroCreateRequest
if err := c.Bind(&req); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "invalid request")
}
hero, err := h.svc.Create(c.Request().Context(), req)
if err != nil {
return err
}
return c.JSON(http.StatusCreated, vo.OK(hero))
}
func (h *HeroController) GetByID(c echo.Context) error {
id, err := parseID(c.Param("id"))
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "invalid id")
}
hero, err := h.svc.GetByID(c.Request().Context(), id)
if err != nil {
if err == sql.ErrNoRows {
return echo.NewHTTPError(http.StatusNotFound, "hero not found")
}
return err
}
return c.JSON(http.StatusOK, vo.OK(hero))
}
func (h *HeroController) Update(c echo.Context) error {
id, err := parseID(c.Param("id"))
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "invalid id")
}
var req dto.HeroUpdateRequest
if err := c.Bind(&req); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "invalid request")
}
hero, err := h.svc.Update(c.Request().Context(), id, req)
if err != nil {
if err == sql.ErrNoRows {
return echo.NewHTTPError(http.StatusNotFound, "hero not found")
}
return err
}
return c.JSON(http.StatusOK, vo.OK(hero))
}
func (h *HeroController) Delete(c echo.Context) error {
id, err := parseID(c.Param("id"))
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "invalid id")
}
if err := h.svc.Delete(c.Request().Context(), id); err != nil {
if err == sql.ErrNoRows {
return echo.NewHTTPError(http.StatusNotFound, "hero not found")
}
return err
}
return c.JSON(http.StatusOK, vo.OK(nil))
}
func parseID(raw string) (int64, error) {
return strconv.ParseInt(raw, 10, 64)
}

View File

@@ -5,7 +5,7 @@ import "go.uber.org/fx"
var Module = fx.Options(
fx.Provide(
NewHealthController,
NewHeroController,
),
fx.Invoke(
RegisterRoutes,

View File

@@ -8,10 +8,13 @@ import (
type RouteParams struct {
fx.In
Echo *echo.Echo
Health *HealthController
Echo *echo.Echo
Hero *HeroController
}
func RegisterRoutes(p RouteParams) {
p.Echo.GET("/health", p.Health.Health)
p.Echo.POST("/heroes", p.Hero.Create)
p.Echo.GET("/heroes/:id", p.Hero.GetByID)
p.Echo.PUT("/heroes/:id", p.Hero.Update)
p.Echo.DELETE("/heroes/:id", p.Hero.Delete)
}

View File

@@ -1,22 +0,0 @@
package controller
import (
"net/http"
"github.com/labstack/echo/v4"
"epic-ent/internal/service"
)
type UserController struct {
svc *service.HealthService
}
func NewUserController(svc *service.HealthService) *HealthController {
return &HealthController{svc: svc}
}
func (h *UserController) Health(c echo.Context) error {
//status := h.svc.Check()
return c.JSON(http.StatusOK, "你好")
}