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 result
config.nix prod.config.nix

View File

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

View File

@ -3,11 +3,60 @@ let
pkgs = utils.pkgs; pkgs = utils.pkgs;
system = utils.system; system = utils.system;
in in
rec { {config ? ./config.nix}: rec {
srv = (import ./srv).build;
static = (import ./static).build;
config = (import ./config.nix); 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" '' service = pkgs.writeText "mediocregopher-mediocre-blog" ''
[Unit] [Unit]
Description=mediocregopher mediocre blog Description=mediocregopher mediocre blog
@ -18,15 +67,7 @@ in
Restart=always Restart=always
RestartSec=1s RestartSec=1s
User=mediocregopher User=mediocregopher
ExecStart=${srv}/bin/mediocre-blog \ ExecStart=${circusBin}
-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}"
[Install] [Install]
WantedBy=multi-user.target WantedBy=multi-user.target

View File

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

View File

@ -10,6 +10,15 @@ type Mailer interface {
Send(to, subject, body string) error 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 // MailerParams are used to initialize a new Mailer instance
type MailerParams struct { type MailerParams struct {
SMTPAddr string SMTPAddr string