✨ Add broker-service
This commit is contained in:
parent
2796d816e8
commit
60aeac4f4b
5 changed files with 90 additions and 0 deletions
24
broker-service/cmd/api/handlers.go
Normal file
24
broker-service/cmd/api/handlers.go
Normal 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)
|
||||||
|
}
|
||||||
28
broker-service/cmd/api/main.go
Normal file
28
broker-service/cmd/api/main.go
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
29
broker-service/cmd/api/routes.go
Normal file
29
broker-service/cmd/api/routes.go
Normal 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
5
go.mod
|
|
@ -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
4
go.sum
Normal 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=
|
||||||
Loading…
Add table
Add a link
Reference in a new issue