-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
101 lines (75 loc) · 2.29 KB
/
main.go
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
package main
import (
"context"
"embed"
"flag"
"log/slog"
"os"
"os/signal"
"syscall"
"github.com/prometheus/client_golang/prometheus"
"tailscale.com/hostinfo"
)
const BOARD_TITLE string = "tdiscuss - A Discussion Board for your Tailnet"
//go:embed tmpl/*.html
var templateFiles embed.FS
//go:embed static/style.css
var cssFile embed.FS
var (
hostname = flag.String("hostname", envOr("TSNET_HOSTNAME", "discuss"), "Hostname to use on your tailnet")
dataDir = flag.String("data-location", dataLocation(), "Configuration data location.")
debug = flag.Bool("debug", false, "Enable debug logging")
tsnetLog = flag.Bool("tsnet-log", false, "Enable tsnet logging")
version string = "dev"
gitSha string = "no-commit"
logLevel slog.Level = slog.LevelInfo
)
func main() {
flag.Parse()
hostinfo.SetApp("tdiscuss")
if *debug {
logLevel = slog.LevelDebug
}
logger := newLogger(os.Stdout, &logLevel)
logger.Info("starting tdiscuss", slog.String("version", version), slog.String("git_sha", gitSha))
versionGauge.With(prometheus.Labels{"version": version, "git_commit": gitSha, "hostname": *hostname}).Set(1)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
// Enabling logging within csrf.go
if *debug {
initCSRFLogger(logger.With("component", "csrf"))
}
dbconn, err := setupDatabase(ctx, logger)
if err != nil {
logger.Error("failed to connect to database", slog.String("error", err.Error()))
os.Exit(1)
}
defer dbconn.Close()
s := setupTsNetServer(logger)
defer s.Close()
tmpls := setupTemplates()
lc := getTailscaleLocalClient(s, logger)
queries := New(dbconn)
wrappedQueries := &QueriesWrapper{Queries: queries}
dsvc := NewDiscussService(
lc,
logger,
dbconn,
wrappedQueries,
tmpls,
*hostname,
version,
gitSha,
)
mux := setupMux(dsvc)
serverPlain := createHTTPServer(mux)
serverTls := createHTTPSServer(mux)
ln, tln := startListeners(s, logger)
defer ln.Close()
defer tln.Close()
go startServer(serverPlain, ln, logger, "http", *hostname)
go startServer(serverTls, tln, logger, "https", expandSNIName(ctx, lc, logger))
waitForShutdown(sigChan, ctx, logger, serverPlain, serverTls)
}