Add broker-service

This commit is contained in:
Dan Jones 2025-05-16 15:35:44 -05:00
commit 60aeac4f4b
Signed by: dan
GPG key ID: 5B3B0F7217473A29
5 changed files with 90 additions and 0 deletions

View file

@ -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.Marshal(payload)
w.Header().Set("Content-type", "application/json")
w.WriteHeader(http.StatusAccepted)
w.Write(out)
}

View file

@ -0,0 +1,28 @@
package main
import (
"fmt"
"log"
"net/http"
)
const webPort = "8085"
type Config struct{}
func main() {
app := &Config{}
log.Printf("Starting broker service on port %s\n", webPort)
// http.server
srv := &http.Server{
Addr: fmt.Sprintf(":%s", webPort),
Handler: app.routes(),
}
err := srv.ListenAndServe()
if err != nil {
panic(err)
}
}

View file

@ -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()
// Who is allowed
mux.Use(cors.Handler(cors.Options{
AllowedOrigins: []string{"https://*", "https://*"},
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
}

5
go.mod
View file

@ -1,3 +1,8 @@
module vc.goodevilgenius.org/udemy/31476802-go-microservice module vc.goodevilgenius.org/udemy/31476802-go-microservice
go 1.24.2 go 1.24.2
require (
github.com/go-chi/chi/v5 v5.2.1
github.com/go-chi/cors v1.2.1
)

4
go.sum Normal file
View file

@ -0,0 +1,4 @@
github.com/go-chi/chi/v5 v5.2.1 h1:KOIHODQj58PmL80G2Eak4WdvUzjSJSm0vG72crDCqb8=
github.com/go-chi/chi/v5 v5.2.1/go.mod h1:L2yAIGWB3H+phAw1NxKwWM+7eUH/lU8pOMm5hHcoops=
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=