forked from ByteBrushStudios/void
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
386 lines (321 loc) · 10.3 KB
/
Copy pathmain.go
File metadata and controls
386 lines (321 loc) · 10.3 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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
package main
import (
"embed"
"encoding/json"
"fmt"
"html/template"
"io/fs"
"log"
"net/http"
"net/http/httputil"
"net/url"
"path/filepath"
"reflect"
"strings"
"time"
"void/extras"
"void/state"
"void/types"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-playground/validator/v10"
"github.com/infinitybotlist/eureka/zapchi"
"golang.org/x/exp/slices"
"gopkg.in/yaml.v3"
)
var (
//go:embed services
servicesFS embed.FS
//go:embed app.html
appHTML []byte
//go:embed error.html
errorHTML []byte
//go:embed assets/*
assets embed.FS
appTemplate *template.Template
errorTemplate *template.Template
)
// Load the services directory into the state
func init() {
state.Init()
// Initialize service registry
state.Registry = types.ServiceRegistry{
Services: make([]types.Service, 0),
APIUrls: make([]string, 0),
}
// First load default.yaml
defaultConfigBytes, err := servicesFS.ReadFile("services/default.yaml")
if err != nil {
log.Fatal("Could not read default.yaml:", err)
}
err = yaml.Unmarshal(defaultConfigBytes, &state.Registry.DefaultService)
if err != nil {
log.Fatal("Could not parse default.yaml:", err)
}
// Validate default service
validator := validator.New()
err = validator.Struct(state.Registry.DefaultService)
if err != nil {
log.Fatal("Could not validate default.yaml:", err)
}
// Load all other service configs
entries, err := servicesFS.ReadDir("services")
if err != nil {
log.Fatal("Could not read services directory:", err)
}
for _, entry := range entries {
if entry.IsDir() || entry.Name() == "default.yaml" {
continue
}
filePath := filepath.Join("services", entry.Name())
fileBytes, err := servicesFS.ReadFile(filePath)
if err != nil {
state.Logger.Error("Could not read service file:", filePath, err)
continue
}
var config types.ServiceConfig
err = yaml.Unmarshal(fileBytes, &config)
if err != nil {
state.Logger.Error("Could not parse service file:", filePath, err)
continue
}
// Validate config
err = validator.Struct(config)
if err != nil {
state.Logger.Error("Could not validate service file:", filePath, err)
continue
}
// Add services and API URLs to registry
state.Registry.Services = append(state.Registry.Services, config.Services...)
state.Registry.APIUrls = append(state.Registry.APIUrls, config.APIUrls...)
state.Logger.Info("Loaded service file:", filePath)
}
appTemplate = template.Must(template.New("app").Parse(string(appHTML)))
errorTemplate = template.Must(template.New("error").Parse(string(errorHTML)))
state.Logger.Info("Loaded services:", len(state.Registry.Services))
state.Logger.Info("Loaded API URLs:", len(state.Registry.APIUrls))
// Set version if built with -ldflags
extras.SetVersion(extras.GetVoidInfo().Version)
}
func dataHandlerMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// limit body to 10mb
r.Body = http.MaxBytesReader(w, r.Body, 10*1024*1024)
var allowedHeaders = []string{"Content-Type", "Authorization"}
reqHeaderList := strings.Split(r.Header.Get("Access-Control-Request-Headers"), ",")
for _, name := range reqHeaderList {
if name == "" {
continue
}
state.Logger.Info(name)
if strings.HasPrefix(strings.ToLower(name), "x-") {
allowedHeaders = append(allowedHeaders, strings.ReplaceAll(name, " ", ""))
}
}
w.Header().Set("Access-Control-Allow-Origin", r.Header.Get("Origin"))
w.Header().Set("Access-Control-Allow-Credentials", "true")
w.Header().Set("Access-Control-Allow-Headers", strings.Join(allowedHeaders, ", "))
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, PATCH, DELETE, OPTIONS")
if r.URL.Query().Get("data") != "" {
split := strings.Split(r.URL.Query().Get("data"), "|")
if len(split) > 0 {
r.Method = split[0]
}
if len(split) > 1 {
// All the remaining sections are considered PATH
r.URL.Path = strings.Join(split[1:], "/")
}
}
// Handle options immediately here
if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK)
w.Write([]byte{})
return
}
next.ServeHTTP(w, r)
})
}
// serveErrorPage renders the appropriate error page
func serveErrorPage(w http.ResponseWriter, r *http.Request, statusCode int, errorTitle, errorMsg, errorDetails string, service types.Service) {
w.WriteHeader(statusCode)
w.Header().Add("Content-Type", "text/html")
errorCtx := struct {
StatusCode int
ErrorTitle string
ErrorMessage string
ErrorDetails string
MatchedService types.Service
Path string
Hostname string
Info extras.VoidInfo
Redirect string
ErrorSummary string
ErrorStack string
ErrorTime string
}{
StatusCode: statusCode,
ErrorTitle: errorTitle,
ErrorMessage: errorMsg,
ErrorDetails: errorDetails,
MatchedService: service,
Path: r.URL.Path,
Hostname: r.Host,
Info: extras.GetVoidInfo(),
Redirect: r.URL.Query().Get("src"),
ErrorSummary: "Error connecting to backend: " + service.Host,
ErrorStack: captureErrorStack(fmt.Errorf("%s", errorMsg)),
}
err := errorTemplate.Execute(w, errorCtx)
if err != nil {
state.Logger.Error("Could not execute error template:", err)
w.Write([]byte(err.Error()))
}
}
func setupRoutes() {
r := chi.NewRouter()
// A good base middleware stack
r.Use(
middleware.Recoverer,
dataHandlerMiddleware,
middleware.RealIP,
middleware.CleanPath,
zapchi.Logger(state.Logger, "api"),
middleware.Timeout(30*time.Second),
)
subbedAssets, err := fs.Sub(assets, "assets")
if err != nil {
panic(err)
}
r.HandleFunc("/__voidStatic/*", func(w http.ResponseWriter, r *http.Request) {
http.StripPrefix("/__voidStatic", http.FileServer(http.FS(subbedAssets))).ServeHTTP(w, r)
})
// Health check endpoints (use handler from health.go)
r.HandleFunc("/health", extras.HealthHandler)
r.HandleFunc("/healthz", extras.HealthHandler)
// Update check endpoint
r.HandleFunc("/update-check", extras.UpdateCheckHandler)
r.HandleFunc("/*", func(w http.ResponseWriter, r *http.Request) {
// Get the hostname from the request
hostname := r.Host
// Also get the root domain from the request
var rootDomain string
rootDomainLst := strings.Split(hostname, ".")
// Then slice last 2 elements IF the length is greater than 2
if len(rootDomainLst) > 2 {
rootDomain = strings.Join(rootDomainLst[len(rootDomainLst)-2:], ".")
} else {
rootDomain = hostname
}
// Find the right service from config
var service = types.Service{
Name: state.Registry.DefaultService.Name,
Domain: state.Registry.DefaultService.Domain,
Support: state.Registry.DefaultService.Support,
Status: state.Registry.DefaultService.Status,
}
var matched bool
var backendURL string
for _, s := range state.Registry.Services {
if s.Domain == rootDomain {
service = s
matched = true
backendURL = s.Host // Use the host field as backend URL
break
}
}
// If this is an API URL, serve the maintenance JSON as before
if slices.Contains(state.Registry.APIUrls, hostname) {
w.WriteHeader(http.StatusRequestTimeout)
w.Header().Set("Content-Type", "application/json")
apiCtx := types.APICtx{
Message: "This service is down for maintenance...",
Service: service,
Info: extras.GetVoidInfo(),
}
json.NewEncoder(w).Encode(apiCtx)
return
}
// If a matching service is found, reverse proxy to its backend
if matched && backendURL != "" {
target, err := url.Parse(backendURL)
if err != nil {
state.Logger.Error("Invalid backend URL for service:", service.Name, backendURL, err)
serveErrorPage(w, r, http.StatusBadGateway, "Bad Gateway",
"The backend URL for this service is invalid or misconfigured, this error 100% means that the owner of this service misconfigured void.",
fmt.Sprintf("Error parsing backend URL: %s\nDetails: %v", backendURL, err), service)
return
}
proxy := httputil.NewSingleHostReverseProxy(target)
// Optionally, log proxy errors and return a meaningful error to the client
originalDirector := proxy.Director
proxy.Director = func(req *http.Request) {
originalDirector(req)
// Preserve original host header for backend
req.Host = target.Host
}
proxy.ErrorHandler = func(rw http.ResponseWriter, req *http.Request, err error) {
state.Logger.Error("Proxy error for service:", service.Name, err)
serveErrorPage(rw, req, http.StatusServiceUnavailable, "Service Unavailable",
"The backend service is currently unreachable or experiencing issues, this error likely indicates that void was unable to proxy the service and ny errors in the debug section should confirm this.",
fmt.Sprintf("Error connecting to backend: %s\nDetails: %v", backendURL, err), service)
}
proxy.ServeHTTP(w, r)
return
}
// Fallback: serve the info/status page as before
htmlCtx := types.HTMLCtx{
MatchedService: service,
Path: r.URL.Path,
Hostname: hostname,
Info: extras.GetVoidInfo(),
Redirect: r.URL.Query().Get("src"),
}
// Set status code of 408
w.WriteHeader(http.StatusRequestTimeout)
w.Header().Add("Content-Type", "text/html")
// Execute the template
err := appTemplate.Execute(w, htmlCtx)
if err != nil {
state.Logger.Error("Could not execute template:", err)
w.Write([]byte(err.Error()))
}
})
err = http.ListenAndServe(":1292", r)
if err != nil {
panic(err)
}
}
func main() {
setupRoutes()
}
// Helper function to capture stack trace if available
func captureErrorStack(err error) string {
// Check for errors that contain stack traces (like from github.com/pkg/errors)
type stackTracer interface {
StackTrace() []uintptr
}
// Try different error wrapping styles
if st, ok := err.(stackTracer); ok {
return fmt.Sprintf("%+v", st.StackTrace())
}
// For errors that might have their stack as a method or field
v := reflect.ValueOf(err)
if v.Kind() == reflect.Ptr && !v.IsNil() {
v = v.Elem()
}
// Try to get Stack method
stackMethod := v.MethodByName("Stack")
if stackMethod.IsValid() {
stack := stackMethod.Call(nil)
if len(stack) > 0 {
return fmt.Sprintf("%v", stack[0].Interface())
}
}
// Look for Stack field
stackField := v.FieldByName("Stack")
if stackField.IsValid() {
return fmt.Sprintf("%v", stackField.Interface())
}
// Fallback to error message
return ""
}