code quality fixes
This commit is contained in:
@@ -14,9 +14,9 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
defHttpIp = "0.0.0.0"
|
||||
defHttpPort = "8080"
|
||||
defDbUrl = "postgres://postgres:12345678@postgres_svc:5432/egommerce"
|
||||
defHTTPIP = "0.0.0.0"
|
||||
defHTTPPort = "8080"
|
||||
defDbURL = "postgres://postgres:12345678@postgres_svc:5432/egommerce"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -24,15 +24,15 @@ func main() {
|
||||
app.Panicf("Error loading .env file")
|
||||
}
|
||||
|
||||
httpAddr := net.JoinHostPort(config.GetEnv("SERVER_IP", defHttpIp), defHttpPort)
|
||||
dbConnStr := config.GetEnv("DATABASE_URL", defDbUrl)
|
||||
httpAddr := net.JoinHostPort(config.GetEnv("SERVER_IP", defHTTPIP), defHTTPPort)
|
||||
dbConnStr := config.GetEnv("DATABASE_URL", defDbURL)
|
||||
|
||||
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{Addr: httpAddr, DB: dbc}
|
||||
srv := app.NewServer(env)
|
||||
|
||||
go srv.Start()
|
||||
|
||||
@@ -3,7 +3,7 @@ package handler
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
@@ -55,9 +55,9 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func decodeRequestData(r *http.Request, v interface{}) error {
|
||||
buf, _ := ioutil.ReadAll(r.Body)
|
||||
rdr := ioutil.NopCloser(bytes.NewReader(buf))
|
||||
r.Body = ioutil.NopCloser(bytes.NewReader(buf))
|
||||
buf, _ := io.ReadAll(r.Body)
|
||||
rdr := io.NopCloser(bytes.NewReader(buf))
|
||||
r.Body = io.NopCloser(bytes.NewReader(buf))
|
||||
|
||||
json.NewDecoder(rdr).Decode(&v)
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ package app
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
@@ -63,8 +63,8 @@ func PrepareHeadersMiddleware(next http.Handler) http.Handler {
|
||||
|
||||
func ValidateJsonBodyMiddleware(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
buf, _ := ioutil.ReadAll(r.Body)
|
||||
r.Body = ioutil.NopCloser(bytes.NewReader(buf)) // rollack *Request to original state
|
||||
buf, _ := io.ReadAll(r.Body)
|
||||
r.Body = io.NopCloser(bytes.NewReader(buf)) // rollack *Request to original state
|
||||
|
||||
if len(buf) > 0 && !json.Valid(buf) {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
|
||||
Reference in New Issue
Block a user