Skip to content

Commit 817a867

Browse files
authoredMay 12, 2023
Config to extend rendered head element (#27)
1 parent 423b232 commit 817a867

File tree

5 files changed

+30
-3
lines changed

5 files changed

+30
-3
lines changed
 

‎docs/self-hosting.md

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ SNIPS_LIMITS_SESSIONDURATION Duration 15m maximum s
4747
SNIPS_DB_FILEPATH String data/snips.db path to database file
4848
SNIPS_HTTP_INTERNAL URL http://localhost:8080 internal address to listen for http requests
4949
SNIPS_HTTP_EXTERNAL URL http://localhost:8080 external http address displayed in commands
50+
SNIPS_HTML_EXTENDHEADFILE String path to html file for extra content in <head>
5051
SNIPS_SSH_INTERNAL URL ssh://localhost:2222 internal address to listen for ssh requests
5152
SNIPS_SSH_EXTERNAL URL ssh://localhost:2222 external ssh address displayed in commands
5253
SNIPS_SSH_HOSTKEYPATH String data/keys/snips path to host keys (without extension)

‎internal/config/config.go

+4
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ type Config struct {
4040
External url.URL `default:"http://localhost:8080" desc:"external http address displayed in commands"`
4141
}
4242

43+
HTML struct {
44+
ExtendHeadFile string `default:"" desc:"path to html file for extra content in <head>"`
45+
}
46+
4347
SSH struct {
4448
Internal url.URL `default:"ssh://localhost:2222" desc:"internal address to listen for ssh requests"`
4549
External url.URL `default:"ssh://localhost:2222" desc:"external ssh address displayed in commands"`

‎internal/http/assets.go

+23-3
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ import (
55
"embed"
66
"html/template"
77
"net/http"
8+
"os"
89
"path"
910
"path/filepath"
1011
"strings"
1112

13+
"github.com/rs/zerolog/log"
1214
"github.com/tdewolff/minify/v2"
1315
"github.com/tdewolff/minify/v2/css"
1416
"github.com/tdewolff/minify/v2/js"
@@ -50,7 +52,7 @@ type Assets struct {
5052
}
5153

5254
// NewAssets holds the templates, static content and minifies accordingly.
53-
func NewAssets(webFS *embed.FS, docsFS *embed.FS, readme []byte) (*Assets, error) {
55+
func NewAssets(webFS *embed.FS, docsFS *embed.FS, readme []byte, extendHeadFile string) (*Assets, error) {
5456
assets := &Assets{
5557
webFS: webFS,
5658
docsFS: docsFS,
@@ -61,9 +63,27 @@ func NewAssets(webFS *embed.FS, docsFS *embed.FS, readme []byte) (*Assets, error
6163
assets.mini.AddFunc(cssMime, css.Minify)
6264
assets.mini.AddFunc(jsMime, js.Minify)
6365

64-
var err error
66+
var (
67+
err error
68+
extendHeadContent string
69+
)
6570

66-
if assets.tmpl, err = template.ParseFS(webFS, tmplPattern); err != nil {
71+
if extendHeadFile != "" {
72+
if headContent, err := os.ReadFile(extendHeadFile); err == nil {
73+
extendHeadContent = string(headContent)
74+
} else {
75+
log.Warn().Err(err).Msg("unable to extend head content")
76+
}
77+
}
78+
79+
tmpl := template.New("file")
80+
tmpl.Funcs(template.FuncMap{
81+
"ExtendedHeadContent": func() template.HTML {
82+
return template.HTML(extendHeadContent)
83+
},
84+
})
85+
86+
if assets.tmpl, err = tmpl.ParseFS(webFS, tmplPattern); err != nil {
6787
return nil, err
6888
}
6989

‎main.go

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ func main() {
5151
&webFS,
5252
&docsFS,
5353
readme,
54+
cfg.HTML.ExtendHeadFile,
5455
)
5556
if err != nil {
5657
log.Fatal().Err(err).Msg("failed to load assets")

‎web/templates/file.go.html

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<title>{{ .FileID }} - snips.sh</title>
1111
<link rel="stylesheet" href="/assets/index.css">
1212
<script type="module" src="/assets/index.js"></script>
13+
{{ ExtendedHeadContent }}
1314
</head>
1415

1516
<body>

0 commit comments

Comments
 (0)
Failed to load comments.