-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
executable file
·161 lines (152 loc) · 3.82 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
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
package main
import (
"context"
"embed"
"flag"
"fmt"
"html/template"
"io"
"io/fs"
"log"
"net/http"
"os"
"os/signal"
"strings"
"time"
"github.com/IoIxD/dforum/database"
"github.com/diamondburned/arikawa/v3/gateway"
"github.com/diamondburned/arikawa/v3/state"
"github.com/diamondburned/arikawa/v3/utils/httputil/httpdriver"
"github.com/naoina/toml"
)
//go:embed resources
var embedfs embed.FS
type config struct {
BotToken string
ListenAddr string
Resources string
SiteURL string
ServiceName string
ServerHostedIn string
SitemapDir string
ReloadTemplates bool
TraceDiscordREST bool
Database string
}
type TraceClient struct {
httpdriver.Client
}
func (c TraceClient) Do(req httpdriver.Request) (httpdriver.Response, error) {
then := time.Now()
resp, err := c.Client.Do(req)
log.Printf("Discord REST: %s in %s", req.GetPath(), time.Since(then))
return resp, err
}
func main() {
cfgpath := flag.String("config", "config.toml", "path to config.toml")
flag.Parse()
file, err := os.ReadFile(*cfgpath)
if err != nil {
log.Fatalln("Error while reading config:", file)
}
config := config{ListenAddr: ":8084"}
if err := toml.Unmarshal(file, &config); err != nil {
log.Fatalln("Error while parsing config:", err)
}
if !strings.HasPrefix(config.Database, "postgres://") {
log.Fatalln("Config option 'Database' does not begin with postgres://", config.Database)
}
var fsys fs.FS
if config.Resources != "" {
fsys = os.DirFS(config.Resources)
} else {
config.ReloadTemplates = false
if fsys, err = fs.Sub(embedfs, "resources"); err != nil {
log.Fatalln("Error while using embedded resources:")
}
}
var tmplfn ExecuteTemplateFunc
if config.ReloadTemplates {
tmplfn = func(wr io.Writer, name string, data interface{}) error {
tmpl := template.New("")
tmpl.Funcs(funcMap)
_, err = tmpl.ParseFS(fsys, "templates/*")
if err != nil {
return err
}
tmpl.Funcs(funcMap)
return tmpl.ExecuteTemplate(wr, name, data)
}
} else {
tmpl := template.New("")
tmpl.Funcs(funcMap)
_, err = tmpl.ParseFS(fsys, "templates/*")
if err != nil {
log.Fatalln("Error parsing templates:", err)
}
tmplfn = tmpl.ExecuteTemplate
}
ctx, done := signal.NotifyContext(context.Background(), os.Interrupt)
defer done()
state := state.New("Bot " + config.BotToken)
if config.TraceDiscordREST {
state.Client.Client.Client = TraceClient{state.Client.Client.Client}
}
state.AddIntents(0 |
gateway.IntentGuildMessages |
gateway.IntentGuilds |
gateway.IntentGuildMembers,
)
db, err := database.OpenPostgres(config.Database)
if err != nil {
log.Fatalln("Opening database connection:", err)
}
server, err := newServer(state, fsys, db, config)
if err != nil {
fmt.Println(err)
return
}
ready, cancel := state.ChanFor(func(e interface{}) bool {
_, ok := e.(*gateway.ReadyEvent)
return ok
})
if err = state.Open(ctx); err != nil {
log.Fatalln("Error while opening gateway connection to Discord:", err)
}
self, err := state.Me()
if err != nil {
log.Fatalln("Error fetching self:", err)
}
select {
case <-ready:
case <-ctx.Done():
return
}
cancel()
go server.UpdateSitemap()
log.Printf("Connected to Discord as %s#%s (%s)\n", self.Username, self.Discriminator, self.ID)
server.executeTemplateFn = tmplfn
httpserver := &http.Server{
Addr: config.ListenAddr,
Handler: server,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
httperr := make(chan error, 1)
go func() {
httperr <- httpserver.ListenAndServe()
}()
select {
case <-ctx.Done():
done()
err := httpserver.Shutdown(context.Background())
if err != nil {
log.Fatalln("HTTP server shutdown:", err)
}
case err := <-httperr:
if err != nil {
log.Fatalln("HTTP server encountered error:", err)
}
}
}