Merge branch 'develop'
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
commit
056421b5c0
@ -7,7 +7,7 @@ steps:
|
|||||||
image: golang:latest
|
image: golang:latest
|
||||||
commands:
|
commands:
|
||||||
- go install honnef.co/go/tools/cmd/staticcheck@latest
|
- go install honnef.co/go/tools/cmd/staticcheck@latest
|
||||||
- cd src && staticcheck ./src/internal/...
|
- cd src && staticcheck ./...
|
||||||
volumes:
|
volumes:
|
||||||
- name: gopath
|
- name: gopath
|
||||||
path: /go
|
path: /go
|
||||||
@ -16,7 +16,7 @@ steps:
|
|||||||
image: golang:latest
|
image: golang:latest
|
||||||
commands:
|
commands:
|
||||||
- go install golang.org/x/lint/golint@latest
|
- go install golang.org/x/lint/golint@latest
|
||||||
- golint ./src/internal/...
|
- golint ./src/...
|
||||||
volumes:
|
volumes:
|
||||||
- name: gopath
|
- name: gopath
|
||||||
path: /go
|
path: /go
|
||||||
@ -24,7 +24,7 @@ steps:
|
|||||||
- name: analyze
|
- name: analyze
|
||||||
image: golang:latest
|
image: golang:latest
|
||||||
commands:
|
commands:
|
||||||
- go vet ./src/internal/...
|
- cd src && go vet ./...
|
||||||
volumes:
|
volumes:
|
||||||
- name: gopath
|
- name: gopath
|
||||||
path: /go
|
path: /go
|
||||||
|
@ -14,9 +14,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
defHttpIp = "0.0.0.0"
|
defHTTPIP = "0.0.0.0"
|
||||||
defHttpPort = "8080"
|
defHTTPPort = "8080"
|
||||||
defDbUrl = "postgres://postgres:12345678@127.0.0.1:5432/egommerce"
|
defDbURL = "postgres://postgres:12345678@127.0.0.1:5432/egommerce"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@ -24,15 +24,15 @@ func main() {
|
|||||||
app.Panicf("Error loading .env file")
|
app.Panicf("Error loading .env file")
|
||||||
}
|
}
|
||||||
|
|
||||||
httpAddr := net.JoinHostPort(config.GetEnv("SERVER_IP", defHttpIp), defHttpPort)
|
httpAddr := net.JoinHostPort(config.GetEnv("SERVER_IP", defHTTPIP), defHTTPPort)
|
||||||
dbConnStr := config.GetEnv("DATABASE_URL", defDbUrl)
|
dbConnStr := config.GetEnv("DATABASE_URL", defDbURL)
|
||||||
|
|
||||||
dbc, err := database.Connect(dbConnStr)
|
dbc, err := database.Connect(dbConnStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
app.Panicf("Unable to connect to database: %v\n", err)
|
app.Panicf("Unable to connect to database: %v\n", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
env := &handler.Env{httpAddr, dbc}
|
env := &handler.Env{Addr: httpAddr, DB: dbc}
|
||||||
srv := app.NewServer(env)
|
srv := app.NewServer(env)
|
||||||
|
|
||||||
go srv.Start()
|
go srv.Start()
|
||||||
|
@ -3,7 +3,7 @@ package handler
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io/ioutil"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
@ -55,9 +55,9 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func decodeRequestData(r *http.Request, v interface{}) error {
|
func decodeRequestData(r *http.Request, v interface{}) error {
|
||||||
buf, _ := ioutil.ReadAll(r.Body)
|
buf, _ := io.ReadAll(r.Body)
|
||||||
rdr := ioutil.NopCloser(bytes.NewReader(buf))
|
rdr := io.NopCloser(bytes.NewReader(buf))
|
||||||
r.Body = ioutil.NopCloser(bytes.NewReader(buf))
|
r.Body = io.NopCloser(bytes.NewReader(buf))
|
||||||
|
|
||||||
json.NewDecoder(rdr).Decode(&v)
|
json.NewDecoder(rdr).Decode(&v)
|
||||||
|
|
||||||
|
@ -13,7 +13,7 @@ func SetupRouter(env *handler.Env) *mux.Router {
|
|||||||
r.MethodNotAllowedHandler = &handler.MethodNotAllowedHandler{}
|
r.MethodNotAllowedHandler = &handler.MethodNotAllowedHandler{}
|
||||||
|
|
||||||
r.Use(PrepareHeadersMiddleware)
|
r.Use(PrepareHeadersMiddleware)
|
||||||
r.Use(ValidateJsonBodyMiddleware) // probably not needed
|
r.Use(ValidateJSONBodyMiddleware) // probably not needed
|
||||||
r.Use(LoggingMiddleware)
|
r.Use(LoggingMiddleware)
|
||||||
|
|
||||||
r.Handle("/health", handler.Init(env, handler.HealthCheckHandler)).Methods(http.MethodGet)
|
r.Handle("/health", handler.Init(env, handler.HealthCheckHandler)).Methods(http.MethodGet)
|
||||||
|
@ -3,7 +3,7 @@ package app
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io/ioutil"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -61,10 +61,10 @@ func PrepareHeadersMiddleware(next http.Handler) http.Handler {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func ValidateJsonBodyMiddleware(next http.Handler) http.Handler {
|
func ValidateJSONBodyMiddleware(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
buf, _ := ioutil.ReadAll(r.Body)
|
buf, _ := io.ReadAll(r.Body)
|
||||||
r.Body = ioutil.NopCloser(bytes.NewReader(buf)) // rollack *Request to original state
|
r.Body = io.NopCloser(bytes.NewReader(buf)) // rollack *Request to original state
|
||||||
|
|
||||||
if len(buf) > 0 && !json.Valid(buf) {
|
if len(buf) > 0 && !json.Valid(buf) {
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
Loading…
Reference in New Issue
Block a user