feat(data): parse json and render simple thing
This commit is contained in:
parent
a50d678a67
commit
d25e04d8c7
7 changed files with 62 additions and 17 deletions
13
data.go
Normal file
13
data.go
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
type Data struct {
|
||||||
|
Image string `json:"image"`
|
||||||
|
Description string `json:"description"`
|
||||||
|
Person *Person `json:"person"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Person struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
Pronouns string `json:"pronouns"`
|
||||||
|
Image string `json:"image"`
|
||||||
|
}
|
0
dist/styles.css
vendored
Normal file
0
dist/styles.css
vendored
Normal file
9
example.json
Normal file
9
example.json
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
{
|
||||||
|
"image": "",
|
||||||
|
"description": "",
|
||||||
|
"person": {
|
||||||
|
"name": "",
|
||||||
|
"pronouns": "",
|
||||||
|
"image": ""
|
||||||
|
}
|
||||||
|
}
|
39
main.go
39
main.go
|
@ -2,41 +2,58 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"embed"
|
"embed"
|
||||||
|
"encoding/json"
|
||||||
"flag"
|
"flag"
|
||||||
"github.com/anhgelus/golatt"
|
"github.com/anhgelus/golatt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:embed templates
|
//go:embed templates
|
||||||
var templates embed.FS
|
var templates embed.FS
|
||||||
|
|
||||||
var (
|
var (
|
||||||
domain string
|
domain string
|
||||||
data string
|
dataPath string
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
flag.StringVar(&domain, "domain", "", "domain to use")
|
flag.StringVar(&domain, "domain", "", "domain to use")
|
||||||
flag.StringVar(&data, "data", "", "data to use")
|
flag.StringVar(&dataPath, "data", "", "data to use")
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
if domain == "" {
|
if domain == "" {
|
||||||
slog.Error("Domain not set. Set it with --domain value")
|
domain = os.Getenv("NOW_DOMAIN")
|
||||||
return
|
if domain == "" {
|
||||||
|
slog.Error("Domain not set. Set it with --domain value or with the env NOW_DOMAIN")
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if data == "" {
|
if dataPath == "" {
|
||||||
slog.Error("Data not set. Set it with --data relative path")
|
dataPath = os.Getenv("NOW_DATA")
|
||||||
return
|
if dataPath == "" {
|
||||||
|
slog.Error("Data not set. Set it with --data relative path or with the env NOW_DATA")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
b, err := os.ReadFile(dataPath)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
var data Data
|
||||||
|
err = json.Unmarshal(b, &data)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
}
|
}
|
||||||
g := golatt.New(templates)
|
g := golatt.New(templates)
|
||||||
g.DefaultSeoData = &golatt.SeoData{
|
g.DefaultSeoData = &golatt.SeoData{
|
||||||
Image: "",
|
Image: data.Image,
|
||||||
Description: "",
|
Description: data.Description,
|
||||||
Domain: domain,
|
Domain: domain,
|
||||||
}
|
}
|
||||||
g.Templates = append(g.Templates, "templates/page/*.gohtml")
|
g.Templates = append(g.Templates, "templates/base/*.gohtml")
|
||||||
|
|
||||||
//g.StartServer(":80")
|
//g.StartServer(":80")
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,13 +10,7 @@
|
||||||
{{template "opengraph-base" .SEO}}
|
{{template "opengraph-base" .SEO}}
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
{{if .Data.HasNav}}
|
|
||||||
{{template "navbar" .}}
|
|
||||||
{{end}}
|
|
||||||
{{template "body" .}}
|
{{template "body" .}}
|
||||||
{{if .Data.HasFooter}}
|
|
||||||
{{template "footer" .}}
|
|
||||||
{{end}}
|
|
||||||
<script type="module" src="{{getAssetPath "index.js"}}" defer></script>
|
<script type="module" src="{{getAssetPath "index.js"}}" defer></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
12
templates/page/index.gohtml
Normal file
12
templates/page/index.gohtml
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
{{define "body"}}
|
||||||
|
<main>
|
||||||
|
<div class="presentation">
|
||||||
|
<figure>
|
||||||
|
<img src="{{ getStaticPath .Person.Image }}" alt="{{ .Person.Name }}'s image">
|
||||||
|
</figure>
|
||||||
|
<h2>{{ .Person.Name }}</h2>
|
||||||
|
<p>{{ .Person.Pronouns }}</p>
|
||||||
|
</div>
|
||||||
|
<p class="description">{{ .Description }}</p>
|
||||||
|
</main>
|
||||||
|
{{end}}
|
Loading…
Add table
Add a link
Reference in a new issue