package main import ( "encoding/json" "io" "log/slog" "net/http" "os" "path" "github.com/fermyon/spin-go-sdk/variables" spinhttp "github.com/fermyon/spin/sdk/go/v2/http" ) var logLevel slog.LevelVar func init() { // call the Handle function spinhttp.Handle(func(w http.ResponseWriter, r *http.Request) { if levelValue, err := variables.Get("log_level"); err == nil { _ = logLevel.UnmarshalText([]byte(levelValue)) } logger := slog.New(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{ Level: &logLevel, })) logger.Info("Incoming request", slog.String("url", r.URL.String())) mappingValue, err := variables.Get("domain_mapping") if err != nil { panic(err) } mapping := make(map[string]string) if err := json.Unmarshal([]byte(mappingValue), &mapping); err != nil { panic(err) } mappedHost, ok := mapping[r.URL.Host] if !ok { w.WriteHeader(http.StatusNotFound) return } if path.Ext(r.URL.Path) == "" { r.URL.Path = path.Join(r.URL.Path, "index.html") } r.URL.Host = mappedHost r.URL.Scheme = "https" req, err := http.NewRequestWithContext(r.Context(), r.Method, r.URL.String(), nil) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } logger.Info("Forwarding request", slog.String("url", req.URL.String())) resp, err := spinhttp.Send(req) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } defer resp.Body.Close() logger.Info("Got response", slog.Int("status_code", resp.StatusCode)) for k, v := range resp.Header { w.Header().Add(k, v[0]) } w.WriteHeader(resp.StatusCode) _, _ = io.Copy(w, resp.Body) }) }