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
|
package main
import (
"context"
"os"
"os/signal"
"syscall"
"time"
"github.com/mediocregopher/blog.mediocregopher.com/srv/cache"
cfgpkg "github.com/mediocregopher/blog.mediocregopher.com/srv/cfg"
"github.com/mediocregopher/blog.mediocregopher.com/srv/gmi"
"github.com/mediocregopher/blog.mediocregopher.com/srv/http"
"github.com/mediocregopher/blog.mediocregopher.com/srv/mailinglist"
"github.com/mediocregopher/blog.mediocregopher.com/srv/post"
"github.com/mediocregopher/blog.mediocregopher.com/srv/pow"
"github.com/mediocregopher/mediocre-go-lib/v2/mctx"
"github.com/mediocregopher/mediocre-go-lib/v2/mlog"
"github.com/tilinna/clock"
)
func main() {
ctx := context.Background()
cfg := cfgpkg.NewBlogCfg(cfgpkg.Params{})
var dataDir cfgpkg.DataDir
dataDir.SetupCfg(cfg)
defer dataDir.Close()
ctx = mctx.WithAnnotator(ctx, &dataDir)
var powMgrParams pow.ManagerParams
powMgrParams.SetupCfg(cfg)
ctx = mctx.WithAnnotator(ctx, &powMgrParams)
var mailerParams mailinglist.MailerParams
mailerParams.SetupCfg(cfg)
ctx = mctx.WithAnnotator(ctx, &mailerParams)
var mlParams mailinglist.Params
mlParams.SetupCfg(cfg)
ctx = mctx.WithAnnotator(ctx, &mlParams)
var httpParams http.Params
httpParams.SetupCfg(cfg)
ctx = mctx.WithAnnotator(ctx, &httpParams)
var gmiParams gmi.Params
gmiParams.SetupCfg(cfg)
ctx = mctx.WithAnnotator(ctx, &gmiParams)
// initialization
err := cfg.Init(ctx)
logger := mlog.NewLogger(nil)
defer logger.Close()
logger.Info(ctx, "process started")
defer logger.Info(ctx, "process exiting")
if err != nil {
logger.Fatal(ctx, "initializing", err)
}
clock := clock.Realtime()
powStore := pow.NewMemoryStore(clock)
defer powStore.Close()
powMgrParams.Store = powStore
powMgrParams.Clock = clock
powMgr := pow.NewManager(powMgrParams)
var mailer mailinglist.Mailer
if mailerParams.SMTPAddr == "" {
logger.Info(ctx, "-smtp-addr not given, using a fake Mailer")
mailer = mailinglist.NewLogMailer(logger.WithNamespace("fake-mailer"))
} else {
mailer = mailinglist.NewMailer(mailerParams)
}
mlStore, err := mailinglist.NewStore(dataDir)
if err != nil {
logger.Fatal(ctx, "initializing mailing list storage", err)
}
defer mlStore.Close()
mlParams.Store = mlStore
mlParams.Mailer = mailer
mlParams.Clock = clock
ml := mailinglist.New(mlParams)
postSQLDB, err := post.NewSQLDB(dataDir)
if err != nil {
logger.Fatal(ctx, "initializing sql db for post data", err)
}
defer postSQLDB.Close()
postStore := post.NewStore(postSQLDB)
postAssetStore := post.NewAssetStore(postSQLDB)
postDraftStore := post.NewDraftStore(postSQLDB)
cache := cache.New(5000)
httpParams.Logger = logger.WithNamespace("http")
httpParams.Cache = cache
httpParams.PowManager = powMgr
httpParams.PostStore = postStore
httpParams.PostAssetStore = postAssetStore
httpParams.PostDraftStore = postDraftStore
httpParams.MailingList = ml
httpParams.GeminiPublicURL = gmiParams.PublicURL
logger.Info(ctx, "starting http api")
httpAPI, err := http.New(httpParams)
if err != nil {
logger.Fatal(ctx, "starting http api", err)
}
defer func() {
shutdownCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
if err := httpAPI.Shutdown(shutdownCtx); err != nil {
logger.Fatal(ctx, "shutting down http api", err)
}
}()
gmiParams.Logger = logger.WithNamespace("gmi")
gmiParams.Cache = cache
gmiParams.PostStore = postStore
gmiParams.PostAssetStore = postAssetStore
gmiParams.HTTPPublicURL = httpParams.PublicURL
logger.Info(ctx, "starting gmi api")
gmiAPI, err := gmi.New(gmiParams)
if err != nil {
logger.Fatal(ctx, "starting gmi api", err)
}
defer func() {
shutdownCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
if err := gmiAPI.Shutdown(shutdownCtx); err != nil {
logger.Fatal(ctx, "shutting down gmi api", err)
}
}()
// wait
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
<-sigCh
// let the defers begin
}
|