diff options
Diffstat (limited to 'srv/src/cmd')
-rw-r--r-- | srv/src/cmd/mailinglist-cli/main.go | 120 | ||||
-rw-r--r-- | srv/src/cmd/mediocre-blog/main.go | 160 | ||||
-rw-r--r-- | srv/src/cmd/userid-calc-cli/main.go | 28 |
3 files changed, 308 insertions, 0 deletions
diff --git a/srv/src/cmd/mailinglist-cli/main.go b/srv/src/cmd/mailinglist-cli/main.go new file mode 100644 index 0000000..c3207df --- /dev/null +++ b/srv/src/cmd/mailinglist-cli/main.go @@ -0,0 +1,120 @@ +package main + +import ( + "context" + "errors" + "io" + "path" + + "github.com/mediocregopher/blog.mediocregopher.com/srv/cfg" + "github.com/mediocregopher/blog.mediocregopher.com/srv/mailinglist" + "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 := cfg.New(cfg.Params{ + EnvPrefix: "MEDIOCRE_BLOG", + }) + + dataDir := cfg.String("data-dir", ".", "Directory to use for long term storage") + + var mailerParams mailinglist.MailerParams + mailerParams.SetupCfg(cfg) + ctx = mctx.WithAnnotator(ctx, &mailerParams) + + var mlParams mailinglist.Params + mlParams.SetupCfg(cfg) + ctx = mctx.WithAnnotator(ctx, &mlParams) + + // 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() + + var mailer mailinglist.Mailer + if mailerParams.SMTPAddr == "" { + logger.Info(ctx, "-smtp-addr not given, using NullMailer") + mailer = mailinglist.NullMailer + } else { + mailer = mailinglist.NewMailer(mailerParams) + } + + mailingListDBFile := path.Join(*dataDir, "mailinglist.sqlite3") + ctx = mctx.Annotate(ctx, "mailingListDBFile", mailingListDBFile) + + mlStore, err := mailinglist.NewStore(mailingListDBFile) + 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) + + subCmd := cfg.SubCmd() + ctx = mctx.Annotate(ctx, "subCmd", subCmd) + + switch subCmd { + + case "list": + + for it := mlStore.GetAll(); ; { + email, err := it() + if errors.Is(err, io.EOF) { + break + } else if err != nil { + logger.Fatal(ctx, "retrieving next email", err) + } + + ctx := mctx.Annotate(context.Background(), + "email", email.Email, + "createdAt", email.CreatedAt, + "verifiedAt", email.VerifiedAt, + ) + + logger.Info(ctx, "next") + } + + case "publish": + + title := cfg.String("title", "", "Title of the post which was published") + url := cfg.String("url", "", "URL of the post which was published") + + if err := cfg.Init(ctx); err != nil { + logger.Fatal(ctx, "initializing", err) + } + + if *title == "" { + logger.FatalString(ctx, "-title is required") + + } else if *url == "" { + logger.FatalString(ctx, "-url is required") + } + + err := ml.Publish(*title, *url) + if err != nil { + logger.Fatal(ctx, "publishing", err) + } + + default: + logger.FatalString(ctx, "invalid sub-command, must be list|publish") + } +} diff --git a/srv/src/cmd/mediocre-blog/main.go b/srv/src/cmd/mediocre-blog/main.go new file mode 100644 index 0000000..4cf3024 --- /dev/null +++ b/srv/src/cmd/mediocre-blog/main.go @@ -0,0 +1,160 @@ +package main + +import ( + "context" + "os" + "os/signal" + "path" + "syscall" + "time" + + "github.com/mediocregopher/blog.mediocregopher.com/srv/api" + "github.com/mediocregopher/blog.mediocregopher.com/srv/cfg" + "github.com/mediocregopher/blog.mediocregopher.com/srv/chat" + "github.com/mediocregopher/blog.mediocregopher.com/srv/mailinglist" + "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/mediocregopher/radix/v4" + "github.com/tilinna/clock" +) + +func main() { + + ctx := context.Background() + + cfg := cfg.New(cfg.Params{ + EnvPrefix: "MEDIOCRE_BLOG", + }) + + dataDir := cfg.String("data-dir", ".", "Directory to use for long term storage") + + 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 apiParams api.Params + apiParams.SetupCfg(cfg) + ctx = mctx.WithAnnotator(ctx, &apiParams) + + 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") + + // 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) + } + + ctx = mctx.Annotate(ctx, + "dataDir", *dataDir, + "redisProto", *redisProto, + "redisAddr", *redisAddr, + "redisPoolSize", *redisPoolSize, + "chatGlobalRoomMaxMsgs", *chatGlobalRoomMaxMsgs, + ) + + 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) + } + + mailingListDBFile := path.Join(*dataDir, "mailinglist.sqlite3") + ctx = mctx.Annotate(ctx, "mailingListDBFile", mailingListDBFile) + + mlStore, err := mailinglist.NewStore(mailingListDBFile) + 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) + + redis, err := (radix.PoolConfig{ + Size: *redisPoolSize, + }).New( + ctx, *redisProto, *redisAddr, + ) + + if err != nil { + logger.Fatal(ctx, "initializing redis pool", err) + } + 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 { + logger.Fatal(ctx, "initializing global chat room", err) + } + defer chatGlobalRoom.Close() + + chatUserIDCalc := chat.NewUserIDCalculator([]byte(*chatUserIDCalcSecret)) + + apiParams.Logger = logger.WithNamespace("api") + apiParams.PowManager = powMgr + apiParams.MailingList = ml + apiParams.GlobalRoom = chatGlobalRoom + apiParams.UserIDCalculator = chatUserIDCalc + + logger.Info(ctx, "listening") + a, err := api.New(apiParams) + if err != nil { + logger.Fatal(ctx, "initializing api", err) + } + defer func() { + shutdownCtx, cancel := context.WithTimeout(ctx, 5*time.Second) + defer cancel() + + if err := a.Shutdown(shutdownCtx); err != nil { + logger.Fatal(ctx, "shutting down api", err) + } + }() + + // wait + + sigCh := make(chan os.Signal, 1) + signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM) + <-sigCh + + // let the defers begin +} diff --git a/srv/src/cmd/userid-calc-cli/main.go b/srv/src/cmd/userid-calc-cli/main.go new file mode 100644 index 0000000..90c44e7 --- /dev/null +++ b/srv/src/cmd/userid-calc-cli/main.go @@ -0,0 +1,28 @@ +package main + +import ( + "encoding/json" + "flag" + "fmt" + + "github.com/mediocregopher/blog.mediocregopher.com/srv/chat" +) + +func main() { + + secret := flag.String("secret", "", "Secret to use when calculating UserIDs") + name := flag.String("name", "", "") + password := flag.String("password", "", "") + + flag.Parse() + + calc := chat.NewUserIDCalculator([]byte(*secret)) + userID := calc.Calculate(*name, *password) + + b, err := json.Marshal(userID) + if err != nil { + panic(err) + } + + fmt.Println(string(b)) +} |