2021-07-31 22:45:49 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-08-02 21:21:12 +00:00
|
|
|
"context"
|
2021-08-03 22:40:33 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
2021-08-01 23:54:53 +00:00
|
|
|
|
2021-08-08 02:38:37 +00:00
|
|
|
"github.com/mediocregopher/blog.mediocregopher.com/srv/api"
|
|
|
|
"github.com/mediocregopher/blog.mediocregopher.com/srv/cfg"
|
2022-05-07 19:17:18 +00:00
|
|
|
cfgpkg "github.com/mediocregopher/blog.mediocregopher.com/srv/cfg"
|
2021-08-31 02:08:51 +00:00
|
|
|
"github.com/mediocregopher/blog.mediocregopher.com/srv/chat"
|
2021-08-02 21:21:12 +00:00
|
|
|
"github.com/mediocregopher/blog.mediocregopher.com/srv/mailinglist"
|
2021-08-01 23:54:53 +00:00
|
|
|
"github.com/mediocregopher/blog.mediocregopher.com/srv/pow"
|
2021-08-02 21:21:12 +00:00
|
|
|
"github.com/mediocregopher/mediocre-go-lib/v2/mctx"
|
|
|
|
"github.com/mediocregopher/mediocre-go-lib/v2/mlog"
|
2021-08-31 02:08:51 +00:00
|
|
|
"github.com/mediocregopher/radix/v4"
|
2021-08-01 23:54:53 +00:00
|
|
|
"github.com/tilinna/clock"
|
2021-07-31 22:45:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2021-08-02 21:21:12 +00:00
|
|
|
|
2021-08-03 22:40:33 +00:00
|
|
|
ctx := context.Background()
|
2022-05-06 02:17:59 +00:00
|
|
|
|
2022-05-07 19:17:18 +00:00
|
|
|
cfg := cfg.NewBlogCfg(cfg.Params{})
|
2021-08-03 22:40:33 +00:00
|
|
|
|
2022-05-07 19:17:18 +00:00
|
|
|
var dataDir cfgpkg.DataDir
|
|
|
|
dataDir.SetupCfg(cfg)
|
|
|
|
defer dataDir.Close()
|
|
|
|
ctx = mctx.WithAnnotator(ctx, &dataDir)
|
2021-08-02 21:21:12 +00:00
|
|
|
|
2021-08-08 02:38:37 +00:00
|
|
|
var powMgrParams pow.ManagerParams
|
|
|
|
powMgrParams.SetupCfg(cfg)
|
|
|
|
ctx = mctx.WithAnnotator(ctx, &powMgrParams)
|
2021-08-03 21:20:32 +00:00
|
|
|
|
2021-08-08 02:38:37 +00:00
|
|
|
var mailerParams mailinglist.MailerParams
|
|
|
|
mailerParams.SetupCfg(cfg)
|
|
|
|
ctx = mctx.WithAnnotator(ctx, &mailerParams)
|
2021-08-01 23:54:53 +00:00
|
|
|
|
2021-08-08 02:38:37 +00:00
|
|
|
var mlParams mailinglist.Params
|
|
|
|
mlParams.SetupCfg(cfg)
|
|
|
|
ctx = mctx.WithAnnotator(ctx, &mlParams)
|
2021-08-02 21:21:12 +00:00
|
|
|
|
2021-08-08 02:38:37 +00:00
|
|
|
var apiParams api.Params
|
|
|
|
apiParams.SetupCfg(cfg)
|
|
|
|
ctx = mctx.WithAnnotator(ctx, &apiParams)
|
2021-08-01 23:54:53 +00:00
|
|
|
|
2021-08-31 02:08:51 +00:00
|
|
|
redisProto := cfg.String("redis-proto", "tcp", "Network protocol to connect to redis over, can be tcp or unix")
|
|
|
|
redisAddr := cfg.String("redis-addr", "127.0.0.1:6379", "Address redis is expected to listen on")
|
|
|
|
redisPoolSize := cfg.Int("redis-pool-size", 5, "Number of connections in the redis pool to keep")
|
|
|
|
|
|
|
|
chatGlobalRoomMaxMsgs := cfg.Int("chat-global-room-max-messages", 1000, "Maximum number of messages the global chat room can retain")
|
|
|
|
chatUserIDCalcSecret := cfg.String("chat-user-id-calc-secret", "", "Secret to use when calculating user ids")
|
|
|
|
|
2021-08-08 02:38:37 +00:00
|
|
|
// initialization
|
|
|
|
err := cfg.Init(ctx)
|
2021-08-01 23:54:53 +00:00
|
|
|
|
2021-08-08 02:38:37 +00:00
|
|
|
logger := mlog.NewLogger(nil)
|
|
|
|
defer logger.Close()
|
2021-08-03 22:28:22 +00:00
|
|
|
|
2021-08-08 02:38:37 +00:00
|
|
|
logger.Info(ctx, "process started")
|
|
|
|
defer logger.Info(ctx, "process exiting")
|
2021-08-03 21:20:32 +00:00
|
|
|
|
2021-08-01 23:54:53 +00:00
|
|
|
if err != nil {
|
2022-05-06 01:33:40 +00:00
|
|
|
logger.Fatal(ctx, "initializing", err)
|
2021-08-07 02:34:18 +00:00
|
|
|
}
|
2021-08-01 23:54:53 +00:00
|
|
|
|
2021-08-03 22:40:33 +00:00
|
|
|
ctx = mctx.Annotate(ctx,
|
2021-08-31 02:08:51 +00:00
|
|
|
"redisProto", *redisProto,
|
|
|
|
"redisAddr", *redisAddr,
|
|
|
|
"redisPoolSize", *redisPoolSize,
|
|
|
|
"chatGlobalRoomMaxMsgs", *chatGlobalRoomMaxMsgs,
|
2021-08-02 21:21:12 +00:00
|
|
|
)
|
|
|
|
|
2021-08-01 23:54:53 +00:00
|
|
|
clock := clock.Realtime()
|
|
|
|
|
|
|
|
powStore := pow.NewMemoryStore(clock)
|
|
|
|
defer powStore.Close()
|
|
|
|
|
2021-08-08 02:38:37 +00:00
|
|
|
powMgrParams.Store = powStore
|
|
|
|
powMgrParams.Clock = clock
|
2021-07-31 22:45:49 +00:00
|
|
|
|
2021-08-08 02:38:37 +00:00
|
|
|
powMgr := pow.NewManager(powMgrParams)
|
2021-08-02 21:21:12 +00:00
|
|
|
|
2021-08-07 02:34:18 +00:00
|
|
|
var mailer mailinglist.Mailer
|
2021-08-08 02:38:37 +00:00
|
|
|
if mailerParams.SMTPAddr == "" {
|
2021-08-30 03:34:02 +00:00
|
|
|
logger.Info(ctx, "-smtp-addr not given, using a fake Mailer")
|
|
|
|
mailer = mailinglist.NewLogMailer(logger.WithNamespace("fake-mailer"))
|
2021-08-07 02:34:18 +00:00
|
|
|
} else {
|
2021-08-08 02:38:37 +00:00
|
|
|
mailer = mailinglist.NewMailer(mailerParams)
|
2021-08-07 02:34:18 +00:00
|
|
|
}
|
2021-08-02 21:21:12 +00:00
|
|
|
|
2022-05-07 19:17:18 +00:00
|
|
|
mlStore, err := mailinglist.NewStore(dataDir)
|
2021-08-02 21:21:12 +00:00
|
|
|
if err != nil {
|
2022-05-06 01:33:40 +00:00
|
|
|
logger.Fatal(ctx, "initializing mailing list storage", err)
|
2021-08-02 21:21:12 +00:00
|
|
|
}
|
|
|
|
defer mlStore.Close()
|
|
|
|
|
2021-08-08 02:38:37 +00:00
|
|
|
mlParams.Store = mlStore
|
|
|
|
mlParams.Mailer = mailer
|
|
|
|
mlParams.Clock = clock
|
2021-08-02 21:21:12 +00:00
|
|
|
|
2021-08-08 02:38:37 +00:00
|
|
|
ml := mailinglist.New(mlParams)
|
2021-08-04 23:12:16 +00:00
|
|
|
|
2021-08-31 02:08:51 +00:00
|
|
|
redis, err := (radix.PoolConfig{
|
|
|
|
Size: *redisPoolSize,
|
|
|
|
}).New(
|
|
|
|
ctx, *redisProto, *redisAddr,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
2022-05-06 01:33:40 +00:00
|
|
|
logger.Fatal(ctx, "initializing redis pool", err)
|
2021-08-31 02:08:51 +00:00
|
|
|
}
|
|
|
|
defer redis.Close()
|
|
|
|
|
|
|
|
chatGlobalRoom, err := chat.NewRoom(ctx, chat.RoomParams{
|
|
|
|
Logger: logger.WithNamespace("global-chat-room"),
|
|
|
|
Redis: redis,
|
|
|
|
ID: "global",
|
|
|
|
MaxMessages: *chatGlobalRoomMaxMsgs,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2022-05-06 01:33:40 +00:00
|
|
|
logger.Fatal(ctx, "initializing global chat room", err)
|
2021-08-31 02:08:51 +00:00
|
|
|
}
|
|
|
|
defer chatGlobalRoom.Close()
|
|
|
|
|
|
|
|
chatUserIDCalc := chat.NewUserIDCalculator([]byte(*chatUserIDCalcSecret))
|
|
|
|
|
2021-08-08 02:38:37 +00:00
|
|
|
apiParams.Logger = logger.WithNamespace("api")
|
|
|
|
apiParams.PowManager = powMgr
|
|
|
|
apiParams.MailingList = ml
|
2021-08-31 02:08:51 +00:00
|
|
|
apiParams.GlobalRoom = chatGlobalRoom
|
|
|
|
apiParams.UserIDCalculator = chatUserIDCalc
|
2021-07-31 22:45:49 +00:00
|
|
|
|
2021-08-02 21:21:12 +00:00
|
|
|
logger.Info(ctx, "listening")
|
2021-08-08 02:38:37 +00:00
|
|
|
a, err := api.New(apiParams)
|
2021-08-03 22:40:33 +00:00
|
|
|
if err != nil {
|
2022-05-06 01:33:40 +00:00
|
|
|
logger.Fatal(ctx, "initializing api", err)
|
2021-08-03 23:47:01 +00:00
|
|
|
}
|
2021-08-03 22:40:33 +00:00
|
|
|
defer func() {
|
2021-08-08 02:38:37 +00:00
|
|
|
shutdownCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
2021-08-03 22:40:33 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2021-08-08 02:38:37 +00:00
|
|
|
if err := a.Shutdown(shutdownCtx); err != nil {
|
2022-05-06 01:33:40 +00:00
|
|
|
logger.Fatal(ctx, "shutting down api", err)
|
2021-08-03 22:40:33 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-08-08 02:38:37 +00:00
|
|
|
// wait
|
|
|
|
|
2022-05-06 01:33:40 +00:00
|
|
|
sigCh := make(chan os.Signal, 1)
|
2021-08-03 22:40:33 +00:00
|
|
|
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
<-sigCh
|
|
|
|
|
|
|
|
// let the defers begin
|
2021-07-31 22:45:49 +00:00
|
|
|
}
|