Home

tailpolicy @main - refs - log -
-
https://git.jolheiser.com/tailpolicy.git
Tailscale policy editor on your tailnet
tailpolicy / main.go
- raw
 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main

import (
	"encoding/json"
	"errors"
	"fmt"
	iofs "io/fs"
	"net/http"
	"os"
	"os/signal"
	"path/filepath"
	"strings"

	"github.com/go-git/go-git/v5"
	"go.jolheiser.com/tailpolicy/static"
	"go.jolheiser.com/tailroute"
)

func maine() error {
	args, fs := Flags()
	if err := fs.Parse(os.Args[1:]); err != nil {
		return err
	}
	if args.AuthKey != "" {
		os.Setenv("TS_AUTHKEY", args.AuthKey)
	}

	mux := http.NewServeMux()
	mux.HandleFunc("/", Index(args.RepoUrl, args.RepoLocation))
	mux.HandleFunc("/preview", preview(args.RepoUrl))
	mux.HandleFunc("/tailwind.css", static.TailwindHandler)
	r := tailroute.Router{
		Tailnet: mux,
	}

	if _, err := os.Stat(args.RepoLocation); errors.Is(err, iofs.ErrNotExist) {
		if err := os.MkdirAll(filepath.Dir(args.RepoLocation), os.ModePerm); err != nil {
			return err
		}
		if _, err := git.PlainClone(args.RepoLocation, false, &git.CloneOptions{
			URL: args.RepoUrl,
		}); err != nil {
			return err
		}
	}

	go func() {
		fmt.Printf("Tailnet listening on http://%s\n", args.Hostname)
		if err := r.Serve(args.Hostname, args.DataDir); err != nil {
			panic(err)
		}
	}()

	ch := make(chan os.Signal, 1)
	signal.Notify(ch, os.Kill, os.Interrupt)
	<-ch

	return nil
}

func preview(repoURL string) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var req struct {
			Input string
		}
		if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}

		out, err := Transpile(strings.NewReader(req.Input), repoURL)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		w.Write([]byte(out))
	}
}

func main() {
	if err := maine(); err != nil {
		fmt.Println(err)
	}
}