add redis process, put circus in charge of process management

This commit is contained in:
Brian Picciano 2021-08-06 20:34:18 -06:00
parent e6d607a248
commit dce39b836a
5 changed files with 90 additions and 33 deletions

2
.gitignore vendored
View File

@ -1,2 +1,2 @@
result
config.nix
prod.config.nix

View File

@ -1,9 +1,10 @@
{
powSecret = "";
powSecret = "ssshhh";
mlSMTPAddr = "";
mlSMTPAuth = "";
dataDir = ".";
dataDir = "/tmp/mediocre-blog-data";
publicURL = "http://localhost:4000";
listenProto = "tcp";
listenAddr = ":4000";
redisListenPath = "/tmp/mediocre-blog-redis";
}

View File

@ -3,11 +3,60 @@ let
pkgs = utils.pkgs;
system = utils.system;
in
rec {
srv = (import ./srv).build;
static = (import ./static).build;
{config ? ./config.nix}: rec {
config = (import ./config.nix);
static = (import ./static).build;
srv = (import ./srv).build;
srvBin = pkgs.writeScript "mediocregopher-mediocre-blog-srvBin" ''
#!/bin/sh
exec ${srv}/bin/mediocre-blog \
-pow-secret "${config.powSecret}" \
-ml-smtp-addr "${config.mlSMTPAddr}" \
-ml-smtp-auth "${config.mlSMTPAuth}" \
-data-dir "${config.dataDir}" \
-public-url "${config.publicURL}" \
-static-dir "${static}" \
-listen-proto "${config.listenProto}" \
-listen-addr "${config.listenAddr}"
'';
redisCfg = pkgs.writeText "mediocregopher-mediocre-blog-redisCfg" ''
port 0
unixsocket ${config.redisListenPath}
daemonize no
loglevel notice
logfile ""
appendonly yes
appendfilename "appendonly.aof"
dir ${config.dataDir}/redis
'';
redisBin = pkgs.writeScript "mediocregopher-mediocre-blog-redisBin" ''
#!/bin/sh
mkdir -p ${config.dataDir}/redis
exec ${pkgs.redis}/bin/redis-server ${redisCfg}
'';
circusCfg = pkgs.writeText "mediocregopher-mediocre-blog-circusCfg" ''
[circus]
endpoint = tcp://127.0.0.1:0
pubsub_endpoint = tcp://127.0.0.1:0
[watcher:srv]
cmd = ${srvBin}
numprocesses = 1
[watcher:redis]
cmd = ${redisBin}
numprocesses = 1
'';
circusBin = pkgs.writeScript "mediocregopher-mediocre-blog-circusBin" ''
exec ${pkgs.circus}/bin/circusd ${circusCfg}
'';
service = pkgs.writeText "mediocregopher-mediocre-blog" ''
[Unit]
Description=mediocregopher mediocre blog
@ -18,15 +67,7 @@ in
Restart=always
RestartSec=1s
User=mediocregopher
ExecStart=${srv}/bin/mediocre-blog \
-pow-secret "${config.powSecret}" \
-ml-smtp-addr "${config.mlSMTPAddr}" \
-ml-smtp-auth "${config.mlSMTPAuth}" \
-data-dir "${config.dataDir}" \
-public-url "${config.publicURL}" \
-static-dir "${static}" \
-listen-proto "${config.listenProto}" \
-listen-addr "${config.listenAddr}"
ExecStart=${circusBin}
[Install]
WantedBy=multi-user.target

View File

@ -62,10 +62,6 @@ func main() {
logger.Fatal(ctx, "-static-dir or -static-proxy-url is required")
case *powSecret == "":
logger.Fatal(ctx, "-pow-secret is required")
case *smtpAddr == "":
logger.Fatal(ctx, "-ml-smtp-addr is required")
case *smtpAuthStr == "":
logger.Fatal(ctx, "-ml-smtp-auth is required")
}
publicURL, err := url.Parse(*publicURLStr)
@ -87,14 +83,22 @@ func main() {
}
powTarget := uint32(powTargetUint)
var mailerCfg mailinglist.MailerParams
if *smtpAddr != "" {
mailerCfg.SMTPAddr = *smtpAddr
smtpAuthParts := strings.SplitN(*smtpAuthStr, ":", 2)
if len(smtpAuthParts) < 2 {
logger.Fatal(ctx, "invalid -ml-smtp-auth")
}
smtpAuth := sasl.NewPlainClient("", smtpAuthParts[0], smtpAuthParts[1])
smtpSendAs := smtpAuthParts[0]
mailerCfg.SMTPAuth = sasl.NewPlainClient("", smtpAuthParts[0], smtpAuthParts[1])
mailerCfg.SendAs = smtpAuthParts[0]
// initialization
ctx = mctx.Annotate(ctx,
"smtpAddr", mailerCfg.SMTPAddr,
"smtpSendAs", mailerCfg.SendAs,
)
}
ctx = mctx.Annotate(ctx,
"publicURL", publicURL.String(),
@ -102,10 +106,10 @@ func main() {
"listenAddr", *listenAddr,
"dataDir", *dataDir,
"powTarget", fmt.Sprintf("%x", powTarget),
"smtpAddr", *smtpAddr,
"smtpSendAs", smtpSendAs,
)
// initialization
if *staticDir != "" {
ctx = mctx.Annotate(ctx, "staticDir", *staticDir)
} else {
@ -127,11 +131,13 @@ func main() {
// sugar
requirePow := func(h http.Handler) http.Handler { return requirePowMiddleware(powMgr, h) }
mailer := mailinglist.NewMailer(mailinglist.MailerParams{
SMTPAddr: *smtpAddr,
SMTPAuth: smtpAuth,
SendAs: smtpSendAs,
})
var mailer mailinglist.Mailer
if *smtpAddr == "" {
logger.Info(ctx, "-smtp-addr not given, using NullMailer")
mailer = mailinglist.NullMailer
} else {
mailer = mailinglist.NewMailer(mailerCfg)
}
mlStore, err := mailinglist.NewStore(path.Join(*dataDir, "mailinglist.sqlite3"))
if err != nil {

View File

@ -10,6 +10,15 @@ type Mailer interface {
Send(to, subject, body string) error
}
// NullMailer acts as a Mailer but actually just does nothing.
var NullMailer = nullMailer{}
type nullMailer struct{}
func (nullMailer) Send(to, subject, body string) error {
return nil
}
// MailerParams are used to initialize a new Mailer instance
type MailerParams struct {
SMTPAddr string