init: Initial commit
This commit is contained in:
67
app/server/gateway.go
Normal file
67
app/server/gateway.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"git.secnex.io/secnex/api-gateway/config"
|
||||
"git.secnex.io/secnex/masterlog"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/chi/v5/middleware"
|
||||
)
|
||||
|
||||
type Gateway struct {
|
||||
router *chi.Mux
|
||||
config config.Config
|
||||
routes *Routes
|
||||
proxy *Proxy
|
||||
}
|
||||
|
||||
func NewGateway(config config.Config) *Gateway {
|
||||
r := chi.NewRouter()
|
||||
|
||||
for _, feature := range config.GetFeatures() {
|
||||
switch feature {
|
||||
case "request_id":
|
||||
r.Use(middleware.RequestID)
|
||||
case "real_ip":
|
||||
r.Use(middleware.RealIP)
|
||||
case "logger":
|
||||
r.Use(middleware.Logger)
|
||||
}
|
||||
}
|
||||
|
||||
r.Route("/_/health", func(r chi.Router) {
|
||||
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte("OK"))
|
||||
})
|
||||
})
|
||||
|
||||
return &Gateway{config: config, router: r, routes: nil, proxy: nil}
|
||||
}
|
||||
|
||||
func (g *Gateway) SetRoutes(routes *Routes) {
|
||||
g.routes = routes
|
||||
}
|
||||
|
||||
func (g *Gateway) SetProxy(proxy *Proxy) {
|
||||
g.proxy = proxy
|
||||
}
|
||||
|
||||
func (g *Gateway) Start() {
|
||||
masterlog.Info("Starting gateway", map[string]interface{}{
|
||||
"host": g.config.GetGatewayConfiguration().Host,
|
||||
"port": g.config.GetGatewayConfiguration().Port,
|
||||
})
|
||||
for path, handler := range g.routes.handlers {
|
||||
masterlog.Info("Registering route", map[string]interface{}{
|
||||
"path": path,
|
||||
})
|
||||
g.router.Handle(path, handler)
|
||||
}
|
||||
|
||||
gatewayConfig := g.config.GetGatewayConfiguration()
|
||||
|
||||
http.ListenAndServe(fmt.Sprintf("%s:%d", gatewayConfig.Host, gatewayConfig.Port), g.router)
|
||||
}
|
||||
Reference in New Issue
Block a user