Merge 'feature/dev-prepare-for-release_1' (#1) from feature/dev-prepare-for-release_1 into develop
Reviewed-on: https://git.pbiernat.dev/golang/rest-api-prototype/pulls/1
This commit is contained in:
commit
752a058ac3
@ -2,7 +2,6 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
"os/signal"
|
||||
@ -10,30 +9,32 @@ 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"
|
||||
)
|
||||
|
||||
const (
|
||||
defHttpIp = "127.0.0.1"
|
||||
defHttpPort = "8080"
|
||||
defDbUrl = "postgres://postgres:postgres@127.0.0.1:5432/Api" // FIXME use default container conf in future
|
||||
defDbUrl = "postgres://postgres:postgres@127.0.0.1:5432/Api" // FIXME: use env
|
||||
)
|
||||
|
||||
func main() {
|
||||
if config.ErrLoadingEnvs != nil {
|
||||
log.Fatalln("Error loading .env file")
|
||||
app.Panicf("Error loading .env file")
|
||||
}
|
||||
|
||||
httpAddr := net.JoinHostPort(config.GetEnv("SERVER_IP", defHttpIp), config.GetEnv("SERVER_PORT", defHttpPort))
|
||||
// dbConnStr := config.GetEnv("DATABASE_URL", defDbUrl)
|
||||
dbConnStr := config.GetEnv("DATABASE_URL", defDbUrl)
|
||||
//fmt.Println(dbConnStr)
|
||||
//os.Exit(1)
|
||||
|
||||
// dbc, err := database.Connect(dbConnStr)
|
||||
// if err != nil {
|
||||
// log.Panicf("Unable to connect to database: %v\n", err)
|
||||
// }
|
||||
dbc, err := database.Connect(dbConnStr)
|
||||
if err != nil {
|
||||
app.Panicf("Unable to connect to database: %v\n", err)
|
||||
}
|
||||
|
||||
// env := &handler.Env{httpAddr, dbc}
|
||||
env := &handler.Env{httpAddr, nil}
|
||||
env := &handler.Env{httpAddr, dbc}
|
||||
srv := app.NewServer(env)
|
||||
|
||||
go srv.Start()
|
||||
|
2
go.mod
2
go.mod
@ -1,6 +1,6 @@
|
||||
module git.pbiernat.dev/golang/rest-api-prototype
|
||||
|
||||
go 1.17
|
||||
go 1.18
|
||||
|
||||
require (
|
||||
github.com/go-ozzo/ozzo-validation v3.6.0+incompatible
|
||||
|
@ -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
|
||||
}
|
16
internal/app/log.go
Normal file
16
internal/app/log.go
Normal file
@ -0,0 +1,16 @@
|
||||
package app
|
||||
|
||||
import "log"
|
||||
|
||||
func Panic(v ...any) {
|
||||
log.Panicln(Name + ":", v)
|
||||
}
|
||||
|
||||
func Panicf(format string, v ...any) {
|
||||
log.Panicf(Name + ": " + format, v...)
|
||||
}
|
||||
|
||||
func Panicln(v ...any) {
|
||||
v = append([]any{Name + ":"}, v...)
|
||||
log.Panicln(v...)
|
||||
}
|
@ -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
|
||||
}
|
||||
|
@ -15,6 +15,8 @@ import (
|
||||
"git.pbiernat.dev/golang/rest-api-prototype/internal/app/handler"
|
||||
)
|
||||
|
||||
const Name = "REST API Service"
|
||||
|
||||
type Server struct {
|
||||
*http.Server
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user