http and common
This commit is contained in:
+14
-17
@@ -2,7 +2,6 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
@@ -10,6 +9,8 @@ import (
|
||||
|
||||
"git.k6n.net/mats/go-cart-actor/pkg/backofficeadmin"
|
||||
"git.k6n.net/mats/platform/config"
|
||||
"git.k6n.net/mats/platform/httpclient"
|
||||
"git.k6n.net/mats/platform/httpserver"
|
||||
"git.k6n.net/mats/platform/rabbit"
|
||||
)
|
||||
|
||||
@@ -41,21 +42,14 @@ func main() {
|
||||
})
|
||||
|
||||
// Global CORS middleware allowing all origins and handling preflight.
|
||||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, PATCH, DELETE, OPTIONS")
|
||||
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization, X-Requested-With")
|
||||
w.Header().Set("Access-Control-Expose-Headers", "*")
|
||||
if r.Method == http.MethodOptions {
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
return
|
||||
}
|
||||
mux.ServeHTTP(w, r)
|
||||
})
|
||||
|
||||
srv := &http.Server{
|
||||
Addr: addr,
|
||||
Handler: handler,
|
||||
Addr: addr,
|
||||
Handler: httpclient.CORS(
|
||||
httpclient.WithOrigin("*"),
|
||||
httpclient.WithMethods("GET, POST, PUT, PATCH, DELETE, OPTIONS"),
|
||||
httpclient.WithHeaders("Content-Type, Authorization, X-Requested-With"),
|
||||
httpclient.WithExposeHeaders("*"),
|
||||
)(mux),
|
||||
ReadTimeout: 15 * time.Second,
|
||||
WriteTimeout: 30 * time.Second,
|
||||
IdleTimeout: 60 * time.Second,
|
||||
@@ -70,6 +64,7 @@ func main() {
|
||||
if err != nil {
|
||||
log.Fatalf("failed to connect to RabbitMQ: %v", err)
|
||||
}
|
||||
defer conn.Close()
|
||||
}
|
||||
if err := app.Start(ctx, conn); err != nil {
|
||||
log.Printf("AMQP listener disabled: %v", err)
|
||||
@@ -78,7 +73,9 @@ func main() {
|
||||
}
|
||||
|
||||
log.Printf("backoffice HTTP listening on %s", addr)
|
||||
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||
log.Fatalf("http server error: %v", err)
|
||||
if err := httpserver.RunServerWithShutdown(srv, "cart-backoffice", 10*time.Second, 5*time.Second,
|
||||
func(context.Context) error { cancel(); return nil },
|
||||
); err != nil {
|
||||
log.Fatalf("server: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user