2025-01-26 22:23:42 +00:00
|
|
|
package conf
|
|
|
|
|
|
|
|
|
|
import (
|
2025-02-04 20:27:32 +00:00
|
|
|
"errors"
|
|
|
|
|
"io"
|
2025-01-26 22:23:42 +00:00
|
|
|
"log"
|
2025-02-04 20:27:32 +00:00
|
|
|
"net/http"
|
2025-01-26 22:23:42 +00:00
|
|
|
"os"
|
2025-02-04 20:27:32 +00:00
|
|
|
"path/filepath"
|
2025-01-26 22:23:42 +00:00
|
|
|
|
2025-01-28 22:08:01 +00:00
|
|
|
"github.com/pelletier/go-toml/v2"
|
2025-01-26 22:23:42 +00:00
|
|
|
)
|
|
|
|
|
|
2025-02-04 20:27:32 +00:00
|
|
|
type Asset struct {
|
|
|
|
|
Path string
|
|
|
|
|
Url string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (asset *Asset) FetchIfNotExists(staticPath string) {
|
|
|
|
|
destPath := filepath.Join(staticPath, asset.Path)
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-26 22:23:42 +00:00
|
|
|
type Config struct {
|
2025-02-04 20:27:32 +00:00
|
|
|
Port int
|
2025-01-28 22:08:01 +00:00
|
|
|
Extension string
|
|
|
|
|
NotesDir string
|
|
|
|
|
Templates struct {
|
|
|
|
|
Dir string
|
|
|
|
|
Base string
|
|
|
|
|
}
|
|
|
|
|
Static struct {
|
2025-02-04 20:27:32 +00:00
|
|
|
Dir string
|
|
|
|
|
Root string
|
|
|
|
|
Assets []Asset
|
2025-01-28 22:08:01 +00:00
|
|
|
}
|
2025-01-26 22:23:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var Conf Config
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
}
|
2025-02-04 20:27:32 +00:00
|
|
|
|
|
|
|
|
for _, asset := range Conf.Static.Assets {
|
|
|
|
|
asset.FetchIfNotExists(Conf.Static.Dir)
|
|
|
|
|
}
|
2025-01-26 22:23:42 +00:00
|
|
|
}
|