aboutsummaryrefslogtreecommitdiff
path: root/cmd/xesite/internalapi.go
blob: 7d1ebcbc6239a427034151128da03a83e74622e2 (plain)
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
package main

import (
	"context"
	"expvar"
	"log"
	"net"
	"net/http"
	"path/filepath"

	"xeiaso.net/v4/internal/lume"
)

func internalAPI(fs *lume.FS) {
	mux := http.NewServeMux()

	mux.Handle("/debug/vars", expvar.Handler())

	mux.HandleFunc("/rebuild", func(w http.ResponseWriter, r *http.Request) {
		go fs.Update(context.Background())
	})

	mux.HandleFunc("/zip", func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "application/zip")
		w.Header().Set("Content-Disposition", "attachment; filename=site.zip")
		http.ServeFile(w, r, filepath.Join(*dataDir, "site.zip"))
	})

	ln, err := net.Listen("tcp", ":80")
	if err != nil {
		log.Fatal(err)
	}

	http.Serve(ln, mux)
}