diff --git a/.gitignore b/.gitignore index 9a844d5..136130e 100644 --- a/.gitignore +++ b/.gitignore @@ -24,3 +24,5 @@ dist-ssr *.sw? public/ +ka_data/ +kuma-archive diff --git a/cmd/app.go b/app.go similarity index 86% rename from cmd/app.go rename to app.go index d20aea1..63ebb64 100644 --- a/cmd/app.go +++ b/app.go @@ -4,6 +4,7 @@ import ( "fmt" "os" + "git.wh64.net/devproje/kuma-archive/config" "git.wh64.net/devproje/kuma-archive/internal/routes" "github.com/devproje/commando" "github.com/devproje/commando/option" @@ -13,6 +14,8 @@ import ( func main() { command := commando.NewCommando(os.Args[1:]) + cnf := config.Get() + command.Root("daemon", "run file server", func(n *commando.Node) error { debug, err := option.ParseBool(*n.MustGetOpt("debug"), n) if err != nil { @@ -26,7 +29,7 @@ func main() { gin := gin.Default() routes.New(gin) - if err := gin.Run(); err != nil { + if err := gin.Run(fmt.Sprintf(":%d", cnf.Port)); err != nil { return err } diff --git a/config/mod.go b/config/mod.go index cf66046..e37689d 100644 --- a/config/mod.go +++ b/config/mod.go @@ -1,5 +1,57 @@ package config +import ( + "encoding/json" + "fmt" + "os" + "path/filepath" +) + type ConfigRef struct { Port int `json:"port"` } + +var ( + ROOT_DIRECTORY string + CONFIG_DIR string + INDEX_DIR string +) + +func init() { + ROOT_DIRECTORY = "ka_data" + if os.Getenv("KA_PATH") != "" { + ROOT_DIRECTORY = os.Getenv("KA_PATH") + } + + if _, err := os.ReadDir(ROOT_DIRECTORY); err != nil { + raw := ConfigRef{ + Port: 8080, + } + buf, _ := json.MarshalIndent(raw, "", " ") + + _ = os.Mkdir(ROOT_DIRECTORY, 0755) + _ = os.WriteFile(filepath.Join(ROOT_DIRECTORY, "config.json"), []byte(buf), 0644) + } + + CONFIG_DIR = filepath.Join(ROOT_DIRECTORY, "config.json") + + INDEX_DIR = filepath.Join(ROOT_DIRECTORY, "index") + if _, err := os.ReadDir(INDEX_DIR); err != nil { + _ = os.Mkdir(INDEX_DIR, 0755) + } +} + +func Get() *ConfigRef { + buf, err := os.ReadFile(CONFIG_DIR) + if err != nil { + _, _ = fmt.Fprintf(os.Stderr, "%v\n", err) + return nil + } + + var config ConfigRef + if err = json.Unmarshal(buf, &config); err != nil { + return nil + } + + return &config +} diff --git a/go.mod b/go.mod index 6ce59ef..2afed37 100644 --- a/go.mod +++ b/go.mod @@ -18,6 +18,7 @@ require ( github.com/go-playground/universal-translator v0.18.1 // indirect github.com/go-playground/validator/v10 v10.25.0 // indirect github.com/goccy/go-json v0.10.5 // indirect + github.com/golang-jwt/jwt v3.2.2+incompatible // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/klauspost/cpuid/v2 v2.2.10 // indirect github.com/kr/text v0.2.0 // indirect diff --git a/go.sum b/go.sum index 942f8d4..ed3a342 100644 --- a/go.sum +++ b/go.sum @@ -30,6 +30,8 @@ github.com/go-playground/validator/v10 v10.25.0 h1:5Dh7cjvzR7BRZadnsVOzPhWsrwUr0 github.com/go-playground/validator/v10 v10.25.0/go.mod h1:GGzBIJMuE98Ic/kJsBXbz1x/7cByt++cQ+YOuDM5wus= github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4= github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M= +github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY= +github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I= github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= diff --git a/index.html b/index.html index e4b78ea..14b7eee 100644 --- a/index.html +++ b/index.html @@ -1,13 +1,12 @@ -
- - - -