From 4f01edb9230f58ff84b0dd892c931ec8ac9aad55 Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Tue, 13 Sep 2022 12:56:08 +0200 Subject: move src out of srv, clean up default.nix and Makefile --- srv/src/cmd/hash-password/main.go | 23 ------ srv/src/cmd/load-test-data/galaxy.jpg | Bin 69901 -> 0 bytes srv/src/cmd/load-test-data/main.go | 129 ------------------------------- srv/src/cmd/load-test-data/test-data.yml | 85 -------------------- srv/src/cmd/mailinglist-cli/main.go | 118 ---------------------------- srv/src/cmd/mediocre-blog/main.go | 128 ------------------------------ 6 files changed, 483 deletions(-) delete mode 100644 srv/src/cmd/hash-password/main.go delete mode 100644 srv/src/cmd/load-test-data/galaxy.jpg delete mode 100644 srv/src/cmd/load-test-data/main.go delete mode 100644 srv/src/cmd/load-test-data/test-data.yml delete mode 100644 srv/src/cmd/mailinglist-cli/main.go delete mode 100644 srv/src/cmd/mediocre-blog/main.go (limited to 'srv/src/cmd') diff --git a/srv/src/cmd/hash-password/main.go b/srv/src/cmd/hash-password/main.go deleted file mode 100644 index 47ae1e7..0000000 --- a/srv/src/cmd/hash-password/main.go +++ /dev/null @@ -1,23 +0,0 @@ -package main - -import ( - "bufio" - "fmt" - "os" - "strings" - - "github.com/mediocregopher/blog.mediocregopher.com/srv/http" -) - -func main() { - - fmt.Fprint(os.Stderr, "Password: ") - - line, err := bufio.NewReader(os.Stdin).ReadString('\n') - - if err != nil { - panic(err) - } - - fmt.Println(http.NewPasswordHash(strings.TrimSpace(line))) -} diff --git a/srv/src/cmd/load-test-data/galaxy.jpg b/srv/src/cmd/load-test-data/galaxy.jpg deleted file mode 100644 index a2ee2d1..0000000 Binary files a/srv/src/cmd/load-test-data/galaxy.jpg and /dev/null differ diff --git a/srv/src/cmd/load-test-data/main.go b/srv/src/cmd/load-test-data/main.go deleted file mode 100644 index 5ebee32..0000000 --- a/srv/src/cmd/load-test-data/main.go +++ /dev/null @@ -1,129 +0,0 @@ -package main - -import ( - "context" - "fmt" - "os" - "path/filepath" - "time" - - cfgpkg "github.com/mediocregopher/blog.mediocregopher.com/srv/cfg" - "github.com/mediocregopher/blog.mediocregopher.com/srv/post" - "github.com/mediocregopher/mediocre-go-lib/v2/mctx" - "github.com/mediocregopher/mediocre-go-lib/v2/mlog" - "gopkg.in/yaml.v3" -) - -type testData struct { - PublishedPosts []post.Post `yaml:"published_posts"` - Assets map[string]string -} - -func loadTestData(path string) (testData, error) { - - f, err := os.Open(path) - if err != nil { - return testData{}, fmt.Errorf("opening file: %w", err) - } - defer f.Close() - - var res testData - - if err := yaml.NewDecoder(f).Decode(&res); err != nil { - return testData{}, fmt.Errorf("decoding file contents: %w", err) - } - - return res, nil -} - -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) - - testDataPath := cfg.String("test-data-path", "./test-data.yml", "File containing the data to be loaded in") - - // 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) - } - - testDataDir := filepath.Dir(*testDataPath) - - testData, err := loadTestData(*testDataPath) - if err != nil { - logger.Fatal(ctx, "loading test data", err) - } - - postDB, err := post.NewSQLDB(dataDir) - if err != nil { - logger.Fatal(ctx, "initializing post sql db", err) - } - defer postDB.Close() - - { - - postStore := post.NewStore(postDB) - - now := time.Now().Truncate(time.Hour) - - for _, post := range testData.PublishedPosts { - - ctx := mctx.Annotate(ctx, - "postID", post.ID, - "now", now) - - if _, err := postStore.Set(post, now); err != nil { - logger.Fatal(ctx, "setting post", err) - } - - logger.Info(ctx, "set post") - - now = now.Add(-1 * time.Hour) - } - - } - - { - assetStore := post.NewAssetStore(postDB) - - setAsset := func(assetID, assetPath string) error { - assetFullPath := filepath.Join(testDataDir, assetPath) - - f, err := os.Open(assetFullPath) - if err != nil { - return fmt.Errorf("opening %q for reading: %w", assetFullPath, err) - } - defer f.Close() - - return assetStore.Set(assetID, f) - } - - for assetID, assetPath := range testData.Assets { - - ctx := mctx.Annotate(ctx, - "assetID", assetID, - "assetPath", assetPath) - - if err := setAsset(assetID, assetPath); err != nil { - logger.Fatal(ctx, "setting asset", err) - } - - logger.Info(ctx, "set asset") - } - } -} diff --git a/srv/src/cmd/load-test-data/test-data.yml b/srv/src/cmd/load-test-data/test-data.yml deleted file mode 100644 index 51a08c5..0000000 --- a/srv/src/cmd/load-test-data/test-data.yml +++ /dev/null @@ -1,85 +0,0 @@ ---- - -published_posts: - - - id: markdown-test - title: Markdown Test - description: A little post containing different kinds of markdown elements. - tags: - - foo - series: testing - body: | - - This here's a test post containing various markdown elements in its body. - It's useful for making sure that posts will look good (generally). - - ## Let's Begin - - There's various things worth testing. _Emphasized_ and **bold** text are - great starting points. Also `little bits of code`. - - One might consider making a list of them. - - * A bit normal. - * _A bit emphasized_ - * **A bit bold** - * `A bit of code.` - - So many! - - ### A Subsection - - Crazy. Another way to delineate a subsection is with a horizontal rule. - - ----- - - And it only gets crazier from here! - - Check out this code block. - - ``` - // It's like actually being in the matrix - for !dead { - if awake { - work() - } else { - continue - } - } - ``` - - Edgy. - - #### Side-note - - Did you know that the terms "cyberspace" and "matrix" are attributable to a book from 1984 called _Neuromancer_? - - > The 1999 cyberpunk science fiction film The Matrix particularly draws from Neuromancer both eponym and usage of the term "matrix". - > - Wikipedia - - Here's a real picture of cyberspace. - - ![not a sound stage]({{ AssetURL "galaxy.jpg" }}) - - This has been a great post. - - - id: empty-test - title: Empty Test - description: A post with no content. Might as well test it. - tags: - - foo - - bar - series: testing - body: "" - - - id: little-markdown-test - title: Little Markdown Test - description: A post with almost no content. - tags: - - bar - series: testing - body: | - This page is almost empty. - -assets: - galaxy.jpg: ./galaxy.jpg diff --git a/srv/src/cmd/mailinglist-cli/main.go b/srv/src/cmd/mailinglist-cli/main.go deleted file mode 100644 index c090f41..0000000 --- a/srv/src/cmd/mailinglist-cli/main.go +++ /dev/null @@ -1,118 +0,0 @@ -package main - -import ( - "context" - "errors" - "io" - - "github.com/mediocregopher/blog.mediocregopher.com/srv/cfg" - cfgpkg "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 := cfgpkg.NewBlogCfg(cfg.Params{}) - - var dataDir cfgpkg.DataDir - dataDir.SetupCfg(cfg) - defer dataDir.Close() - ctx = mctx.WithAnnotator(ctx, &dataDir) - - 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) - } - - 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) - - 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 deleted file mode 100644 index 6b41e04..0000000 --- a/srv/src/cmd/mediocre-blog/main.go +++ /dev/null @@ -1,128 +0,0 @@ -package main - -import ( - "context" - "os" - "os/signal" - "syscall" - "time" - - cfgpkg "github.com/mediocregopher/blog.mediocregopher.com/srv/cfg" - "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) - - // 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) - - httpParams.Logger = logger.WithNamespace("http") - httpParams.PowManager = powMgr - httpParams.PostStore = postStore - httpParams.PostAssetStore = postAssetStore - httpParams.PostDraftStore = postDraftStore - httpParams.MailingList = ml - - logger.Info(ctx, "listening") - httpAPI, err := http.New(httpParams) - if err != nil { - logger.Fatal(ctx, "initializing 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) - } - }() - - // wait - - sigCh := make(chan os.Signal, 1) - signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM) - <-sigCh - - // let the defers begin -} -- cgit v1.2.3