[release] release v1
This commit is contained in:
parent
39ed417398
commit
90a0f562ea
@ -2,7 +2,6 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.pbiernat.dev/golang/rest-api-prototype/internal/app/database"
|
||||
"net"
|
||||
"os"
|
||||
"os/signal"
|
||||
@ -10,6 +9,7 @@ import (
|
||||
|
||||
"git.pbiernat.dev/golang/rest-api-prototype/internal/app"
|
||||
"git.pbiernat.dev/golang/rest-api-prototype/internal/app/config"
|
||||
"git.pbiernat.dev/golang/rest-api-prototype/internal/app/database"
|
||||
"git.pbiernat.dev/golang/rest-api-prototype/internal/app/handler"
|
||||
)
|
||||
|
||||
@ -26,6 +26,8 @@ func main() {
|
||||
|
||||
httpAddr := net.JoinHostPort(config.GetEnv("SERVER_IP", defHttpIp), config.GetEnv("SERVER_PORT", defHttpPort))
|
||||
dbConnStr := config.GetEnv("DATABASE_URL", defDbUrl)
|
||||
//fmt.Println(dbConnStr)
|
||||
//os.Exit(1)
|
||||
|
||||
dbc, err := database.Connect(dbConnStr)
|
||||
if err != nil {
|
||||
|
@ -1,16 +0,0 @@
|
||||
package definition
|
||||
|
||||
import "git.pbiernat.dev/golang/rest-api-prototype/internal/app/entity"
|
||||
|
||||
type CreateArticleRequest struct {
|
||||
CategoryID int `json:"category_id"`
|
||||
Title string `json:"title"`
|
||||
Intro string `json:"intro"`
|
||||
Text string `json:"text"`
|
||||
}
|
||||
|
||||
type CreateArticleResponse struct {
|
||||
Status string `json:"status"`
|
||||
Data *entity.Article `json:"data"`
|
||||
Err string `json:"err,omitempty"`
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
package definition
|
||||
|
||||
import (
|
||||
"git.pbiernat.dev/golang/rest-api-prototype/internal/app/entity"
|
||||
validation "github.com/go-ozzo/ozzo-validation"
|
||||
)
|
||||
|
||||
type CreateCategoryRequest struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
func (c CreateCategoryRequest) Validate() error {
|
||||
return validation.ValidateStruct(&c,
|
||||
validation.Field(&c.Name, validation.Required, validation.Length(3, 255)),
|
||||
)
|
||||
}
|
||||
|
||||
type CreateCategoryResponse struct {
|
||||
Data *entity.Category `json:"data"`
|
||||
Err string `json:"err,omitempty"` // FIXME: omitempty on/off?
|
||||
}
|
||||
|
||||
type DeleteCategoryRequest struct {
|
||||
}
|
||||
|
||||
type DeleteCategoryResponse struct {
|
||||
}
|
@ -1,15 +0,0 @@
|
||||
package entity
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type Article struct {
|
||||
ID int `json:"id"`
|
||||
CategoryID int `json:"category_id"`
|
||||
Title string `json:"title"`
|
||||
Intro string `json:"intro"`
|
||||
Text string `json:"text"`
|
||||
CreateDate time.Time `json:"create_date"`
|
||||
ModifyDate time.Time `json:"modify_date"`
|
||||
}
|
@ -1,18 +0,0 @@
|
||||
package entity
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type Category struct {
|
||||
ID int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
CreateDate time.Time `json:"create_date"`
|
||||
ModifyDate time.Time `json:"modify_date"` // FIXME: zero-value issue
|
||||
}
|
||||
|
||||
// func (c Category) Validate() error {
|
||||
// return validation.ValidateStruct(&c,
|
||||
// validation.Field(&c.Name, validation.Required, validation.Length(3, 255)),
|
||||
// )
|
||||
// }
|
@ -1,34 +0,0 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
def "git.pbiernat.dev/golang/rest-api-prototype/internal/app/definition"
|
||||
"git.pbiernat.dev/golang/rest-api-prototype/internal/app/entity"
|
||||
)
|
||||
|
||||
var CreateArticleHandler *Handler
|
||||
|
||||
func init() {
|
||||
CreateArticleHandler = &Handler{
|
||||
Handle: CreateArticleHandlerFunc,
|
||||
Request: &def.CreateArticleRequest{},
|
||||
Response: &def.CreateArticleResponse{},
|
||||
}
|
||||
}
|
||||
|
||||
func CreateArticleHandlerFunc(h *Handler, w http.ResponseWriter) (interface{}, int, error) {
|
||||
var art = h.Request.(*def.CreateArticleRequest)
|
||||
log.Println(art)
|
||||
|
||||
return &entity.Article{
|
||||
ID: 1,
|
||||
CategoryID: 1,
|
||||
Title: "Dummy article",
|
||||
Intro: "Intro",
|
||||
Text: "Text",
|
||||
CreateDate: time.Now(),
|
||||
}, http.StatusCreated, nil
|
||||
}
|
@ -1,57 +0,0 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
def "git.pbiernat.dev/golang/rest-api-prototype/internal/app/definition"
|
||||
"git.pbiernat.dev/golang/rest-api-prototype/internal/app/entity"
|
||||
)
|
||||
|
||||
var CreateCategoryHandler *Handler
|
||||
var DeleteCategoryHandler *Handler
|
||||
|
||||
func init() {
|
||||
CreateCategoryHandler = &Handler{
|
||||
Handle: CreateCategoryHandlerFunc,
|
||||
Request: &def.CreateCategoryRequest{},
|
||||
Response: &def.CreateCategoryResponse{},
|
||||
}
|
||||
|
||||
DeleteCategoryHandler = &Handler{
|
||||
Handle: DeleteCategoryHandlerFunc,
|
||||
Request: &def.DeleteCategoryRequest{},
|
||||
Response: &def.DeleteCategoryResponse{},
|
||||
}
|
||||
}
|
||||
|
||||
func CreateCategoryHandlerFunc(h *Handler, w http.ResponseWriter) (interface{}, int, error) {
|
||||
var cat = h.Request.(*def.CreateCategoryRequest)
|
||||
log.Println("Cat input:", cat)
|
||||
|
||||
if err := cat.Validate(); err != nil {
|
||||
log.Println("Create category validation errors:", err)
|
||||
return nil, http.StatusUnprocessableEntity, err
|
||||
}
|
||||
|
||||
return &entity.Category{
|
||||
Name: cat.Name,
|
||||
CreateDate: time.Now(),
|
||||
}, http.StatusCreated, nil
|
||||
}
|
||||
|
||||
func DeleteCategoryHandlerFunc(h *Handler, w http.ResponseWriter) (interface{}, int, error) {
|
||||
var cat = h.Request.(*def.DeleteCategoryRequest)
|
||||
log.Println(cat)
|
||||
|
||||
id, _ := strconv.Atoi(h.Params["id"])
|
||||
log.Println(h.Params)
|
||||
|
||||
if id != 1 {
|
||||
return nil, http.StatusNotFound, nil
|
||||
}
|
||||
|
||||
return nil, http.StatusNoContent, nil
|
||||
}
|
@ -4,7 +4,6 @@ import (
|
||||
"net/http"
|
||||
|
||||
"git.pbiernat.dev/golang/rest-api-prototype/internal/app/handler"
|
||||
"git.pbiernat.dev/golang/rest-api-prototype/internal/app/service"
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
@ -23,13 +22,5 @@ func SetupRouter(env *handler.Env) *mux.Router {
|
||||
auth := r.PathPrefix("/auth").Subrouter()
|
||||
auth.Handle("/login", handler.Init(env, handler.AuthLoginHandler)).Methods(http.MethodPost)
|
||||
|
||||
api := r.PathPrefix("/api").Subrouter()
|
||||
api.Use(service.AuthService.ValidateUserTokenMiddleware) // only /api/** endpoints use this middleware
|
||||
|
||||
api.Handle("/article", handler.Init(env, handler.CreateArticleHandler)).Methods(http.MethodPost)
|
||||
|
||||
api.Handle("/category", handler.Init(env, handler.CreateCategoryHandler)).Methods(http.MethodPost)
|
||||
api.Handle("/category/{id:[0-9]+}", handler.Init(env, handler.DeleteCategoryHandler)).Methods(http.MethodDelete)
|
||||
|
||||
return r
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user