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