149 lines
3.3 KiB
Go
149 lines
3.3 KiB
Go
package server
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
"log/slog"
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
|
|
"git.t-juice.club/microfilm/auth"
|
|
"git.t-juice.club/microfilm/auth/authmw"
|
|
"git.t-juice.club/microfilm/proxy"
|
|
"github.com/go-chi/chi/v5"
|
|
"github.com/go-chi/chi/v5/middleware"
|
|
"github.com/nats-io/nats.go"
|
|
)
|
|
|
|
type Service string
|
|
|
|
const (
|
|
ServiceUser Service = "user"
|
|
ServiceAuth Service = "auth"
|
|
ServiceMovies Service = "movies"
|
|
)
|
|
|
|
type Server struct {
|
|
Logger *slog.Logger
|
|
|
|
config *Config
|
|
nats *nats.EncodedConn
|
|
|
|
http.Server
|
|
}
|
|
|
|
func NewServer(config *Config) (*Server, error) {
|
|
srv := &Server{}
|
|
srv.config = config
|
|
|
|
r := chi.NewRouter()
|
|
r.Use(middleware.RealIP)
|
|
r.Use(middleware.RequestID)
|
|
r.Use(srv.MiddlewareLogging)
|
|
|
|
r.Get("/info", InfoHandler)
|
|
r.Handle("/user/*", srv.ProxyHandler(ServiceUser))
|
|
r.Handle("/auth/*", srv.ProxyHandler(ServiceAuth))
|
|
|
|
r.With(authmw.VerifyToken("http://mf-auth:8082", []string{auth.RoleAdmin})).Get("/testauth/admin", InfoHandler)
|
|
|
|
srv.Handler = r
|
|
srv.Addr = config.ListenAddr
|
|
|
|
return srv, nil
|
|
}
|
|
|
|
func InfoHandler(w http.ResponseWriter, r *http.Request) {
|
|
enc := json.NewEncoder(w)
|
|
|
|
data := &proxy.InfoResponse{
|
|
Version: proxy.Version,
|
|
}
|
|
|
|
_ = enc.Encode(data)
|
|
}
|
|
|
|
func WriteError(w http.ResponseWriter, response proxy.ErrorResponse) {
|
|
w.WriteHeader(response.Status)
|
|
|
|
encoder := json.NewEncoder(w)
|
|
_ = encoder.Encode(&response)
|
|
}
|
|
|
|
func (s *Server) ProxyHandler(service Service) http.Handler {
|
|
fn := func(w http.ResponseWriter, r *http.Request) {
|
|
client := http.Client{}
|
|
defer r.Body.Close()
|
|
|
|
newURL := s.convertURL(r.URL, service)
|
|
req, err := http.NewRequest(r.Method, newURL.String(), r.Body)
|
|
if err != nil {
|
|
s.Logger.Warn("Failed to create forwarding request.", "error", err, "url", newURL)
|
|
WriteError(w, proxy.ErrorResponse{
|
|
Status: http.StatusInternalServerError,
|
|
Message: "Failed to create request.",
|
|
})
|
|
return
|
|
}
|
|
for k, h := range r.Header {
|
|
for _, v := range h {
|
|
req.Header.Add(k, v)
|
|
}
|
|
}
|
|
|
|
req.Header.Add("X-Forwarded-For", r.RemoteAddr)
|
|
reqID := middleware.GetReqID(r.Context())
|
|
req.Header.Add("X-Request-Id", reqID)
|
|
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
s.Logger.Warn("Failed to perform forwarding request.", "error", err, "url", newURL)
|
|
WriteError(w, proxy.ErrorResponse{
|
|
Status: http.StatusInternalServerError,
|
|
Message: "Failed to create request.",
|
|
})
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
for key, values := range resp.Header {
|
|
for _, value := range values {
|
|
w.Header().Add(key, value)
|
|
}
|
|
}
|
|
|
|
w.WriteHeader(resp.StatusCode)
|
|
_, _ = io.Copy(w, resp.Body)
|
|
s.Logger.Debug("Performed proxy request.", "new_url", newURL, "original_url", r.URL.String(), "reqID", reqID)
|
|
}
|
|
return http.HandlerFunc(fn)
|
|
}
|
|
|
|
func (s *Server) convertURL(requestURL *url.URL, service Service) *url.URL {
|
|
oldURL := *requestURL
|
|
var targetURL *url.URL
|
|
var err error
|
|
var stripPrefix string
|
|
|
|
switch service {
|
|
case ServiceAuth:
|
|
targetURL, err = url.Parse(s.config.AuthServiceBaseURL)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
stripPrefix = "/auth"
|
|
case ServiceUser:
|
|
targetURL, err = url.Parse(s.config.UserServiceBaseURL)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
stripPrefix = "/user"
|
|
}
|
|
|
|
oldURL.Scheme = targetURL.Scheme
|
|
oldURL.Host = targetURL.Host
|
|
oldURL.Path, _ = strings.CutPrefix(oldURL.Path, stripPrefix)
|
|
|
|
return &oldURL
|
|
}
|