feat(config): supports toml config files

This commit is contained in:
Anhgelus Morhtuuzh 2025-03-03 11:05:58 +01:00
parent 9ea75094de
commit 69ab8022bd
No known key found for this signature in database
GPG key ID: CAD341EFA92DDDE5
4 changed files with 58 additions and 53 deletions

78
data.go
View file

@ -27,60 +27,60 @@ type ConfigData interface {
} }
type Config struct { type Config struct {
Image string `json:"image"` Image string `json:"image" toml:"image"`
Description string `json:"description"` Description string `json:"description" toml:"description"`
Person *Person `json:"person"` Person *Person `json:"person" toml:"person"`
Color *Color `json:"colors"` Color *Color `json:"colors" toml:"colors"`
Links []*Link `json:"links"` Links []*Link `json:"links" toml:"links"`
Legal *Legal `json:"legal"` Legal *Legal `json:"legal" toml:"legal"`
CustomPages []string `json:"custom_pages"` CustomPages []string `json:"custom_pages" toml:"custom_pages"`
} }
type Person struct { type Person struct {
Name string `json:"name"` Name string `json:"name" toml:"name"`
Pronouns string `json:"pronouns"` Pronouns string `json:"pronouns" toml:"pronouns"`
Image string `json:"image"` Image string `json:"image" toml:"image"`
Tags []*Tag `json:"tags"` Tags []*Tag `json:"tags" toml:"tags"`
} }
type Tag struct { type Tag struct {
Name string `json:"name"` Name string `json:"name" toml:"name"`
Description string `json:"description"` Description string `json:"description" toml:"description"`
Link string `json:"link"` Link string `json:"link" toml:"link"`
} }
type Color struct { type Color struct {
Background *BackgroundColor `json:"background"` Background *BackgroundColor `json:"background" toml:"background"`
Button *ButtonColor `json:"buttons"` Button *ButtonColor `json:"buttons" toml:"buttons"`
Text string `json:"text"` Text string `json:"text" toml:"text"`
TagHover string `json:"tag_hover"` TagHover string `json:"tag_hover" toml:"tag_hover"`
} }
type BackgroundColor struct { type BackgroundColor struct {
Type string `json:"type"` Type string `json:"type" toml:"type"`
Angle uint `json:"angle"` Angle uint `json:"angle" toml:"angle"`
Colors []struct { Colors []struct {
Color string `json:"color"` Color string `json:"color" toml:"color"`
Position uint `json:"position"` Position uint `json:"position" toml:"position"`
} `json:"colors"` } `json:"colors"`
} }
type ButtonColor struct { type ButtonColor struct {
Text string `json:"text"` Text string `json:"text" toml:"text"`
TextHover string `json:"text_hover"` TextHover string `json:"text_hover" toml:"text_hover"`
Background string `json:"background"` Background string `json:"background" toml:"background"`
BackgroundHover string `json:"background_hover"` BackgroundHover string `json:"background_hover" toml:"background_hover"`
} }
type Link struct { type Link struct {
Link string `json:"link"` Link string `json:"link" toml:"link"`
Content string `json:"content"` Content string `json:"content" toml:"content"`
} }
type Legal struct { type Legal struct {
LegalInformationLink string `json:"legal_information_link"` LegalInformationLink string `json:"legal_information_link" toml:"legal_information_link"`
ImagesSource []string `json:"images_source"` ImagesSource []string `json:"images_source" toml:"images_source"`
FontSource string `json:"font_source"` FontSource string `json:"font_source" toml:"font_source"`
} }
func (c *Config) GetBackground() template.CSS { func (c *Config) GetBackground() template.CSS {
@ -96,17 +96,17 @@ func (c *Config) GetTextColor() template.CSS {
} }
type CustomPage struct { type CustomPage struct {
Title string `json:"title"` Title string `json:"title" toml:"title"`
URI string `json:"uri"` URI string `json:"uri" toml:"uri"`
Image string `json:"image"` Image string `json:"image" toml:"image"`
Description string `json:"description"` Description string `json:"description" toml:"description"`
Color *Color `json:"colors"` Color *Color `json:"colors" toml:"colors"`
Content []*CustomContent `json:"content"` Content []*CustomContent `json:"content" toml:"content"`
} }
type CustomContent struct { type CustomContent struct {
Type string `json:"type"` Type string `json:"type" toml:"type"`
Content string `json:"content"` Content string `json:"content" toml:"content"`
} }
type Content interface { type Content interface {

5
go.mod
View file

@ -2,6 +2,9 @@ module now
go 1.24 go 1.24
require github.com/anhgelus/golatt v0.4.0 require (
github.com/BurntSushi/toml v1.4.0
github.com/anhgelus/golatt v0.4.0
)
require github.com/gorilla/mux v1.8.1 // indirect require github.com/gorilla/mux v1.8.1 // indirect

16
go.sum
View file

@ -1,14 +1,6 @@
github.com/anhgelus/golatt v0.1.0 h1:Lb6wCtbOmIE/p/wlDeFzuFuR++twPx+fHMNVDGhqjXk= github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0=
github.com/anhgelus/golatt v0.1.0/go.mod h1:Lgyy/Tm9A3w1ft72mZOUxl/p+4G2/WB3qxoXTv7Y4y8= github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
github.com/anhgelus/golatt v0.2.0 h1:tdgrx3K61mVYBMy9CNhTheSIV8lPWBl/JjeDIHmz9eg= github.com/anhgelus/golatt v0.4.0 h1:EZIBHtWMpwhJwMEzTSUNgieqWwHPtlBpKQ4idSjWqRA=
github.com/anhgelus/golatt v0.2.0/go.mod h1:Lgyy/Tm9A3w1ft72mZOUxl/p+4G2/WB3qxoXTv7Y4y8= github.com/anhgelus/golatt v0.4.0/go.mod h1:yC4OU6rMSbHKsfcvN7jYSyGsnpbivHKDGVFohZI5VH4=
github.com/anhgelus/golatt v0.3.0 h1:HjolihJ+S9egfTXcbk3YBfxBKmEcEraUsqCsrPXzHOU=
github.com/anhgelus/golatt v0.3.0/go.mod h1:Lgyy/Tm9A3w1ft72mZOUxl/p+4G2/WB3qxoXTv7Y4y8=
github.com/anhgelus/golatt v0.3.1-0.20250224013601-7215f78c2860 h1:Xi1aqbDU1sr/8pJEun4sjOqz5PPQc7hVSR1kuCiRgTE=
github.com/anhgelus/golatt v0.3.1-0.20250224013601-7215f78c2860/go.mod h1:Lgyy/Tm9A3w1ft72mZOUxl/p+4G2/WB3qxoXTv7Y4y8=
github.com/anhgelus/golatt v0.3.1-0.20250224134406-139fb679dfbe h1:5tOWGCqgDHY3zxdTYBLAjmRhwVJBeBobf+4TJTLB8II=
github.com/anhgelus/golatt v0.3.1-0.20250224134406-139fb679dfbe/go.mod h1:yC4OU6rMSbHKsfcvN7jYSyGsnpbivHKDGVFohZI5VH4=
github.com/anhgelus/golatt v0.3.1-0.20250224140223-d0258a5fba9f h1:SwvbWm/6oeHs8KzadqC3P3mFTgilkMZz2O5Glxtsq6s=
github.com/anhgelus/golatt v0.3.1-0.20250224140223-d0258a5fba9f/go.mod h1:yC4OU6rMSbHKsfcvN7jYSyGsnpbivHKDGVFohZI5VH4=
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=

12
main.go
View file

@ -4,10 +4,12 @@ import (
"embed" "embed"
"encoding/json" "encoding/json"
"flag" "flag"
"github.com/BurntSushi/toml"
"github.com/anhgelus/golatt" "github.com/anhgelus/golatt"
"log/slog" "log/slog"
"net/http" "net/http"
"os" "os"
"strings"
) )
var ( var (
@ -49,8 +51,15 @@ func main() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
var cfg Config var cfg Config
err = json.Unmarshal(b, &cfg) if strings.HasSuffix(configPath, ".json") {
err = json.Unmarshal(b, &cfg)
} else if strings.HasSuffix(configPath, ".toml") {
err = toml.Unmarshal(b, &cfg)
} else {
panic("config file must be .json or .toml")
}
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -58,6 +67,7 @@ func main() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
var g *golatt.Golatt var g *golatt.Golatt
if dev { if dev {
g = golatt.New(golatt.UsableEmbedFS("templates", templates), os.DirFS("public"), os.DirFS("dist")) g = golatt.New(golatt.UsableEmbedFS("templates", templates), os.DirFS("public"), os.DirFS("dist"))