Home

tailpolicy @d4fcd34d1604e92d6f5ad6ba3765233024dd110b - 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
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
package main

import (
	_ "embed"
	"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"
)

var (
	//go:embed static/prism.js
	prismJS []byte
	//go:embed static/prism.css
	prismCSS []byte
	//go:embed static/ctp.css
	ctpCSS []byte
)

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)
	mux.HandleFunc("/prism.js", func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "text/javascript")
		w.Write(prismJS)
	})
	mux.HandleFunc("/prism.css", func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "text/css")
		w.Write(prismCSS)
	})
	mux.HandleFunc("/ctp.css", func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "text/css")
		w.Write(ctpCSS)
	})
	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.RepoOrigin,
		}); 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)
	}
}