2024-03-20 17:54:23 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
v1 "go-cook/api/handlers/v1"
|
|
|
|
"log"
|
2024-03-23 08:26:49 -07:00
|
|
|
"net/http"
|
2024-03-20 17:54:23 -07:00
|
|
|
|
|
|
|
_ "github.com/glebarez/go-sqlite"
|
2024-03-23 08:26:49 -07:00
|
|
|
"github.com/go-playground/validator"
|
2024-03-20 17:54:23 -07:00
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
"github.com/labstack/echo/v4/middleware"
|
|
|
|
)
|
|
|
|
|
2024-03-23 08:26:49 -07:00
|
|
|
// @title Swagger Example API
|
|
|
|
// @version 1.0
|
|
|
|
// @description This is a sample server Petstore server.
|
2024-03-20 17:54:23 -07:00
|
|
|
func main() {
|
|
|
|
db, err := sql.Open("sqlite", "gocook.db")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
e := echo.New()
|
2024-03-23 08:26:49 -07:00
|
|
|
e.Validator = &CustomValidator{
|
|
|
|
validator: validator.New(),
|
|
|
|
}
|
2024-03-20 17:54:23 -07:00
|
|
|
e.Pre(middleware.RemoveTrailingSlash())
|
|
|
|
e.Pre(middleware.Logger())
|
2024-03-26 17:49:01 -07:00
|
|
|
e.Pre(middleware.Recover())
|
2024-03-20 17:54:23 -07:00
|
|
|
|
|
|
|
v1Group := e.Group("/api/v1")
|
|
|
|
v1Api := v1.NewHandler(db)
|
|
|
|
v1Api.Register(v1Group)
|
|
|
|
|
|
|
|
e.Logger.Fatal(e.Start(":1323"))
|
|
|
|
}
|
2024-03-23 08:26:49 -07:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|