package conf import ( "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 { Port int Extension string NotesDir string Templates struct { Dir string Base string } Static struct { Dir string Root string Assets []Asset } } 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) } for _, asset := range Conf.Static.Assets { asset.FetchIfNotExists(Conf.Static.Dir) } }