diff --git a/broker-service/cmd/api/handlers.go b/broker-service/cmd/api/handlers.go new file mode 100644 index 0000000..e69f157 --- /dev/null +++ b/broker-service/cmd/api/handlers.go @@ -0,0 +1,24 @@ +package main + +import ( + "encoding/json" + "net/http" +) + +type jsonResponse struct { + Error bool `json:"error"` + Message string `json:"message"` + Data any `json:"data,omitempty"` +} + +func (app *Config) Broker(w http.ResponseWriter, r *http.Request) { + payload := jsonResponse{ + Error: false, + Message: "Hit the broker", + } + + out, _ := json.MarshalIndent(payload, "", "\t") + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusAccepted) + w.Write(out) +} diff --git a/broker-service/cmd/api/main.go b/broker-service/cmd/api/main.go new file mode 100644 index 0000000..944cc85 --- /dev/null +++ b/broker-service/cmd/api/main.go @@ -0,0 +1,28 @@ +package main + +import ( + "fmt" + "log" + "net/http" +) + +const webPort = "4000" + +type Config struct{} + +func main() { + app := Config{} + + log.Printf("Starting broker service on port %s\n", webPort) + + // define http server + srv := &http.Server{ + Addr: fmt.Sprintf(":%s", webPort), + Handler: app.routes(), + } + + err := srv.ListenAndServe() + if err != nil { + log.Panic(err) + } +} diff --git a/broker-service/cmd/api/routes.go b/broker-service/cmd/api/routes.go new file mode 100644 index 0000000..1d49585 --- /dev/null +++ b/broker-service/cmd/api/routes.go @@ -0,0 +1,29 @@ +package main + +import ( + "net/http" + + "github.com/go-chi/chi/v5" + "github.com/go-chi/chi/v5/middleware" + "github.com/go-chi/cors" +) + +func (app *Config) routes() http.Handler { + mux := chi.NewRouter() + + // specify who is allowed to connect + mux.Use(cors.Handler(cors.Options{ + AllowedOrigins: []string{"https://*", "http://*"}, + AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"}, + AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"}, + ExposedHeaders: []string{"Link"}, + AllowCredentials: true, + MaxAge: 300, + })) + + mux.Use(middleware.Heartbeat("/ping")) + + mux.Post("/", app.Broker) + + return mux +} diff --git a/broker-service/go.mod b/broker-service/go.mod new file mode 100644 index 0000000..6af2f9b --- /dev/null +++ b/broker-service/go.mod @@ -0,0 +1,8 @@ +module broker + +go 1.22.5 + +require ( + github.com/go-chi/chi/v5 v5.1.0 + github.com/go-chi/cors v1.2.1 +) diff --git a/broker-service/go.sum b/broker-service/go.sum new file mode 100644 index 0000000..13b5d44 --- /dev/null +++ b/broker-service/go.sum @@ -0,0 +1,4 @@ +github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw= +github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= +github.com/go-chi/cors v1.2.1 h1:xEC8UT3Rlp2QuWNEr4Fs/c2EAGVKBwy/1vHx3bppil4= +github.com/go-chi/cors v1.2.1/go.mod h1:sSbTewc+6wYHBBCW7ytsFSn836hqM7JxpglAy2Vzc58=