go-cook/main.go

49 lines
1.0 KiB
Go
Raw Normal View History

package main
import (
"database/sql"
v1 "go-cook/api/handlers/v1"
"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)
}
e := echo.New()
e.Validator = &CustomValidator{
validator: validator.New(),
}
e.Pre(middleware.RemoveTrailingSlash())
e.Pre(middleware.Logger())
v1Group := e.Group("/api/v1")
v1Api := v1.NewHandler(db)
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
}