From 38dc79c5db1086cdebabf3d24e139f7176a61cde Mon Sep 17 00:00:00 2001 From: mauricioromagnollo Date: Thu, 26 Oct 2023 00:13:05 -0300 Subject: [PATCH] style: fix issues by lint --- cmd/api/main.go | 15 +++++++++- external/config/env.go | 2 ++ external/controllers/publish_controller.go | 35 +++++++++++++++++----- external/controllers/status_controller.go | 18 ++++++++--- 4 files changed, 57 insertions(+), 13 deletions(-) diff --git a/cmd/api/main.go b/cmd/api/main.go index 38e03cf..00d9e79 100644 --- a/cmd/api/main.go +++ b/cmd/api/main.go @@ -2,7 +2,9 @@ package main import ( "fmt" + "log" "net/http" + "time" "github.com/go-chi/chi/v5" "github.com/mauricioromagnollo/kafrest/external/config" @@ -21,5 +23,16 @@ func main() { r.Post("/messages", publishController.Handle) port := fmt.Sprintf(":%v", env.AppPort) - http.ListenAndServe(port, r) + + server := &http.Server{ + Addr: port, + Handler: r, + ReadTimeout: 10 * time.Second, + WriteTimeout: 10 * time.Second, + IdleTimeout: 120 * time.Second, + } + + if err := server.ListenAndServe(); err != nil { + log.Fatal(err) + } } diff --git a/external/config/env.go b/external/config/env.go index 586117b..14b62ec 100644 --- a/external/config/env.go +++ b/external/config/env.go @@ -7,6 +7,7 @@ import ( "github.com/joho/godotenv" ) +// Environment is the struct that will hold the environment variables. type Environment struct { AppEnv string AppName string @@ -14,6 +15,7 @@ type Environment struct { KafkaHost string } +// NewEnvironment is the constructor for the Environment struct. func NewEnvironment() *Environment { loadDotEnvFile() diff --git a/external/controllers/publish_controller.go b/external/controllers/publish_controller.go index d9f5b70..51e6e1d 100644 --- a/external/controllers/publish_controller.go +++ b/external/controllers/publish_controller.go @@ -9,28 +9,31 @@ import ( "github.com/segmentio/kafka-go" ) +// PublishController is the struct that will handle the /publish endpoint. type PublishController struct { Environment *config.Environment } -type PublishRequest struct { +type publishRequest struct { Topic string `json:"topic"` Messages []map[string]interface{} `json:"messages"` } -type PublishResponse struct { +type publishResponse struct { MessagesPublished []map[string]interface{} `json:"messages_published"` } +// NewPublishController is the constructor for the PublishController struct. func NewPublishController(env *config.Environment) *PublishController { return &PublishController{ Environment: env, } } +// Handle is the function that will be called when a request is made to the /publish endpoint. func (pc PublishController) Handle(w http.ResponseWriter, r *http.Request) { - var requestData PublishRequest - var responseData PublishResponse + var requestData publishRequest + var responseData publishResponse if err := json.NewDecoder(r.Body).Decode(&requestData); err != nil { return @@ -46,13 +49,21 @@ func (pc PublishController) Handle(w http.ResponseWriter, r *http.Request) { parsedMessage, err := json.Marshal(message) if err != nil { w.WriteHeader(http.StatusBadRequest) - w.Write([]byte(err.Error())) + _, err := w.Write([]byte(err.Error())) + if err != nil { + panic(err) + } + return } if err := kw.WriteMessages(context.Background(), kafka.Message{Value: parsedMessage}); err != nil { w.WriteHeader(http.StatusBadRequest) - w.Write([]byte(err.Error())) + _, err := w.Write([]byte(err.Error())) + if err != nil { + panic(err) + } + return } @@ -62,11 +73,19 @@ func (pc PublishController) Handle(w http.ResponseWriter, r *http.Request) { responseJSON, err := json.Marshal(responseData) if err != nil { w.WriteHeader(http.StatusBadRequest) - w.Write([]byte(err.Error())) + _, err := w.Write([]byte(err.Error())) + if err != nil { + panic(err) + } + return } w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusCreated) - w.Write(responseJSON) + + _, werr := w.Write(responseJSON) + if err != nil { + panic(werr) + } } diff --git a/external/controllers/status_controller.go b/external/controllers/status_controller.go index e544465..0277ee1 100644 --- a/external/controllers/status_controller.go +++ b/external/controllers/status_controller.go @@ -7,23 +7,26 @@ import ( "github.com/mauricioromagnollo/kafrest/external/config" ) +// StatusController is the struct that will handle the /status endpoint. type StatusController struct { Environment *config.Environment } -type StatusResponse struct { +type statusResponse struct { Message string `json:"message"` Broker string `json:"broker"` } +// NewStatusController is the constructor for the StatusController struct. func NewStatusController(env *config.Environment) *StatusController { return &StatusController{ Environment: env, } } +// Handle is the function that will be called when a request is made to the /status endpoint. func (sc StatusController) Handle(w http.ResponseWriter, _ *http.Request) { - responseData := StatusResponse{ + responseData := statusResponse{ Message: "OK", Broker: sc.Environment.KafkaHost, } @@ -31,10 +34,17 @@ func (sc StatusController) Handle(w http.ResponseWriter, _ *http.Request) { responseJSON, err := json.Marshal(responseData) if err != nil { w.WriteHeader(http.StatusInternalServerError) - w.Write([]byte(err.Error())) + _, err := w.Write([]byte(err.Error())) + if err != nil { + panic(err) + } + return } w.WriteHeader(http.StatusOK) - w.Write(responseJSON) + _, werr := w.Write(responseJSON) + if werr != nil { + panic(werr) + } }