-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
284 lines (249 loc) · 7.52 KB
/
main.go
File metadata and controls
284 lines (249 loc) · 7.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package main
import (
"context"
"flag"
"fmt"
"log/slog"
"net/http"
"net/http/httputil"
"net/url"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/golang-jwt/jwt/v4"
"github.com/rs/cors"
"gopkg.in/yaml.v3"
)
// Config structs
type Config struct {
Server ServerConfig `yaml:"server"`
JWTSecret string `yaml:"jwt_secret"`
Services []ServiceConfig `yaml:"services"`
}
type ServerConfig struct {
Port string `yaml:"port"`
}
type ServiceConfig struct {
Name string `yaml:"name"`
PathPrefix string `yaml:"path_prefix"`
TargetURL string `yaml:"target_url"`
StripPrefix string `yaml:"strip_prefix"`
AuthRequired bool `yaml:"auth_required"`
EnvVar string `yaml:"env_var"`
}
var logger *slog.Logger
// read config file and apply env overrides
func loadConfig(path string) (*Config, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("failed to read config file: %w", err)
}
var cfg Config
if err := yaml.Unmarshal(data, &cfg); err != nil {
return nil, fmt.Errorf("failed to unmarshal config yaml: %w", err)
}
// Environment overrides
if secret := os.Getenv("JWT_SECRET"); secret != "" {
cfg.JWTSecret = secret
}
for i := range cfg.Services {
env := cfg.Services[i].EnvVar
if env == "" {
// default source for service URL
n := strings.ToUpper(strings.ReplaceAll(cfg.Services[i].Name, "-", "_"))
env = n + "_SERVICE_URL"
}
if v := os.Getenv(env); v != "" {
cfg.Services[i].TargetURL = v
logger.Info("service url overridden from env", "service", cfg.Services[i].Name, "var", env)
}
}
return &cfg, nil
}
func newProxy(targetURL, stripPrefix string) (*httputil.ReverseProxy, error) {
target, err := url.Parse(targetURL)
if err != nil {
return nil, fmt.Errorf("invalid target url: %w", err)
}
proxy := httputil.NewSingleHostReverseProxy(target)
orig := proxy.Director
proxy.Director = func(req *http.Request) {
// keep user headers
sub := req.Header.Get("X-User-Subject")
userId := req.Header.Get("X-User-Id")
roles := req.Header.Get("X-User-Roles")
orig(req)
req.Host = target.Host
if sub != "" {
req.Header.Set("X-User-Subject", sub)
}
if userId != "" {
req.Header.Set("X-User-Id", userId)
}
if roles != "" {
req.Header.Set("X-User-Roles", roles)
}
if stripPrefix != "" {
req.URL.Path = strings.TrimPrefix(req.URL.Path, stripPrefix)
}
}
proxy.ModifyResponse = func(resp *http.Response) error {
logger.Info("response from downstream", "service", targetURL, "status", resp.Status, "path", resp.Request.URL.Path)
return nil
}
return proxy, nil
}
// auth
type contextKey string
const userClaimsKey contextKey = "userClaims"
func authMiddleware(secret []byte) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
auth := r.Header.Get("Authorization")
if auth == "" {
http.Error(w, "Missing Authorization Header", http.StatusUnauthorized)
return
}
tok, found := strings.CutPrefix(auth, "Bearer ")
if !found {
http.Error(w, "Invalid Authorization Header format", http.StatusUnauthorized)
return
}
p, err := jwt.Parse(tok, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
}
return secret, nil
})
if err != nil {
logger.Warn("error parsing token", "err", err)
http.Error(w, "Invalid Token", http.StatusUnauthorized)
return
}
if claims, ok := p.Claims.(jwt.MapClaims); ok && p.Valid {
ctx := context.WithValue(r.Context(), userClaimsKey, claims)
next.ServeHTTP(w, r.WithContext(ctx))
return
}
http.Error(w, "Invalid Token", http.StatusUnauthorized)
})
}
}
func injectUserInfo(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if claims, ok := r.Context().Value(userClaimsKey).(jwt.MapClaims); ok {
if sub, exists := claims["sub"]; exists {
userIdStr := fmt.Sprintf("%v", sub)
// Set both headers for compatibility with different services
r.Header.Set("X-User-Subject", userIdStr)
r.Header.Set("X-User-Id", userIdStr)
}
if roles, exists := claims["roles"]; exists {
if rs, ok := roles.([]interface{}); ok {
var parts []string
for _, r := range rs {
parts = append(parts, fmt.Sprintf("%v", r))
}
r.Header.Set("X-User-Roles", strings.Join(parts, ","))
}
}
logger.Info("injecting user info headers", "sub", r.Header.Get("X-User-Subject"), "user-id", r.Header.Get("X-User-Id"))
}
next.ServeHTTP(w, r)
})
}
func main() {
logger = slog.New(slog.NewJSONHandler(os.Stdout, nil))
slog.SetDefault(logger)
// Command line flags
cfgPath := flag.String("config", "config.yaml", "Path to configuration yaml")
overridePort := flag.String("port", "", "Optional: override server port (e.g. :8080)")
flag.Parse()
cfg, err := loadConfig(*cfgPath)
if err != nil {
logger.Error("failed to load config", "error", err)
os.Exit(1)
}
// Port override from flags
if *overridePort != "" {
cfg.Server.Port = *overridePort
}
r := buildRouter(cfg)
srv := &http.Server{
Addr: cfg.Server.Port,
Handler: r,
}
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGTERM, syscall.SIGINT)
go func() {
logger.Info("api-gateway listening", "addr", srv.Addr)
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
logger.Error("listen error", "err", err)
os.Exit(1)
}
}()
<-quit
logger.Info("shutting down server...")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
logger.Error("server forced shutdown", "err", err)
os.Exit(1)
}
logger.Info("server exiting")
}
// buildRouter constructs a Chi router for the gateway — useful for testing
func buildRouter(cfg *Config) chi.Router {
r := chi.NewRouter()
r.Use(middleware.RequestID)
r.Use(middleware.RealIP)
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
// CORS
allowedOrigins := []string{"http://localhost:3000"}
if env := os.Getenv("FRONTEND_ORIGINS"); env != "" {
for _, origin := range strings.Split(env, ",") {
if strings.TrimSpace(origin) != "" {
allowedOrigins = append(allowedOrigins, strings.TrimSpace(origin))
}
}
}
corsMw := cors.New(cors.Options{
AllowedOrigins: allowedOrigins,
AllowedMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"},
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
ExposedHeaders: []string{"Link"},
AllowCredentials: true,
MaxAge: 300,
})
r.Use(corsMw.Handler)
// health
r.Get("/healthz", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
})
authMw := authMiddleware([]byte(cfg.JWTSecret))
for _, s := range cfg.Services {
proxy, err := newProxy(s.TargetURL, s.StripPrefix)
if err != nil {
logger.Error("failed to create proxy", "service", s.Name, "err", err)
os.Exit(1)
}
h := http.Handler(proxy)
r.Group(func(r2 chi.Router) {
if s.AuthRequired {
r2.Use(authMw)
r2.Use(injectUserInfo)
}
// Register both prefix and wildcard form to match both exact and nested paths
r2.Handle(s.PathPrefix, h)
r2.Handle(s.PathPrefix+"/*", h)
})
logger.Info("registered service", "name", s.Name, "prefix", s.PathPrefix, "target", s.TargetURL)
}
return r
}