aboutsummaryrefslogtreecommitdiff
path: root/cmd/xesite/internalapi.go
blob: ca4da3bac56ef00c01c947292907d74d91ffda89 (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
36
37
package main

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

	"tailscale.com/tsnet"
	"tailscale.com/tsweb"
	"xeiaso.net/v4/internal/lume"
)

func internalAPI(srv *tsnet.Server, fs *lume.FS) {
	mux := http.NewServeMux()

	mux.Handle("/debug/vars", expvar.Handler())
	mux.HandleFunc("/metrics", tsweb.VarzHandler)

	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 := srv.Listen("tcp", ":80")
	if err != nil {
		log.Fatal(err)
	}

	http.Serve(ln, mux)
}