diff options
| author | Xe Iaso <me@xeiaso.net> | 2023-10-27 18:44:20 -0400 |
|---|---|---|
| committer | Xe Iaso <me@xeiaso.net> | 2023-10-27 18:44:20 -0400 |
| commit | b1d8c9266d48a59e9c666b203ae9f77e8f24d23d (patch) | |
| tree | f942a4e15677e6f4a2905c785b4bbf447ae676a3 /cmd | |
| parent | 02990e6d3e7a44d409d1d30b2181152f20251a36 (diff) | |
| download | x-b1d8c9266d48a59e9c666b203ae9f77e8f24d23d.tar.xz x-b1d8c9266d48a59e9c666b203ae9f77e8f24d23d.zip | |
cmd/xedn: upload zipserver zips
Signed-off-by: Xe Iaso <me@xeiaso.net>
Diffstat (limited to 'cmd')
| -rw-r--r-- | cmd/xedn/internal/xesite/xesite.go | 38 | ||||
| -rw-r--r-- | cmd/xedn/main.go | 18 |
2 files changed, 48 insertions, 8 deletions
diff --git a/cmd/xedn/internal/xesite/xesite.go b/cmd/xedn/internal/xesite/xesite.go index fac2000..13e5f62 100644 --- a/cmd/xedn/internal/xesite/xesite.go +++ b/cmd/xedn/internal/xesite/xesite.go @@ -3,11 +3,14 @@ package xesite import ( "archive/zip" "compress/gzip" + "fmt" "io" "log/slog" "net/http" "os" + "path/filepath" "sync" + "time" ) const ( @@ -29,12 +32,13 @@ func init() { } type ZipServer struct { + dir string lock sync.RWMutex zip *zip.ReadCloser } -func NewZipServer(zipPath string) (*ZipServer, error) { - result := &ZipServer{} +func NewZipServer(zipPath, dir string) (*ZipServer, error) { + result := &ZipServer{dir: dir} if _, err := os.Stat(zipPath); !os.IsNotExist(err) { file, err := zip.OpenReader(zipPath) @@ -48,6 +52,36 @@ func NewZipServer(zipPath string) (*ZipServer, error) { return result, nil } +func (zs *ZipServer) UploadNewZip(w http.ResponseWriter, r *http.Request) { + fname := fmt.Sprintf("xesite-%s.zip", time.Now().Format("2006-01-02T15-04-05")) + fpath := filepath.Join(zs.dir, "xesite", fname) + fout, err := os.Create(fpath) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + slog.Error("can't create file", "err", err, "fpath", fpath) + return + } + defer fout.Close() + + if _, err := io.Copy(fout, r.Body); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + slog.Error("can't write file", "err", err, "fpath", fpath) + return + } + + if err := fout.Close(); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + slog.Error("can't close file", "err", err, "fpath", fpath) + return + } + + if err := zs.Update(fpath); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + slog.Error("can't update zip", "err", err, "fpath", fpath) + return + } +} + func (zs *ZipServer) Update(fname string) error { zs.lock.Lock() defer zs.lock.Unlock() diff --git a/cmd/xedn/main.go b/cmd/xedn/main.go index 6536f0a..2305938 100644 --- a/cmd/xedn/main.go +++ b/cmd/xedn/main.go @@ -126,6 +126,12 @@ func main() { group: &singleflight.Group{}, } + os.MkdirAll(filepath.Join(*dir, "xesite"), 0700) + zs, err := xesite.NewZipServer(filepath.Join(*dir, "xesite", "latest.zip"), *dir) + if err != nil { + log.Fatal(err) + } + go func() { lis, err := srv.Listen("tcp", ":80") if err != nil { @@ -135,6 +141,7 @@ func main() { http.DefaultServeMux.HandleFunc("/debug/varz", tsweb.VarzHandler) http.DefaultServeMux.HandleFunc("/xedn/files", dc.ListFiles) http.DefaultServeMux.HandleFunc("/xedn/purge", dc.Purge) + http.DefaultServeMux.HandleFunc("/xesite/upload", zs.UploadNewZip) http.DefaultServeMux.HandleFunc("/sticker/files", ois.ListFiles) http.DefaultServeMux.HandleFunc("/sticker/purge", ois.Purge) @@ -192,19 +199,18 @@ func main() { cdn.HandleFunc("/file/christine-static/", hdlr) cdn.HandleFunc("/file/xeserv-akko/", hdlr) - os.MkdirAll(filepath.Join(*dir, "xesite"), 0700) - zs, err := xesite.NewZipServer(filepath.Join(*dir, "xesite", "latest.zip")) - if err != nil { - log.Fatal(err) - } - topLevel := mux.NewRouter() + topLevel.Host("cdn.christine.website").Handler(cdn) topLevel.Host("cdn.xeiaso.net").Handler(cdn) topLevel.Host("xedn.fly.dev").Handler(cdn) topLevel.Host("pneuma.shark-harmonic.ts.net").Handler(cdn) topLevel.Host("xelaso.net").Handler(zs) + topLevel.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + http.Error(w, "wait, what, how did you do that?", http.StatusBadRequest) + }) + slog.Info("starting up", "addr", *addr) http.ListenAndServe(*addr, cors.Default().Handler(xffMW.Handler(topLevel))) } |
