mirror of
https://github.com/zoriya/vex.git
synced 2026-08-16 02:44:55 +00:00
Create AddFeed dto, add validation and sqlx
This commit is contained in:
+20
-4
@@ -1,18 +1,21 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/echo/v4/middleware"
|
||||
_ "github.com/lib/pq"
|
||||
"github.com/zoriya/vex"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
database *sql.DB
|
||||
feeds vex.FeedService
|
||||
}
|
||||
|
||||
func (h *Handler) GetEntries(c echo.Context) error {
|
||||
@@ -20,6 +23,18 @@ func (h *Handler) GetEntries(c echo.Context) error {
|
||||
return c.JSON(200, ret)
|
||||
}
|
||||
|
||||
type Validator struct {
|
||||
validator *validator.Validate
|
||||
}
|
||||
|
||||
func (cv *Validator) 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
|
||||
}
|
||||
|
||||
func main() {
|
||||
con := fmt.Sprintf(
|
||||
"postgresql://%v:%v@%v:%v/%v?sslmode=disable",
|
||||
@@ -29,15 +44,16 @@ func main() {
|
||||
os.Getenv("POSTGRES_PORT"),
|
||||
os.Getenv("POSTGRES_DB"),
|
||||
)
|
||||
db, err := sql.Open("postgres", con)
|
||||
db, err := sqlx.Open("postgres", con)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
h := Handler{
|
||||
database: db,
|
||||
feeds: vex.NewFeedService(db),
|
||||
}
|
||||
|
||||
e := echo.New()
|
||||
e.Validator = &Validator{validator: validator.New()}
|
||||
e.Use(middleware.Logger())
|
||||
e.GET("/entries", h.GetEntries)
|
||||
h.RegisterFeedsRoutes(e)
|
||||
|
||||
Reference in New Issue
Block a user