functions/blob-proxy/main.go
Peter Kurfer 8897bbe4eb
Some checks failed
functions / build (blob-proxy) (push) Successful in 1m42s
functions / build (comics) (push) Failing after 1m57s
functions / deploy (push) Has been skipped
feat: migrate blob proxy
2025-01-21 16:33:58 +01:00

80 lines
1.7 KiB
Go

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 (
client = spinhttp.NewClient()
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 := client.Do(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)
})
}