go-cook/main.go

53 lines
1.1 KiB
Go
Raw Normal View History

package main
import (
"database/sql"
v1 "go-cook/api/handlers/v1"
2024-03-31 17:47:09 -07:00
"go-cook/api/services"
"log"
"net/http"
_ "github.com/glebarez/go-sqlite"
"github.com/go-playground/validator"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
// @title Swagger Example API
// @version 1.0
// @description This is a sample server Petstore server.
func main() {
db, err := sql.Open("sqlite", "gocook.db")
if err != nil {
log.Fatal(err)
}
2024-03-31 17:47:09 -07:00
cfg := services.NewEnvConfig()
e := echo.New()
e.Validator = &CustomValidator{
validator: validator.New(),
}
e.Pre(middleware.RemoveTrailingSlash())
e.Pre(middleware.Logger())
2024-03-26 17:49:01 -07:00
e.Pre(middleware.Recover())
v1Group := e.Group("/api/v1")
2024-03-31 17:47:09 -07:00
v1Api := v1.NewHandler(db, cfg)
v1Api.Register(v1Group)
e.Logger.Fatal(e.Start(":1323"))
}
type CustomValidator struct {
validator *validator.Validate
}
func (cv *CustomValidator) Validate(i interface{}) error {
if err := cv.validator.Struct(i); err != nil {
// Optionally, you could return the error to give each route more control over the status code
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
}
return nil
}