aboutsummaryrefslogtreecommitdiff
path: root/cmd/mimi/main.go
blob: a5c844236b4b4f927067cd841c84fbca430d20fc (plain)
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
package main

import (
	"flag"
	"fmt"
	"log"
	"log/slog"
	"net"
	"net/http"

	"google.golang.org/grpc"
	"within.website/x/cmd/mimi/internal"
	"within.website/x/cmd/mimi/modules/discord"
	"within.website/x/cmd/mimi/modules/discord/flyio"
	"within.website/x/cmd/mimi/modules/discord/heic2jpeg"
	"within.website/x/cmd/mimi/modules/discord/jufra"
	"within.website/x/cmd/mimi/modules/irc"
)

var (
	grpcAddr   = flag.String("grpc-addr", ":9001", "GRPC listen address")
	httpAddr   = flag.String("http-addr", ":9002", "HTTP listen address")
	ircEnabled = flag.Bool("irc-enabled", true, "enable IRC module")
)

func main() {
	ctx, cancel := internal.HandleStartup()
	defer cancel()

	lis, err := net.Listen("tcp", *grpcAddr)
	if err != nil {
		log.Fatalf("can't open GRPC port %s: %v", *grpcAddr, err)
	}

	d, err := discord.New(ctx)
	if err != nil {
		log.Fatalf("error creating discord module: %v", err)
	}

	b := flyio.New()

	juf := jufra.New(d.Session())
	_ = juf

	d.Register(b)
	d.Register(heic2jpeg.New())

	d.Open()

	slog.Info("bot started", "grpcAddr", *grpcAddr, "httpAddr", *httpAddr)

	gs := grpc.NewServer()

	mux := http.NewServeMux()

	mux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintln(w, "OK")
	})

	b.RegisterHTTP(mux)

	if *ircEnabled {
		ircBot, err := irc.New(ctx, d.Session())
		if err != nil {
			log.Fatalf("error creating irc module: %v", err)
		}
		ircBot.RegisterHTTP(mux)
	}

	go func() {
		log.Fatal(gs.Serve(lis))
	}()

	go func() {
		log.Fatal(http.ListenAndServe(*httpAddr, mux))
	}()

	<-ctx.Done()
}