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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
package main
import (
_ "embed"
"encoding/json"
"flag"
"fmt"
"log/slog"
"net/http"
"os"
"os/signal"
"github.com/peterbourgon/ff/v3"
"go.jolheiser.com/cfg"
"go.jolheiser.com/cfg-playground/static"
"go.jolheiser.com/ffjsonnet"
"go.jolheiser.com/tailroute"
)
//go:embed static/index.html
var indexHTML []byte
type args struct {
hostname string
dataDir string
authKey string
verbose bool
}
func maine() error {
args := args{
hostname: "cfg",
dataDir: ".tsnet",
}
fs := flag.NewFlagSet("cfg-playground", flag.ExitOnError)
fs.StringVar(&args.hostname, "hostname", args.hostname, "Tailnet hostname")
fs.StringVar(&args.hostname, "h", args.hostname, "--hostname")
fs.StringVar(&args.dataDir, "data-dir", args.dataDir, "tsnet data directory")
fs.StringVar(&args.dataDir, "d", args.dataDir, "--data-dir")
fs.StringVar(&args.authKey, "auth-key", args.authKey, "tsnet auth key")
fs.StringVar(&args.authKey, "a", args.authKey, "--auth-key")
fs.BoolVar(&args.verbose, "verbose", args.verbose, "Log verbosely")
fs.BoolVar(&args.verbose, "v", args.verbose, "--verbose")
fs.String("config", "cfg.jsonnet", "Config file")
if err := ff.Parse(fs, os.Args[1:],
ff.WithEnvVarPrefix("CFG_PLAYGROUND"),
ff.WithConfigFileFlag("config"),
ff.WithAllowMissingConfigFile(true),
ff.WithConfigFileParser(ffjsonnet.Parser),
); err != nil {
return err
}
if args.verbose {
slog.SetLogLoggerLevel(slog.LevelDebug)
}
if args.authKey != "" {
os.Setenv("TS_AUTHKEY", args.authKey)
}
mux := http.NewServeMux()
mux.HandleFunc("GET /", index)
mux.HandleFunc("GET /tailwind.css", static.TailwindHandler)
mux.HandleFunc("POST /convert", convert)
tr := tailroute.Router{
Tailnet: mux,
}
go func() {
fmt.Printf("Tailnet listening on http://%s\n", args.hostname)
if err := tr.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 index(w http.ResponseWriter, r *http.Request) {
if _, err := w.Write(indexHTML); err != nil {
slog.Debug("could not write output", slog.Any("error", err))
return
}
}
func convert(w http.ResponseWriter, r *http.Request) {
payload := struct {
Input string
From string
To string
}{}
if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
slog.Debug("could not decode JSON body", slog.Any("error", err))
return
}
defer r.Body.Close()
inputFormat, err := cfg.ParseEncoding(payload.From)
if err != nil {
slog.Debug("could not parse input encoding", slog.Any("error", err))
return
}
outputFormat, err := cfg.ParseEncoding(payload.To)
if err != nil {
slog.Debug("could not parse output encoding", slog.Any("error", err))
return
}
var data any
if err := inputFormat.Unmarshal([]byte(payload.Input), &data); err != nil {
slog.Debug("could not unmarshal input", slog.Any("error", err))
return
}
out, err := outputFormat.Marshal(data)
if err != nil {
slog.Debug("could not marshal output", slog.Any("error", err))
return
}
if _, err := w.Write(out); err != nil {
slog.Debug("could not write output", slog.Any("error", err))
return
}
}
func main() {
if err := maine(); err != nil {
slog.Error("error running cfg-playground", slog.Any("error", err))
}
}
|