Skip to content

Commit ad6b5de

Browse files
committed
internal/lume: push to future-sight
Signed-off-by: Xe Iaso <[email protected]>
1 parent 748d911 commit ad6b5de

File tree

3 files changed

+118
-36
lines changed

3 files changed

+118
-36
lines changed

cmd/xesite/main.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ var (
2424
bind = flag.String("bind", ":3000", "Port to listen on")
2525
devel = flag.Bool("devel", false, "Enable development mode")
2626
dataDir = flag.String("data-dir", "./var", "Directory to store data in")
27+
futureSightURL = flag.String("future-sight-url", "", "URL to use for future sight preview deploys")
2728
gitBranch = flag.String("git-branch", "main", "Git branch to clone")
2829
gitRepo = flag.String("git-repo", "https://github.com/Xe/site", "Git repository to clone")
2930
githubSecret = flag.String("github-secret", "", "GitHub secret to use for webhooks")
@@ -54,14 +55,15 @@ func main() {
5455
}
5556

5657
fs, err := lume.New(ctx, &lume.Options{
57-
Branch: *gitBranch,
58-
Repo: *gitRepo,
59-
StaticSiteDir: "lume",
60-
URL: *siteURL,
61-
Development: *devel,
62-
PatreonClient: pc,
63-
DataDir: *dataDir,
64-
MiURL: *miURL,
58+
Branch: *gitBranch,
59+
Repo: *gitRepo,
60+
StaticSiteDir: "lume",
61+
URL: *siteURL,
62+
Development: *devel,
63+
PatreonClient: pc,
64+
DataDir: *dataDir,
65+
MiURL: *miURL,
66+
FutureSightURL: *futureSightURL,
6567
})
6668
if err != nil {
6769
log.Fatal(err)

internal/lume/lume.go

Lines changed: 85 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package lume
22

33
import (
44
"archive/zip"
5+
"bytes"
56
"context"
67
"encoding/json"
78
"errors"
@@ -12,6 +13,7 @@ import (
1213
"io/fs"
1314
"log"
1415
"log/slog"
16+
"mime/multipart"
1517
"net/http"
1618
"os"
1719
"os/exec"
@@ -26,6 +28,7 @@ import (
2628
"google.golang.org/protobuf/types/known/timestamppb"
2729
"gopkg.in/mxpv/patreon-go.v1"
2830
"tailscale.com/metrics"
31+
"within.website/x/web"
2932
"xeiaso.net/v4/internal/config"
3033
"xeiaso.net/v4/internal/jsonfeed"
3134
"xeiaso.net/v4/pb/external/mi"
@@ -40,11 +43,13 @@ var (
4043

4144
_ fs.FS = (*FS)(nil)
4245

43-
opens = metrics.LabelMap{Label: "name"}
44-
builds = expvar.NewInt("gauge_xesite_builds")
45-
updates = expvar.NewInt("gauge_xesite_updates")
46-
updateErrors = expvar.NewInt("gauge_xesite_update_errors")
47-
lastBuildTime = expvar.NewInt("gauge_xesite_last_build_time_ms")
46+
opens = metrics.LabelMap{Label: "name"}
47+
builds = expvar.NewInt("gauge_xesite_builds")
48+
updates = expvar.NewInt("gauge_xesite_updates")
49+
updateErrors = expvar.NewInt("gauge_xesite_update_errors")
50+
lastBuildTime = expvar.NewInt("gauge_xesite_last_build_time_ms")
51+
futureSightPokes = expvar.NewInt("gauge_xesite_future_sight_pokes")
52+
futureSightErrors = expvar.NewInt("gauge_xesite_future_sight_errors")
4853
)
4954

5055
func init() {
@@ -127,14 +132,15 @@ func (f *FS) Open(name string) (fs.File, error) {
127132
}
128133

129134
type Options struct {
130-
Development bool
131-
Branch string
132-
Repo string
133-
StaticSiteDir string
134-
URL string
135-
PatreonClient *patreon.Client
136-
DataDir string
137-
MiURL string
135+
Development bool
136+
Branch string
137+
Repo string
138+
StaticSiteDir string
139+
URL string
140+
PatreonClient *patreon.Client
141+
DataDir string
142+
MiURL string
143+
FutureSightURL string
138144
}
139145

140146
func New(ctx context.Context, o *Options) (*FS, error) {
@@ -217,6 +223,10 @@ func New(ctx context.Context, o *Options) (*FS, error) {
217223
slog.Debug("mi integration enabled")
218224
}
219225

226+
if o.FutureSightURL != "" {
227+
slog.Debug("future sight integration enabled")
228+
}
229+
220230
conf, err := config.Load(filepath.Join(fs.repoDir, "config.dhall"))
221231
if err != nil {
222232
log.Fatal(err)
@@ -231,6 +241,10 @@ func New(ctx context.Context, o *Options) (*FS, error) {
231241
go fs.mimiRefresh()
232242
fs.lastBuildTime = time.Now()
233243

244+
if o.FutureSightURL != "" {
245+
go fs.FutureSight(context.Background())
246+
}
247+
234248
return fs, nil
235249
}
236250

@@ -300,6 +314,10 @@ func (f *FS) Update(ctx context.Context) error {
300314

301315
go f.mimiRefresh()
302316

317+
if f.opt.FutureSightURL != "" {
318+
go f.FutureSight(context.Background())
319+
}
320+
303321
return nil
304322
}
305323

@@ -616,3 +634,57 @@ func (f *FS) mimiRefresh() {
616634
}
617635
}
618636
}
637+
638+
func (f *FS) FutureSight(ctx context.Context) {
639+
if err := f.futureSight(ctx); err != nil {
640+
slog.Error("failed to poke future sight", "err", err)
641+
futureSightErrors.Add(1)
642+
return
643+
}
644+
645+
futureSightPokes.Add(1)
646+
}
647+
648+
func (f *FS) futureSight(ctx context.Context) error {
649+
zipLoc := filepath.Join(f.opt.DataDir, "site.zip")
650+
651+
fin, err := os.Open(zipLoc)
652+
if err != nil {
653+
return fmt.Errorf("lume: can't open site zip for future sight: %w", err)
654+
}
655+
defer fin.Close()
656+
657+
buf := bytes.NewBuffer(nil)
658+
writer := multipart.NewWriter(buf)
659+
660+
part, err := writer.CreateFormFile("file", filepath.Base(zipLoc))
661+
if err != nil {
662+
return fmt.Errorf("lume: can't create form file: %w", err)
663+
}
664+
665+
if _, err := io.Copy(part, fin); err != nil {
666+
return fmt.Errorf("lume: can't copy file to buffer: %w", err)
667+
}
668+
669+
if err := writer.Close(); err != nil {
670+
return fmt.Errorf("lume: can't close writer: %w", err)
671+
}
672+
673+
req, err := http.NewRequestWithContext(ctx, "POST", f.opt.FutureSightURL+"/upload", buf)
674+
if err != nil {
675+
return fmt.Errorf("lume: can't create request: %w", err)
676+
}
677+
678+
req.Header.Set("Content-Type", writer.FormDataContentType())
679+
680+
resp, err := http.DefaultClient.Do(req)
681+
if err != nil {
682+
return fmt.Errorf("lume: can't post to future sight: %w", err)
683+
}
684+
685+
if resp.StatusCode != http.StatusOK {
686+
return web.NewError(http.StatusOK, resp)
687+
}
688+
689+
return nil
690+
}

lume/src/_includes/base.njk

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@
5959
<meta name="twitter:card" content="summary_large_image"/>
6060
{% endif %}
6161
{% endif %}
62+
63+
{% if commit.hash == "development" %}
64+
<meta name="robots" content="noindex" />
65+
{% endif %}
6266
</head>
6367

6468
<body class="px-4 py-2 mx-auto bg-bg-hard dark:bg-bgDark-hard text-fg-0 dark:text-fgDark-0 lg:max-w-5xl">
@@ -113,22 +117,26 @@
113117
</nav>
114118
</header>
115119

116-
<!--
117-
<div id="sticky-banner" tabindex="-1" class="flex justify-between w-full p-4 border-b border-fg-2 bg-bg-0 dark:bg-bgDark-0 dark:border-fg-2">
118-
<div class="flex items-center mx-auto">
119-
<p class="flex items-center font-normal">
120-
<span class="inline-flex p-1 mr-3 bg-bg-1 rounded-full dark:bg-bgDark-1 w-6 h-6 items-center justify-center">
121-
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-exclamation-mark" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
122-
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
123-
<path d="M12 19v.01"></path>
124-
<path d="M12 15v-10"></path>
125-
</svg>
126-
<span class="sr-only">Exclamation</span>
127-
</span>
128-
<span>Xe is now available for employment. Should you or your company be interested, please <a href="/contact/" class="underline">contact Xe</a> for more information. Remote positions only. Must hire in Canada.</span>
129-
</p>
120+
{% if commit.hash == "development" %}
121+
<div id="sticky-banner" tabindex="-1" class="flex justify-between w-full p-4 border-b border-fg-2 bg-bg-0 dark:bg-bgDark-0 dark:border-fg-2">
122+
<div class="flex items-center mx-auto">
123+
<p class="flex items-center font-normal">
124+
<span class="inline-flex p-1 mr-3 bg-bg-1 rounded-full dark:bg-bgDark-1 w-6 h-6 items-center justify-center">
125+
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-exclamation-mark" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
126+
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
127+
<path d="M12 19v.01"></path>
128+
<path d="M12 15v-10"></path>
129+
</svg>
130+
<span class="sr-only">Exclamation</span>
131+
</span>
132+
<span>You have stumbled across the development site for xeiaso.net. Please do not share this link with others, don't be the reason I need something more robust than the honor system.</span>
133+
</p>
134+
</div>
130135
</div>
131-
</div>
136+
{% endif %}
137+
138+
<!--
139+
132140
-->
133141

134142
<div class="mt-4 p-2">

0 commit comments

Comments
 (0)