diff options
| author | Xe Iaso <me@xeiaso.net> | 2024-04-05 11:52:33 -0400 |
|---|---|---|
| committer | Xe Iaso <me@xeiaso.net> | 2024-04-05 11:52:49 -0400 |
| commit | 91b2cdc4f2747f730a493b23f2362701eaefa066 (patch) | |
| tree | b190ee2088c7f9122b1594112dc5ed93cd54a644 /cmd | |
| parent | 0b484d1f236dadde16d84563103e1ba53879e0ee (diff) | |
| download | x-91b2cdc4f2747f730a493b23f2362701eaefa066.tar.xz x-91b2cdc4f2747f730a493b23f2362701eaefa066.zip | |
proto: add mi types
Signed-off-by: Xe Iaso <me@xeiaso.net>
Diffstat (limited to 'cmd')
| -rw-r--r-- | cmd/mimi/fly.toml | 19 | ||||
| -rw-r--r-- | cmd/mimi/main.go | 10 | ||||
| -rw-r--r-- | cmd/mimi/modules/discord/flyio/flyio.go | 11 | ||||
| -rw-r--r-- | cmd/mimi/modules/discord/flyio/statuspage.go | 75 |
4 files changed, 112 insertions, 3 deletions
diff --git a/cmd/mimi/fly.toml b/cmd/mimi/fly.toml index f595925..5e00acf 100644 --- a/cmd/mimi/fly.toml +++ b/cmd/mimi/fly.toml @@ -12,3 +12,22 @@ vm.size = "shared-cpu-2x" [build] image = "registry.fly.io/mimi:latest" + +[[services]] +protocol = "tcp" +internal_port = 9002 +processes = ["app"] + +[[services.ports]] +port = 80 +handlers = ["http"] +force_https = true + +[[services.ports]] +port = 443 +handlers = ["tls", "http"] + +[[services.tcp_checks]] +interval = "15s" +timeout = "2s" +grace_period = "1s" diff --git a/cmd/mimi/main.go b/cmd/mimi/main.go index 2ffb339..8a386ce 100644 --- a/cmd/mimi/main.go +++ b/cmd/mimi/main.go @@ -5,6 +5,7 @@ import ( "log" "log/slog" "net" + "net/http" "google.golang.org/grpc" "within.website/x/cmd/mimi/internal" @@ -15,6 +16,7 @@ import ( var ( grpcAddr = flag.String("grpc-addr", ":9001", "GRPC listen address") + httpAddr = flag.String("http-addr", ":9002", "HTTP listen address") ) func main() { @@ -43,9 +45,17 @@ func main() { scheduling.RegisterSchedulingServer(gs, scheduling.New()) + mux := http.NewServeMux() + + b.RegisterHTTP(mux) + go func() { log.Fatal(gs.Serve(lis)) }() + go func() { + log.Fatal(http.ListenAndServe(*httpAddr, mux)) + }() + <-ctx.Done() } diff --git a/cmd/mimi/modules/discord/flyio/flyio.go b/cmd/mimi/modules/discord/flyio/flyio.go index e9a8d23..98fd837 100644 --- a/cmd/mimi/modules/discord/flyio/flyio.go +++ b/cmd/mimi/modules/discord/flyio/flyio.go @@ -15,7 +15,9 @@ import ( ) var ( - flyDiscordGuild = flag.String("fly-discord-guild", "1194719413732130866", "fly discord guild ID") + flyDiscordGuild = flag.String("fly-discord-guild", "1194719413732130866", "fly discord guild ID") + flyDiscordXeID = flag.String("fly-discord-xe", "72838115944828928", "fly discord xe ID") + flyDiscordTeamID = flag.String("fly-discord-team", "1197660035212394657", "fly discord team ID") ) func p[T any](t T) *T { @@ -24,7 +26,7 @@ func p[T any](t T) *T { func mentionsXe(m *discordgo.MessageCreate) bool { for _, u := range m.Mentions { - if u.ID == "72838115944828928" { + if u.ID == *flyDiscordXeID { return true } } @@ -34,7 +36,7 @@ func mentionsXe(m *discordgo.MessageCreate) bool { func mentionsTeam(m *discordgo.MessageCreate) bool { for _, u := range m.MentionRoles { - if u == "1197660035212394657" { + if u == *flyDiscordTeamID { return true } } @@ -45,6 +47,7 @@ func mentionsTeam(m *discordgo.MessageCreate) bool { type Module struct { ola *ollama.Client model string + sess *discordgo.Session } func New() *Module { @@ -92,6 +95,8 @@ func (m *Module) judgeIfAboutFlyIO(ctx context.Context, msg string) (bool, error func (m *Module) Register(s *discordgo.Session) { s.AddHandler(m.HandleMentionFlyIO) s.AddHandler(m.ReactionAddFlyIO) + + m.sess = s } func (m *Module) scoldMessage(ctx context.Context) (string, error) { diff --git a/cmd/mimi/modules/discord/flyio/statuspage.go b/cmd/mimi/modules/discord/flyio/statuspage.go new file mode 100644 index 0000000..f238902 --- /dev/null +++ b/cmd/mimi/modules/discord/flyio/statuspage.go @@ -0,0 +1,75 @@ +package flyio + +import ( + "context" + "flag" + "fmt" + "log/slog" + "net/http" + "strings" + + "github.com/bwmarrin/discordgo" + "google.golang.org/protobuf/types/known/emptypb" + "within.website/x/proto/mimi/statuspage" +) + +var ( + statusChannelID = flag.String("fly-discord-status-channel", "1194719777625751573", "fly discord status channel ID") +) + +func (m *Module) RegisterHTTP(mux *http.ServeMux) { + mux.Handle(statuspage.UpdatePathPrefix, statuspage.NewUpdateServer(&UpdateService{Module: m})) +} + +type UpdateService struct { + *Module +} + +func (u *UpdateService) Poke(ctx context.Context, sp *statuspage.StatusUpdate) (*emptypb.Empty, error) { + go u.UpdateStatus(context.Background(), sp) + + return &emptypb.Empty{}, nil +} + +func (u *UpdateService) UpdateStatus(ctx context.Context, sp *statuspage.StatusUpdate) { + if sp.GetIncident() != nil { + u.UpdateIncident(ctx, sp.GetIncident()) + return + } + + u.UpdateComponent(ctx, sp) +} + +func (u *UpdateService) UpdateIncident(ctx context.Context, incident *statuspage.Incident) { + var sb strings.Builder + + fmt.Fprintf(&sb, "# %s\nimpact: %s\n", incident.GetName(), incident.GetImpact()) + fmt.Fprintf(&sb, "Follow the incident at %s\n", incident.GetShortlink()) + fmt.Fprintf(&sb, "Status: %s\n", incident.GetStatus()) + fmt.Fprintf(&sb, "Latest update:\n") + fmt.Fprintf(&sb, "At %s: \n%s\n\n", incident.GetIncidentUpdates()[0].GetCreatedAt(), incident.GetIncidentUpdates()[0].GetBody()) + + if _, err := u.sess.ChannelMessageSendEmbed(*statusChannelID, &discordgo.MessageEmbed{ + URL: incident.GetShortlink(), + Title: fmt.Sprintf("Incident %s: %s", incident.GetId(), incident.GetName()), + Description: sb.String(), + }, discordgo.WithContext(ctx)); err != nil { + slog.Error("failed to send incident update", "err", err, "incident", incident) + return + } +} + +func (u *UpdateService) UpdateComponent(ctx context.Context, sp *statuspage.StatusUpdate) { + var sb strings.Builder + + fmt.Fprintf(&sb, "# %s\nstatus: %s\n", sp.GetComponent().GetName(), sp.GetComponent().GetStatus()) + + if _, err := u.sess.ChannelMessageSendEmbed(*statusChannelID, &discordgo.MessageEmbed{ + URL: "https://status.flyio.net/", + Title: fmt.Sprintf("Component %s: %s", sp.GetComponent().GetId(), sp.GetComponent().GetName()), + Description: sb.String(), + }); err != nil { + slog.Error("failed to send component update", "err", err, "component", sp.GetComponent()) + return + } +} |
