parent
d0a604b0ba
commit
60018dd9f5
After Width: | Height: | Size: 68 KiB |
@ -0,0 +1,129 @@ |
|||||||
|
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") |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,85 @@ |
|||||||
|
--- |
||||||
|
|
||||||
|
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 |
Loading…
Reference in new issue