From a38931bde1e888c9cccdae129fec7bad24c566ba Mon Sep 17 00:00:00 2001 From: Gabriel Simmer Date: Sun, 30 Jun 2024 15:56:49 +0100 Subject: [PATCH] Break out file host URL to allow custom CDNs --- cmd/soju/main.go | 3 ++- config/config.go | 3 +++ fileupload/fileupload.go | 13 ++++++++++++- server.go | 1 + 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/cmd/soju/main.go b/cmd/soju/main.go index 26c5f77..954e135 100644 --- a/cmd/soju/main.go +++ b/cmd/soju/main.go @@ -113,6 +113,7 @@ func loadConfig() (*config.Server, *soju.Config, error) { MOTD: motd, Auth: auth, FileUploader: fileUploader, + FileCdn: raw.FileCdn, } return raw, cfg, nil } @@ -151,7 +152,6 @@ func main() { srv := soju.NewServer(db) srv.SetConfig(serverCfg) srv.Logger = soju.NewLogger(log.Writer(), debug) - fileUploadHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { cfg := srv.Config() h := fileupload.Handler{ @@ -159,6 +159,7 @@ func main() { DB: db, Auth: cfg.Auth, HTTPOrigins: cfg.HTTPOrigins, + Cdn: cfg.FileCdn, } h.ServeHTTP(w, r) }) diff --git a/config/config.go b/config/config.go index 252cba1..c6351c9 100644 --- a/config/config.go +++ b/config/config.go @@ -83,6 +83,7 @@ type Server struct { MsgStore MsgStore Auth Auth FileUpload *FileUpload + FileCdn string HTTPOrigins []string HTTPIngress string @@ -130,6 +131,7 @@ func Load(filename string) (*Server, error) { Log []string `scfg:"log"` Auth []string `scfg:"auth"` FileUpload []string `scfg:"file-upload"` + FileCdn string `scfg:"file-cdn"` HTTPOrigin []string `scfg:"http-origin"` HTTPIngress string `scfg:"http-ingress"` AcceptProxyIP []string `scfg:"accept-proxy-ip"` @@ -224,6 +226,7 @@ func Load(filename string) (*Server, error) { } srv.FileUpload = &FileUpload{driver, source} } + srv.FileCdn = raw.FileCdn for _, origin := range raw.HTTPOrigin { if _, err := path.Match(origin, origin); err != nil { return nil, fmt.Errorf("directive http-origin: %v", err) diff --git a/fileupload/fileupload.go b/fileupload/fileupload.go index 1451ec0..d66945d 100644 --- a/fileupload/fileupload.go +++ b/fileupload/fileupload.go @@ -77,6 +77,7 @@ type Handler struct { Auth auth.Authenticator DB database.Database HTTPOrigins []string + Cdn string } func (h *Handler) checkOrigin(reqOrigin string) bool { @@ -153,6 +154,12 @@ func (h *Handler) fetch(resp http.ResponseWriter, req *http.Request) { return } + if h.Cdn != "" { + resp.Header().Set("Location", h.Cdn + "/" + filename) + resp.WriteHeader(http.StatusCreated) + return + } + basename, modTime, content, err := h.Uploader.load(filename) if err != nil { http.Error(resp, "failed to open file", http.StatusNotFound) @@ -285,7 +292,11 @@ func (h *Handler) store(resp http.ResponseWriter, req *http.Request) { return } - resp.Header().Set("Location", "/uploads/"+outFilename) + if h.Cdn != "" { + resp.Header().Set("Location", h.Cdn + "/" + outFilename) + } else { + resp.Header().Set("Location", "/uploads/"+outFilename) + } resp.WriteHeader(http.StatusCreated) } diff --git a/server.go b/server.go index 1f56205..fd409ce 100644 --- a/server.go +++ b/server.go @@ -152,6 +152,7 @@ type Config struct { EnableUsersOnAuth bool Auth auth.Authenticator FileUploader fileupload.Uploader + FileCdn string } type Server struct {