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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
package main
import (
"context"
"flag"
"fmt"
"log/slog"
"net"
"net/http"
"os"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/robfig/cron/v3"
"golang.org/x/sync/errgroup"
"google.golang.org/grpc"
"within.website/x/cmd/mi/models"
"within.website/x/cmd/mi/services/events"
"within.website/x/cmd/mi/services/glance"
"within.website/x/cmd/mi/services/homefrontshim"
"within.website/x/cmd/mi/services/importer"
"within.website/x/cmd/mi/services/posse"
"within.website/x/cmd/mi/services/switchtracker"
"within.website/x/cmd/mi/services/twitchevents"
"within.website/x/internal"
pb "within.website/x/proto/mi"
"within.website/x/proto/mimi/announce"
)
var (
bind = flag.String("bind", ":8080", "HTTP bind address")
dbLoc = flag.String("db-loc", "./var/data.db", "SQLite database location")
backupDBLoc = flag.String("backup-db-loc", "./var/data.db.backup", "backup SQLite database location")
grpcBind = flag.String("grpc-bind", ":8081", "GRPC bind address")
internalBind = flag.String("internal-bind", ":9195", "HTTP internal routes bind address")
// Events flags
flyghtTrackerURL = flag.String("flyght-tracker-url", "", "Flyght Tracker URL")
// POSSE flags
blueskyAuthkey = flag.String("bsky-authkey", "", "Bluesky authkey")
blueskyHandle = flag.String("bsky-handle", "", "Bluesky handle")
blueskyPDS = flag.String("bsky-pds", "https://bsky.social", "Bluesky PDS")
mastodonToken = flag.String("mastodon-token", "", "Mastodon token")
mastodonURL = flag.String("mastodon-url", "", "Mastodon URL")
mastodonUsername = flag.String("mastodon-username", "", "Mastodon username")
mimiAnnounceURL = flag.String("mimi-announce-url", "", "Mimi announce URL")
)
func main() {
internal.HandleStartup()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
slog.Info(
"starting up",
"bind", *bind,
"db-loc", *dbLoc,
"backup-db-loc", *backupDBLoc,
"internal-bind", *internalBind,
"bsky-handle", *blueskyHandle,
"bsky-pds", *blueskyPDS,
"mastodon-url", *mastodonURL,
"mastodon-username", *mastodonUsername,
"have-mimi-announce-url", *mimiAnnounceURL != "",
"have-flyght-tracker-url", *flyghtTrackerURL != "",
)
dao, err := models.New(*dbLoc, *backupDBLoc)
if err != nil {
slog.Error("failed to create dao", "err", err)
os.Exit(1)
}
mux := http.NewServeMux()
ann, err := posse.New(ctx, dao, posse.Config{
BlueskyAuthkey: *blueskyAuthkey,
BlueskyHandle: *blueskyHandle,
BlueskyPDS: *blueskyPDS,
MastodonToken: *mastodonToken,
MastodonURL: *mastodonURL,
MimiAnnounceURL: *mimiAnnounceURL,
})
if err != nil {
slog.Error("failed to create announcer", "err", err)
os.Exit(1)
}
te, err := twitchevents.New(ctx, dao, twitchevents.Config{
BlueskyAuthkey: *blueskyAuthkey,
BlueskyHandle: *blueskyHandle,
BlueskyPDS: *blueskyPDS,
MastodonToken: *mastodonToken,
MastodonURL: *mastodonURL,
MimiAnnounceURL: *mimiAnnounceURL,
})
if err != nil {
slog.Error("failed to create twitch events", "err", err)
os.Exit(1)
}
st := switchtracker.New(dao)
es := events.New(dao, *flyghtTrackerURL)
gs := grpc.NewServer()
announce.RegisterAnnounceServer(gs, ann)
pb.RegisterSwitchTrackerServer(gs, st)
pb.RegisterEventsServer(gs, es)
mux.Handle(announce.AnnouncePathPrefix, announce.NewAnnounceServer(ann))
mux.Handle(pb.SwitchTrackerPathPrefix, pb.NewSwitchTrackerServer(st))
mux.Handle(pb.EventsPathPrefix, pb.NewEventsServer(es))
mux.Handle("/front", homefrontshim.New(dao))
mux.Handle("/twitch", te)
mux.Handle("/glance", glance.New(dao))
i := importer.New(dao)
i.Mount(http.DefaultServeMux)
mux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
if err := dao.Ping(r.Context()); err != nil {
slog.Error("database not healthy", "err", err)
http.Error(w, "database not healthy", http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, "OK")
})
http.Handle("/metrics", promhttp.Handler())
g, _ := errgroup.WithContext(ctx)
g.Go(func() error {
slog.Info("starting private grpc server", "bind", *grpcBind)
lis, err := net.Listen("tcp", *grpcBind)
if err != nil {
return err
}
return gs.Serve(lis)
})
g.Go(func() error {
slog.Info("starting internal server", "bind", *internalBind)
return http.ListenAndServe(*internalBind, nil)
})
g.Go(func() error {
slog.Info("starting server", "bind", *bind)
return http.ListenAndServe(*bind, mux)
})
g.Go(func() error {
<-ctx.Done()
return ctx.Err()
})
g.Go(func() error {
c := cron.New()
c.AddFunc("@every 1h", dao.Backup)
c.Run()
return nil
})
if err := g.Wait(); err != nil {
slog.Error("error doing work", "err", err)
os.Exit(1)
}
}
|