Skip to content

Commit

Permalink
style: fix issues by lint
Browse files Browse the repository at this point in the history
  • Loading branch information
mauricioromagnollo committed Oct 26, 2023
1 parent c129596 commit 38dc79c
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 13 deletions.
15 changes: 14 additions & 1 deletion cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package main

import (
"fmt"
"log"
"net/http"
"time"

"github.com/go-chi/chi/v5"
"github.com/mauricioromagnollo/kafrest/external/config"
Expand All @@ -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)
}
}
2 changes: 2 additions & 0 deletions external/config/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import (
"github.com/joho/godotenv"
)

// Environment is the struct that will hold the environment variables.
type Environment struct {
AppEnv string
AppName string
AppPort string
KafkaHost string
}

// NewEnvironment is the constructor for the Environment struct.
func NewEnvironment() *Environment {
loadDotEnvFile()

Expand Down
35 changes: 27 additions & 8 deletions external/controllers/publish_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand All @@ -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)
}
}
18 changes: 14 additions & 4 deletions external/controllers/status_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,44 @@ 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,
}

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)
}
}

0 comments on commit 38dc79c

Please sign in to comment.