feat: middle save
This commit is contained in:
parent
3fce68c5d0
commit
0caad5ea57
9 changed files with 141 additions and 16 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -24,3 +24,5 @@ dist-ssr
|
||||||
*.sw?
|
*.sw?
|
||||||
|
|
||||||
public/
|
public/
|
||||||
|
ka_data/
|
||||||
|
kuma-archive
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"git.wh64.net/devproje/kuma-archive/config"
|
||||||
"git.wh64.net/devproje/kuma-archive/internal/routes"
|
"git.wh64.net/devproje/kuma-archive/internal/routes"
|
||||||
"github.com/devproje/commando"
|
"github.com/devproje/commando"
|
||||||
"github.com/devproje/commando/option"
|
"github.com/devproje/commando/option"
|
||||||
|
@ -13,6 +14,8 @@ import (
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
command := commando.NewCommando(os.Args[1:])
|
command := commando.NewCommando(os.Args[1:])
|
||||||
|
cnf := config.Get()
|
||||||
|
|
||||||
command.Root("daemon", "run file server", func(n *commando.Node) error {
|
command.Root("daemon", "run file server", func(n *commando.Node) error {
|
||||||
debug, err := option.ParseBool(*n.MustGetOpt("debug"), n)
|
debug, err := option.ParseBool(*n.MustGetOpt("debug"), n)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -26,7 +29,7 @@ func main() {
|
||||||
gin := gin.Default()
|
gin := gin.Default()
|
||||||
routes.New(gin)
|
routes.New(gin)
|
||||||
|
|
||||||
if err := gin.Run(); err != nil {
|
if err := gin.Run(fmt.Sprintf(":%d", cnf.Port)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,57 @@
|
||||||
package config
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
)
|
||||||
|
|
||||||
type ConfigRef struct {
|
type ConfigRef struct {
|
||||||
Port int `json:"port"`
|
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
1
go.mod
|
@ -18,6 +18,7 @@ require (
|
||||||
github.com/go-playground/universal-translator v0.18.1 // indirect
|
github.com/go-playground/universal-translator v0.18.1 // indirect
|
||||||
github.com/go-playground/validator/v10 v10.25.0 // indirect
|
github.com/go-playground/validator/v10 v10.25.0 // indirect
|
||||||
github.com/goccy/go-json v0.10.5 // 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/json-iterator/go v1.1.12 // indirect
|
||||||
github.com/klauspost/cpuid/v2 v2.2.10 // indirect
|
github.com/klauspost/cpuid/v2 v2.2.10 // indirect
|
||||||
github.com/kr/text v0.2.0 // indirect
|
github.com/kr/text v0.2.0 // indirect
|
||||||
|
|
2
go.sum
2
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/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 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4=
|
||||||
github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
|
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 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
|
||||||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
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=
|
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||||
|
|
19
index.html
19
index.html
|
@ -1,13 +1,12 @@
|
||||||
<!doctype html>
|
<!doctype html>
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8" />
|
<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" />
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
<title>Kuma Archive</title>
|
||||||
<title>Vite + React + TS</title>
|
</head>
|
||||||
</head>
|
<body>
|
||||||
<body>
|
<div id="root"></div>
|
||||||
<div id="root"></div>
|
<script type="module" src="/src/main.tsx"></script>
|
||||||
<script type="module" src="/src/main.tsx"></script>
|
</body>
|
||||||
</body>
|
|
||||||
</html>
|
</html>
|
||||||
|
|
|
@ -1,24 +1,66 @@
|
||||||
package routes
|
package routes
|
||||||
|
|
||||||
import (
|
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-contrib/static"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
func New(app *gin.Engine) {
|
func New(app *gin.Engine) {
|
||||||
app.Use(static.Serve("/", static.LocalFile("./public", true)))
|
app.Use(static.Serve("/", static.LocalFile("./public", true)))
|
||||||
|
app.Use(static.Serve("/assets", static.LocalFile("./assets", false)))
|
||||||
|
|
||||||
app.NoRoute(func(ctx *gin.Context) {
|
app.NoRoute(func(ctx *gin.Context) {
|
||||||
ctx.HTML(200, "index.html", nil)
|
ctx.HTML(200, "index.html", nil)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
app.GET("favicon.ico", func(ctx *gin.Context) {
|
||||||
|
ctx.File("/assets/favicon.ico")
|
||||||
|
})
|
||||||
|
|
||||||
api := app.Group("/api")
|
api := app.Group("/api")
|
||||||
{
|
{
|
||||||
api.GET("/path/*path", func(ctx *gin.Context) {
|
api.GET("/path/*path", func(ctx *gin.Context) {
|
||||||
|
worker := service.NewWorkerService()
|
||||||
|
|
||||||
path := ctx.Param("path")
|
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{
|
ctx.JSON(200, gin.H{
|
||||||
"ok": 1,
|
"ok": 1,
|
||||||
"path": path,
|
"path": path,
|
||||||
|
"entries": entries,
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
23
internal/service/worker.go
Normal file
23
internal/service/worker.go
Normal 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)
|
||||||
|
}
|
|
@ -6,10 +6,11 @@
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "vite",
|
"dev": "vite",
|
||||||
"build": "tsc -b && vite build",
|
"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 .",
|
"lint": "eslint .",
|
||||||
"preview": "vite preview",
|
"preview": "vite preview"
|
||||||
"dev:server": "go run ./cmd/app.go daemon -d",
|
|
||||||
"build:server": "go build -o kuma-archive"
|
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"react": "^19.0.0",
|
"react": "^19.0.0",
|
||||||
|
|
Loading…
Reference in a new issue