|
| 1 | +package tailout |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log/slog" |
| 6 | + "net/http" |
| 7 | + |
| 8 | + "github.com/cterence/tailout/internal" |
| 9 | + "github.com/cterence/tailout/internal/views" |
| 10 | + "github.com/tailscale/tailscale-client-go/tailscale" |
| 11 | + |
| 12 | + "github.com/a-h/templ" |
| 13 | +) |
| 14 | + |
| 15 | +func (app *App) Ui(args []string) error { |
| 16 | + indexComponent := views.Index() |
| 17 | + app.Config.NonInteractive = true |
| 18 | + |
| 19 | + client, err := tailscale.NewClient(app.Config.Tailscale.APIKey, app.Config.Tailscale.Tailnet) |
| 20 | + if err != nil { |
| 21 | + return fmt.Errorf("failed to create tailscale client: %w", err) |
| 22 | + } |
| 23 | + |
| 24 | + http.Handle("/", templ.Handler(indexComponent)) |
| 25 | + |
| 26 | + http.HandleFunc("/create", func(w http.ResponseWriter, r *http.Request) { |
| 27 | + slog.Info("Creating tailout node") |
| 28 | + go func() { |
| 29 | + err := app.Create() |
| 30 | + if err != nil { |
| 31 | + slog.Error("failed to create node: " + err.Error()) |
| 32 | + } |
| 33 | + }() |
| 34 | + w.WriteHeader(http.StatusCreated) |
| 35 | + }) |
| 36 | + |
| 37 | + http.HandleFunc("/stop", func(w http.ResponseWriter, r *http.Request) { |
| 38 | + slog.Info("Stopping tailout nodes") |
| 39 | + app.Config.Stop.All = true |
| 40 | + go func() { |
| 41 | + err := app.Stop(nil) |
| 42 | + if err != nil { |
| 43 | + slog.Error("failed to create node: " + err.Error()) |
| 44 | + } |
| 45 | + }() |
| 46 | + w.WriteHeader(http.StatusNoContent) |
| 47 | + }) |
| 48 | + |
| 49 | + http.HandleFunc("/status", func(w http.ResponseWriter, r *http.Request) { |
| 50 | + nodes, err := internal.GetActiveNodes(client) |
| 51 | + if err != nil { |
| 52 | + slog.Error("failed to get active nodes: " + err.Error()) |
| 53 | + w.WriteHeader(http.StatusInternalServerError) |
| 54 | + return |
| 55 | + } |
| 56 | + table := "" |
| 57 | + for _, node := range nodes { |
| 58 | + table += fmt.Sprintf("<tr class=\"bg-white border-b\"><td class=\"px-4 py-2\">%s</td><td class=\"px-4 py-2\">%s</td><td class=\"px-4 py-2\">%s</td></tr>", node.Hostname, node.Addresses[0], node.LastSeen) |
| 59 | + } |
| 60 | + w.Write([]byte(table)) |
| 61 | + }) |
| 62 | + |
| 63 | + http.Handle("/health", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 64 | + w.Write([]byte(`{"status": {"server": "OK"}}`)) |
| 65 | + })) |
| 66 | + |
| 67 | + // Serve assets files |
| 68 | + http.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir("internal/assets")))) |
| 69 | + |
| 70 | + slog.Info("Listening on " + app.Config.Ui.Address + ":" + app.Config.Ui.Port) |
| 71 | + err = http.ListenAndServe(app.Config.Ui.Address+":"+app.Config.Ui.Port, nil) |
| 72 | + if err != nil { |
| 73 | + slog.Error("Failed to start server") |
| 74 | + panic(err) |
| 75 | + } |
| 76 | + return nil |
| 77 | +} |
0 commit comments