Added migrations on startup
This commit is contained in:
parent
3acf9ac542
commit
fbd799c8ed
26
main.go
26
main.go
@ -2,15 +2,17 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
v1 "go-cook/api/handlers/v1"
|
|
||||||
"go-cook/api/services"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"git.jamestombleson.com/jtom38/go-cook/api/services"
|
||||||
|
v1 "git.jamestombleson.com/jtom38/go-cook/api/handlers/v1"
|
||||||
|
|
||||||
_ "github.com/glebarez/go-sqlite"
|
_ "github.com/glebarez/go-sqlite"
|
||||||
"github.com/go-playground/validator"
|
"github.com/go-playground/validator"
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
"github.com/labstack/echo/v4/middleware"
|
"github.com/labstack/echo/v4/middleware"
|
||||||
|
"github.com/pressly/goose/v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
// @title Swagger Example API
|
// @title Swagger Example API
|
||||||
@ -24,6 +26,20 @@ func main() {
|
|||||||
|
|
||||||
cfg := services.NewEnvConfig()
|
cfg := services.NewEnvConfig()
|
||||||
|
|
||||||
|
// Check if we should apply migrations on startup
|
||||||
|
// You can disable this in the ENV configuration of your application.
|
||||||
|
if !cfg.DisableMigrationsOnStartUp {
|
||||||
|
err = goose.SetDialect("sqlite3")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = goose.Up(db, "api/migrations")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
e := echo.New()
|
e := echo.New()
|
||||||
e.Validator = &CustomValidator{
|
e.Validator = &CustomValidator{
|
||||||
validator: validator.New(),
|
validator: validator.New(),
|
||||||
@ -45,8 +61,8 @@ type CustomValidator struct {
|
|||||||
|
|
||||||
func (cv *CustomValidator) Validate(i interface{}) error {
|
func (cv *CustomValidator) Validate(i interface{}) error {
|
||||||
if err := cv.validator.Struct(i); err != nil {
|
if err := cv.validator.Struct(i); err != nil {
|
||||||
// Optionally, you could return the error to give each route more control over the status code
|
// Optionally, you could return the error to give each route more control over the status code
|
||||||
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
|
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user