✨ Check and create directory for content
This commit is contained in:
parent
4e6342dda2
commit
a76f1a7a8d
|
@ -8,6 +8,7 @@ import (
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Host string `env:"HOST" envDefault:"0.0.0.0"`
|
Host string `env:"HOST" envDefault:"0.0.0.0"`
|
||||||
|
ContentDir string `env:"CONTENT_DIR" envDefault:"content/"`
|
||||||
Port uint16 `env:"PORT" envDefault:"3000"`
|
Port uint16 `env:"PORT" envDefault:"3000"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
23
main.go
23
main.go
|
@ -6,12 +6,14 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"flag"
|
"flag"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
"path"
|
||||||
"runtime"
|
"runtime"
|
||||||
"runtime/pprof"
|
"runtime/pprof"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
@ -38,11 +40,26 @@ func init() {
|
||||||
flag.StringVar(&memProfilePath, "memprofile", "", "set path to saving pprof memory profile")
|
flag.StringVar(&memProfilePath, "memprofile", "", "set path to saving pprof memory profile")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
if err := env.ParseWithOptions(config, env.Options{
|
var err error
|
||||||
Prefix: "HOME_",
|
if err = env.ParseWithOptions(config, env.Options{Prefix: "HOME_"}); err != nil {
|
||||||
}); err != nil {
|
|
||||||
logger.Fatalln("cannot unmarshal configuration into domain:", err)
|
logger.Fatalln("cannot unmarshal configuration into domain:", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
config.ContentDir = path.Clean(config.ContentDir)
|
||||||
|
|
||||||
|
if _, err = os.Stat(config.ContentDir); err != nil {
|
||||||
|
if errors.Is(err, os.ErrExist) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if !errors.Is(err, os.ErrNotExist) {
|
||||||
|
logger.Fatalln("cannot check directory path for content:", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = os.MkdirAll(config.ContentDir, os.ModePerm); err != nil {
|
||||||
|
logger.Fatalln("cannot create directory for content:", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user