-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
151 lines (132 loc) · 3.2 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
package main
import (
"context"
"embed"
"log"
"net/http"
"os"
"github.com/BurntSushi/toml"
"github.com/caddyserver/certmagic"
feed "github.com/mmcdole/gofeed"
"github.com/nicksnyder/go-i18n/v2/i18n"
"github.com/ugorji/go/codec"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"golang.org/x/text/language"
smtp "github.com/xhit/go-simple-mail/v2"
)
//go:embed locales/*.toml
var localeFS embed.FS
type Configuration struct {
MongoURL string
MongoDBName string
ListenAddr string
BaseURL string
NotifyOldItems bool
LetsEncrypt struct {
Enable bool
Email string
Domains []string
}
EmailConfig struct {
Security smtp.Encryption
AuthenticationType smtp.AuthType
Port uint
FromAddr string
Hostname string
Username string
Password string
}
}
type app struct {
config *Configuration
codecHandle *codec.MsgpackHandle
i18nBundle *i18n.Bundle
emailClient *smtp.SMTPServer
feedParser *feed.Parser
conn *mongo.Client
database *mongo.Database
users *mongo.Collection
feeds *mongo.Collection
seenItems *mongo.Collection
}
func main() {
a := new(app)
{
cfgPath := os.Getenv("CONFIG_PATH")
if len(cfgPath) == 0 {
cfgPath = "config.toml"
}
data, err := os.ReadFile(cfgPath)
if err != nil {
panic(err)
}
c := new(Configuration)
toml.Unmarshal(data, c)
a.config = c
}
{
cl, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(a.config.MongoURL))
if err != nil {
panic(err)
}
err = cl.Ping(context.TODO(), nil)
if err != nil {
panic(err)
}
a.database = cl.Database(a.config.MongoDBName)
a.users = a.database.Collection("users")
a.feeds = a.database.Collection("feeds")
a.seenItems = a.database.Collection("seen_items")
}
{
msgpHandle := new(codec.MsgpackHandle)
msgpHandle.WriterBufferSize = 8192
msgpHandle.ReaderBufferSize = 8192
msgpHandle.WriteExt = true
a.codecHandle = msgpHandle
}
{
bundle := i18n.NewBundle(language.English)
bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal)
_, _ = bundle.LoadMessageFileFS(localeFS, "locales/en.toml")
_, _ = bundle.LoadMessageFileFS(localeFS, "locales/bn.toml")
a.i18nBundle = bundle
}
{
srv := smtp.NewSMTPClient()
srv.Authentication = a.config.EmailConfig.AuthenticationType
srv.Encryption = a.config.EmailConfig.Security
srv.Host = a.config.EmailConfig.Hostname
srv.Port = int(a.config.EmailConfig.Port)
srv.Username = a.config.EmailConfig.Username
srv.Password = a.config.EmailConfig.Password
a.emailClient = srv
}
{
a.feedParser = feed.NewParser()
}
{
err := a.EnsureIndexes()
if err != nil {
panic(err)
}
}
go a.notificationLoop()
log.Println("All initialized, listening.")
http.HandleFunc("/", a.handler)
if a.config.LetsEncrypt.Enable {
certmagic.DefaultACME.Agreed = true
certmagic.DefaultACME.Email = a.config.LetsEncrypt.Email
certmagic.DefaultACME.CA = certmagic.LetsEncryptProductionCA
err := certmagic.HTTPS(a.config.LetsEncrypt.Domains, http.DefaultServeMux)
if err != nil {
panic(err)
}
} else {
err := http.ListenAndServe(a.config.ListenAddr, nil)
if err != nil {
panic(err)
}
}
}