From 316323587d47164e663941685b7cca29baff4a4c Mon Sep 17 00:00:00 2001 From: Piotr Biernat Date: Sat, 18 Jun 2022 13:51:37 +0200 Subject: [PATCH 1/3] [fix] Restored DB connection --- cmd/server/main.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/cmd/server/main.go b/cmd/server/main.go index ef78d9e..137da91 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -2,6 +2,7 @@ package main import ( "context" + "git.pbiernat.dev/golang/rest-api-prototype/internal/app/database" "log" "net" "os" @@ -16,7 +17,7 @@ import ( 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() { @@ -25,15 +26,14 @@ func main() { } 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) - // 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 { + log.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.39.5 From 39ed417398c34c50d001b9db936adbfee6612f7e Mon Sep 17 00:00:00 2001 From: Piotr Biernat Date: Sat, 18 Jun 2022 15:01:05 +0200 Subject: [PATCH 2/3] [feature] Updated go.mod to Go v1.18 and extended log func --- cmd/server/main.go | 5 ++--- go.mod | 2 +- internal/app/log.go | 16 ++++++++++++++++ internal/app/server.go | 2 ++ 4 files changed, 21 insertions(+), 4 deletions(-) create mode 100644 internal/app/log.go diff --git a/cmd/server/main.go b/cmd/server/main.go index 137da91..c7deac3 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -3,7 +3,6 @@ package main import ( "context" "git.pbiernat.dev/golang/rest-api-prototype/internal/app/database" - "log" "net" "os" "os/signal" @@ -22,7 +21,7 @@ const ( 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)) @@ -30,7 +29,7 @@ func main() { dbc, err := database.Connect(dbConnStr) if err != nil { - log.Panicf("Unable to connect to database: %v\n", err) + app.Panicf("Unable to connect to database: %v\n", err) } env := &handler.Env{httpAddr, dbc} diff --git a/go.mod b/go.mod index 091c6c7..8349e1c 100644 --- a/go.mod +++ b/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 diff --git a/internal/app/log.go b/internal/app/log.go new file mode 100644 index 0000000..53fe9c1 --- /dev/null +++ b/internal/app/log.go @@ -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...) +} \ No newline at end of file diff --git a/internal/app/server.go b/internal/app/server.go index 33d5967..ffc9f59 100644 --- a/internal/app/server.go +++ b/internal/app/server.go @@ -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 } -- 2.39.5 From 90a0f562ea2e1e685f71ecb37a81ed68e46cae0d Mon Sep 17 00:00:00 2001 From: Piotr Biernat Date: Sun, 19 Jun 2022 14:45:38 +0200 Subject: [PATCH 3/3] [release] release v1 --- cmd/server/main.go | 4 +- internal/app/definition/article.go | 16 -------- internal/app/definition/category.go | 27 -------------- internal/app/entity/article.go | 15 -------- internal/app/entity/category.go | 18 --------- internal/app/handler/article.go | 34 ----------------- internal/app/handler/category.go | 57 ----------------------------- internal/app/router.go | 9 ----- 8 files changed, 3 insertions(+), 177 deletions(-) delete mode 100644 internal/app/definition/article.go delete mode 100644 internal/app/definition/category.go delete mode 100644 internal/app/entity/article.go delete mode 100644 internal/app/entity/category.go delete mode 100644 internal/app/handler/article.go delete mode 100644 internal/app/handler/category.go diff --git a/cmd/server/main.go b/cmd/server/main.go index c7deac3..d3b0886 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -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 { diff --git a/internal/app/definition/article.go b/internal/app/definition/article.go deleted file mode 100644 index a6b2800..0000000 --- a/internal/app/definition/article.go +++ /dev/null @@ -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"` -} diff --git a/internal/app/definition/category.go b/internal/app/definition/category.go deleted file mode 100644 index 09804ca..0000000 --- a/internal/app/definition/category.go +++ /dev/null @@ -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 { -} diff --git a/internal/app/entity/article.go b/internal/app/entity/article.go deleted file mode 100644 index dbc3089..0000000 --- a/internal/app/entity/article.go +++ /dev/null @@ -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"` -} diff --git a/internal/app/entity/category.go b/internal/app/entity/category.go deleted file mode 100644 index a91ff4c..0000000 --- a/internal/app/entity/category.go +++ /dev/null @@ -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)), -// ) -// } diff --git a/internal/app/handler/article.go b/internal/app/handler/article.go deleted file mode 100644 index fc2500d..0000000 --- a/internal/app/handler/article.go +++ /dev/null @@ -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 -} diff --git a/internal/app/handler/category.go b/internal/app/handler/category.go deleted file mode 100644 index b4e5723..0000000 --- a/internal/app/handler/category.go +++ /dev/null @@ -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 -} diff --git a/internal/app/router.go b/internal/app/router.go index 59852c7..8545265 100644 --- a/internal/app/router.go +++ b/internal/app/router.go @@ -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 } -- 2.39.5