Skip to content

Commit b51ae17

Browse files
committed
fix(mi): make old routes work to avoid breaking tools
Signed-off-by: Xe Iaso <[email protected]>
1 parent 4f823b4 commit b51ae17

File tree

2 files changed

+64
-39
lines changed

2 files changed

+64
-39
lines changed

cmd/mi/main.go

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"net"
99
"net/http"
1010
"os"
11+
"strings"
1112

1213
"github.com/prometheus/client_golang/prometheus/promhttp"
1314
"github.com/robfig/cron/v3"
@@ -37,13 +38,18 @@ var (
3738
flyghtTrackerURL = flag.String("flyght-tracker-url", "", "Flyght Tracker URL")
3839

3940
// POSSE flags
40-
blueskyAuthkey = flag.String("bsky-authkey", "", "Bluesky authkey")
41-
blueskyHandle = flag.String("bsky-handle", "", "Bluesky handle")
42-
blueskyPDS = flag.String("bsky-pds", "https://bsky.social", "Bluesky PDS")
43-
mastodonToken = flag.String("mastodon-token", "", "Mastodon token")
44-
mastodonURL = flag.String("mastodon-url", "", "Mastodon URL")
45-
mastodonUsername = flag.String("mastodon-username", "", "Mastodon username")
46-
mimiAnnounceURL = flag.String("mimi-announce-url", "", "Mimi announce URL")
41+
blueskyAuthkey = flag.String("bsky-authkey", "", "Bluesky authkey")
42+
blueskyHandle = flag.String("bsky-handle", "", "Bluesky handle")
43+
blueskyPDS = flag.String("bsky-pds", "https://bsky.social", "Bluesky PDS")
44+
mastodonToken = flag.String("mastodon-token", "", "Mastodon token")
45+
mastodonURL = flag.String("mastodon-url", "", "Mastodon URL")
46+
mastodonUsername = flag.String("mastodon-username", "", "Mastodon username")
47+
mimiAnnounceURL = flag.String("mimi-announce-url", "", "Mimi announce URL")
48+
twitchClientID = flag.String("twitch-client-id", "", "twitch.tv client ID")
49+
twitchClientSecret = flag.String("twitch-client-secret", "", "twitch.tv client secret")
50+
twitchUserID = flag.Int("twitch-user-id", 105794391, "twitch.tv user ID")
51+
twitchWebhookSecret = flag.String("twitch-webhook-secret", "", "twitch.tv webhook secret")
52+
twitchWebhookURL = flag.String("twitch-webhook-url", "", "URL for Twitch events to be pushed to")
4753
)
4854

4955
func main() {
@@ -86,17 +92,21 @@ func main() {
8692
os.Exit(1)
8793
}
8894

89-
te, err := twitchevents.New(ctx, dao, twitchevents.Config{
90-
BlueskyAuthkey: *blueskyAuthkey,
91-
BlueskyHandle: *blueskyHandle,
92-
BlueskyPDS: *blueskyPDS,
93-
MastodonToken: *mastodonToken,
94-
MastodonURL: *mastodonURL,
95-
MimiAnnounceURL: *mimiAnnounceURL,
96-
})
97-
if err != nil {
98-
slog.Error("failed to create twitch events", "err", err)
99-
os.Exit(1)
95+
if *twitchClientID != "" {
96+
te, err := twitchevents.New(ctx, dao, twitchevents.Config{
97+
BlueskyAuthkey: *blueskyAuthkey,
98+
BlueskyHandle: *blueskyHandle,
99+
BlueskyPDS: *blueskyPDS,
100+
MastodonToken: *mastodonToken,
101+
MastodonURL: *mastodonURL,
102+
MimiAnnounceURL: *mimiAnnounceURL,
103+
})
104+
if err != nil {
105+
slog.Error("failed to create twitch events", "err", err)
106+
os.Exit(1)
107+
}
108+
109+
mux.Handle("/twitch", te)
100110
}
101111

102112
st := switchtracker.New(dao)
@@ -110,9 +120,26 @@ func main() {
110120

111121
mux.Handle(announcev1.AnnouncePathPrefix, announcev1.NewAnnounceServer(ann))
112122
mux.Handle(pb.SwitchTrackerPathPrefix, pb.NewSwitchTrackerServer(st))
123+
124+
// XXX(Xe): shim through old paths
125+
mux.HandleFunc("/twirp/within.website.x.mi.SwitchTracker/", func(w http.ResponseWriter, r *http.Request) {
126+
slog.Info("got here", "path", r.URL.Path)
127+
r.URL.Path = strings.Replace(r.URL.Path, "/twirp/within.website.x.mi.SwitchTracker/", "/twirp/within.website.x.mi.v1.SwitchTracker/", -1)
128+
slog.Info("got here", "path", r.URL.Path)
129+
pb.NewSwitchTrackerServer(st).ServeHTTP(w, r)
130+
})
131+
113132
mux.Handle(pb.EventsPathPrefix, pb.NewEventsServer(es))
133+
134+
// XXX(Xe): shim through old paths
135+
mux.HandleFunc("/twirp/within.website.x.mi.Events/", func(w http.ResponseWriter, r *http.Request) {
136+
slog.Info("got here", "path", r.URL.Path)
137+
r.URL.Path = strings.Replace(r.URL.Path, "/twirp/within.website.x.mi.Events/", "/twirp/within.website.x.mi.v1.Events/", -1)
138+
slog.Info("got here", "path", r.URL.Path)
139+
pb.NewEventsServer(es).ServeHTTP(w, r)
140+
})
141+
114142
mux.Handle("/front", homefrontshim.New(dao))
115-
mux.Handle("/twitch", te)
116143
mux.Handle("/glance", glance.New(dao))
117144

118145
i := importer.New(dao)

cmd/mi/services/twitchevents/twitchevents.go

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"context"
66
"encoding/json"
7-
"flag"
87
"fmt"
98
"io"
109
"log/slog"
@@ -33,21 +32,20 @@ var (
3332
Name: "mi_twitch_events_count",
3433
Help: "Number of twitch events ever processed",
3534
}, []string{"messageType"})
36-
37-
twitchClientID = flag.String("twitch-client-id", "", "twitch.tv client ID")
38-
twitchClientSecret = flag.String("twitch-client-secret", "", "twitch.tv client secret")
39-
twitchUserID = flag.Int("twitch-user-id", 105794391, "twitch.tv user ID")
40-
twitchWebhookSecret = flag.String("twitch-webhook-secret", "", "twitch.tv webhook secret")
41-
twitchWebhookURL = flag.String("twitch-webhook-url", "", "URL for Twitch events to be pushed to")
4235
)
4336

4437
type Config struct {
45-
BlueskyAuthkey string
46-
BlueskyHandle string
47-
BlueskyPDS string
48-
MastodonToken string
49-
MastodonURL string
50-
MimiAnnounceURL string
38+
BlueskyAuthkey string
39+
BlueskyHandle string
40+
BlueskyPDS string
41+
MastodonToken string
42+
MastodonURL string
43+
MimiAnnounceURL string
44+
TwitchClientID string
45+
TwitchClientSecret string
46+
TwitchUserID int
47+
TwitchWebhookSecret string
48+
TwitchWebhookURL string
5149
}
5250

5351
func (c Config) BlueskyAgent(ctx context.Context) (*bsky.BskyAgent, error) {
@@ -75,8 +73,8 @@ type Server struct {
7573

7674
func New(ctx context.Context, dao *models.DAO, cfg Config) (*Server, error) {
7775
twitch, err := helix.NewClient(&helix.Options{
78-
ClientID: *twitchClientID,
79-
ClientSecret: *twitchClientSecret,
76+
ClientID: cfg.TwitchClientID,
77+
ClientSecret: cfg.TwitchClientSecret,
8078
})
8179

8280
if err != nil {
@@ -125,7 +123,7 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
125123
return
126124
}
127125

128-
if !helix.VerifyEventSubNotification(*twitchWebhookSecret, r.Header, string(data)) {
126+
if !helix.VerifyEventSubNotification(s.cfg.TwitchWebhookSecret, r.Header, string(data)) {
129127
slog.Error("can't verify event", "err", "invalid secret")
130128
http.Error(w, "no auth", http.StatusUnauthorized)
131129
return
@@ -174,7 +172,7 @@ func (s *Server) maybeCreateWebhookSubscription() error {
174172

175173
found := false
176174
for _, sub := range subs.Data.EventSubSubscriptions {
177-
if sub.Transport.Callback == *twitchWebhookURL {
175+
if sub.Transport.Callback == s.cfg.TwitchWebhookURL {
178176
slog.Info("no need to resubscribe, webhook URL was found")
179177
found = true
180178
break
@@ -189,12 +187,12 @@ func (s *Server) maybeCreateWebhookSubscription() error {
189187
Type: "stream.online",
190188
Version: "1",
191189
Condition: helix.EventSubCondition{
192-
BroadcasterUserID: strconv.Itoa(*twitchUserID),
190+
BroadcasterUserID: strconv.Itoa(s.cfg.TwitchUserID),
193191
},
194192
Transport: helix.EventSubTransport{
195193
Method: "webhook",
196-
Callback: *twitchWebhookURL,
197-
Secret: *twitchWebhookSecret,
194+
Callback: s.cfg.TwitchWebhookURL,
195+
Secret: s.cfg.TwitchWebhookSecret,
198196
},
199197
}); err != nil {
200198
return fmt.Errorf("can't create subscription: %w", err)

0 commit comments

Comments
 (0)