feat: middle save

This commit is contained in:
WH64 2025-03-13 14:43:37 +09:00
parent 3fce68c5d0
commit 0caad5ea57
9 changed files with 141 additions and 16 deletions

2
.gitignore vendored
View file

@ -24,3 +24,5 @@ dist-ssr
*.sw?
public/
ka_data/
kuma-archive

View file

@ -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
}

View file

@ -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
}

1
go.mod
View file

@ -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

2
go.sum
View file

@ -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=

View file

@ -1,13 +1,12 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + React + TS</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Kuma Archive</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>

View file

@ -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,
})
})
}

View file

@ -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)
}

View file

@ -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",