Download templates on boot
This commit is contained in:
parent
30339c6b16
commit
3ad3666002
7 changed files with 59 additions and 11 deletions
|
|
@ -1,13 +1,52 @@
|
|||
package conf
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"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)
|
||||
|
||||
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 {
|
||||
|
|
@ -15,8 +54,9 @@ type Config struct {
|
|||
Base string
|
||||
}
|
||||
Static struct {
|
||||
Dir string
|
||||
Root string
|
||||
Dir string
|
||||
Root string
|
||||
Assets []Asset
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -34,4 +74,8 @@ func LoadConfig(path string) {
|
|||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
for _, asset := range Conf.Static.Assets {
|
||||
asset.FetchIfNotExists(Conf.Static.Dir)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue