98 lines
1.8 KiB
Go
98 lines
1.8 KiB
Go
package conf
|
|
|
|
import (
|
|
"embed"
|
|
"errors"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
|
|
"github.com/pelletier/go-toml/v2"
|
|
)
|
|
|
|
type Asset struct {
|
|
Path string
|
|
Url string
|
|
}
|
|
|
|
func (asset *Asset) fetchIfNotExists(staticPath string) {
|
|
destPath := filepath.Join(staticPath, asset.Path)
|
|
|
|
err := os.MkdirAll(path.Dir(destPath), os.FileMode(0750))
|
|
if err != nil {
|
|
log.Printf("Couldn't create parent dirs of %s\n", destPath)
|
|
panic(err)
|
|
}
|
|
|
|
out, err := os.OpenFile(
|
|
destPath,
|
|
os.O_WRONLY|os.O_CREATE|os.O_EXCL,
|
|
0666,
|
|
)
|
|
if err != nil {
|
|
if errors.Is(err, os.ErrExist) {
|
|
log.Printf("%s already exists\n", destPath)
|
|
return
|
|
}
|
|
panic(err)
|
|
}
|
|
defer out.Close()
|
|
|
|
resp, err := http.Get(asset.Url)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
_, err = io.Copy(out, resp.Body)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
type Config struct {
|
|
Address string
|
|
Protocol string
|
|
Extension string
|
|
NotesDir string
|
|
LogAccess bool
|
|
Production bool
|
|
}
|
|
|
|
var (
|
|
Conf Config
|
|
assets []Asset = []Asset{
|
|
{Path: "css/bootstrap.min.css", Url: "https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css"},
|
|
{Path: "css/tiny-mde.min.css", Url: "https://unpkg.com/tiny-markdown-editor/dist/tiny-mde.min.css"},
|
|
{Path: "js/tiny-mde.min.js", Url: "https://unpkg.com/tiny-markdown-editor/dist/tiny-mde.min.js"},
|
|
{Path: "icons/eye.svg", Url: "https://raw.githubusercontent.com/twbs/icons/refs/heads/main/icons/eye.svg"},
|
|
}
|
|
BaseTemplate string = "base.tmpl.html"
|
|
//go:embed static/*
|
|
Static embed.FS
|
|
//go:embed templates/*
|
|
Templates embed.FS
|
|
)
|
|
|
|
func LoadConfig(path string) {
|
|
var err error
|
|
|
|
b, err := os.ReadFile(path)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
err = toml.Unmarshal([]byte(b), &Conf)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func FetchAssets() {
|
|
for _, asset := range assets {
|
|
asset.fetchIfNotExists("./internal/conf/static")
|
|
}
|
|
}
|