From 0caad5ea57799b144886403b186def5002f1ac02 Mon Sep 17 00:00:00 2001 From: Project_IO Date: Thu, 13 Mar 2025 14:43:37 +0900 Subject: [PATCH] feat: middle save --- .gitignore | 2 ++ cmd/app.go => app.go | 5 +++- config/mod.go | 52 ++++++++++++++++++++++++++++++++++++++ go.mod | 1 + go.sum | 2 ++ index.html | 19 +++++++------- internal/routes/mod.go | 46 +++++++++++++++++++++++++++++++-- internal/service/worker.go | 23 +++++++++++++++++ package.json | 7 ++--- 9 files changed, 141 insertions(+), 16 deletions(-) rename cmd/app.go => app.go (86%) create mode 100644 internal/service/worker.go 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 @@ - - - - - Vite + React + TS - - -
- - + + + + Kuma Archive + + +
+ + diff --git a/internal/routes/mod.go b/internal/routes/mod.go index 6e19c3a..8ad5d77 100644 --- a/internal/routes/mod.go +++ b/internal/routes/mod.go @@ -1,24 +1,66 @@ package routes import ( + "fmt" + "os" + "path/filepath" + "strings" + + "git.wh64.net/devproje/kuma-archive/config" + "git.wh64.net/devproje/kuma-archive/internal/service" "github.com/gin-contrib/static" "github.com/gin-gonic/gin" ) func New(app *gin.Engine) { app.Use(static.Serve("/", static.LocalFile("./public", true))) + app.Use(static.Serve("/assets", static.LocalFile("./assets", false))) + app.NoRoute(func(ctx *gin.Context) { ctx.HTML(200, "index.html", nil) }) + app.GET("favicon.ico", func(ctx *gin.Context) { + ctx.File("/assets/favicon.ico") + }) + api := app.Group("/api") { api.GET("/path/*path", func(ctx *gin.Context) { + worker := service.NewWorkerService() + path := ctx.Param("path") + info, err := worker.Read(path) + if err != nil { + fmt.Fprintf(os.Stderr, "%v\n", err) + ctx.Status(404) + return + } + + if !info.IsDir() { + var split = strings.Split(path, "/") + ctx.FileAttachment(filepath.Join(config.INDEX_DIR, path), split[len(split)-1]) + + return + } + + entry, err := os.ReadDir(filepath.Join(config.INDEX_DIR, path)) + if err != nil { + ctx.Status(500) + return + } + + entries := make([]service.DirEntry, 0) + for _, fd := range entry { + entries = append(entries, service.DirEntry{ + Name: fd.Name(), + }) + } ctx.JSON(200, gin.H{ - "ok": 1, - "path": path, + "ok": 1, + "path": path, + "entries": entries, }) }) } diff --git a/internal/service/worker.go b/internal/service/worker.go new file mode 100644 index 0000000..3e10908 --- /dev/null +++ b/internal/service/worker.go @@ -0,0 +1,23 @@ +package service + +import ( + "os" + "path/filepath" + + "git.wh64.net/devproje/kuma-archive/config" +) + +type WorkerService struct{} + +type DirEntry struct { + Name string `json:"name"` +} + +func NewWorkerService() *WorkerService { + return &WorkerService{} +} + +func (sv *WorkerService) Read(path string) (os.FileInfo, error) { + fullpath := filepath.Join(config.INDEX_DIR, path) + return os.Stat(fullpath) +} diff --git a/package.json b/package.json index bed8589..a0f256d 100644 --- a/package.json +++ b/package.json @@ -6,10 +6,11 @@ "scripts": { "dev": "vite", "build": "tsc -b && vite build", + "build-server": "go build -o kuma-archive", + "build:all": "bun run build && bun run build-server", + "dev:all": "bun run build && go run ./app.go daemon -d", "lint": "eslint .", - "preview": "vite preview", - "dev:server": "go run ./cmd/app.go daemon -d", - "build:server": "go build -o kuma-archive" + "preview": "vite preview" }, "dependencies": { "react": "^19.0.0",