Skip to content

Commit

Permalink
添加reload接口
Browse files Browse the repository at this point in the history
  • Loading branch information
zgwit committed Oct 31, 2024
1 parent 7263536 commit e94cea6
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 42 deletions.
5 changes: 3 additions & 2 deletions api/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import "github.com/god-jason/bucket/table"
func init() {

Register("GET", "table/list", table.ApiList)
Register("GET", "table/:table/file/:file", table.ApiManifest)
Register("POST", "table/:table/file/:file", table.ApiManifestUpdate)
Register("GET", "table/:table/reload", table.ApiReload)
Register("GET", "table/:table/conf/*conf", table.ApiConf)
Register("POST", "table/:table/conf/*conf", table.ApiConfUpdate)

Register("POST", "table/:table/count", table.ApiCount)
Register("POST", "table/:table/create", table.ApiCreate)
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ require (
github.com/spf13/viper v1.19.0
github.com/super-l/machine-code v0.0.0-20240104080917-14388119a8dd
go.mongodb.org/mongo-driver v1.15.1
golang.org/x/crypto v0.24.0
golang.org/x/crypto v0.25.0
gopkg.in/natefinch/lumberjack.v2 v2.2.1
gopkg.in/yaml.v3 v3.0.1
)
Expand Down Expand Up @@ -81,7 +81,7 @@ require (
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/arch v0.8.0 // indirect
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.16.0 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -184,16 +184,16 @@ golang.org/x/arch v0.8.0 h1:3wRIsP3pM4yUptoR96otTUOXI367OS0+c9eeRi9doIc=
golang.org/x/arch v0.8.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.24.0 h1:mnl8DM0o513X8fdIkmyFE/5hTYxbwYOjDS/+rK6qpRI=
golang.org/x/crypto v0.24.0/go.mod h1:Z1PMYSOR5nyMcyAVAIQSKCDwalqy85Aqn1x3Ws4L5DM=
golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30=
golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M=
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 h1:yixxcjnhBmY0nkL253HFVIm0JsFHwrHdT3Yh6szTnfY=
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8/go.mod h1:jj3sYF3dwk5D+ghuXyeI3r5MFf+NT2An6/9dOA95KSI=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ=
golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE=
golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=
golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
Expand Down
44 changes: 10 additions & 34 deletions table/manifest.go → table/conf.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package table

import (
"encoding/json"
"github.com/gin-gonic/gin"
"github.com/spf13/viper"
"io"
"os"
"path/filepath"
)
Expand Down Expand Up @@ -42,56 +42,32 @@ func ApiList(ctx *gin.Context) {
OK(ctx, infos)
}

func ApiManifest(ctx *gin.Context) {
func ApiConf(ctx *gin.Context) {
tab := ctx.Param("table")
fn := filepath.Join(viper.GetString("data"), Path, tab+".json")
buf, err := os.ReadFile(fn)
if err != nil {
Error(ctx, err)
return
}
conf := ctx.Param("conf")

var data any
err = json.Unmarshal(buf, &data)
fn := filepath.Join(viper.GetString("data"), Path, tab, conf)
_, err := os.Stat(fn)
if err != nil {
Error(ctx, err)
return
}

OK(ctx, data)
ctx.File(fn)
}

func ApiManifestUpdate(ctx *gin.Context) {
func ApiConfUpdate(ctx *gin.Context) {
tab := ctx.Param("table")
var data any
err := ctx.ShouldBindJSON(&data)
if err != nil {
Error(ctx, err)
return
}
conf := ctx.Param("conf")

buf, err := json.MarshalIndent(data, "", "\t")
if err != nil {
Error(ctx, err)
return
}

_ = os.MkdirAll(filepath.Join(viper.GetString("data"), Path), os.ModePerm)
fn := filepath.Join(viper.GetString("data"), Path, tab+".json")
fn := filepath.Join(viper.GetString("data"), Path, tab, conf)
file, err := os.OpenFile(fn, os.O_CREATE|os.O_WRONLY, os.ModePerm)
if err != nil {
Error(ctx, err)
return
}
defer file.Close()
_, err = file.Write(buf)
if err != nil {
Error(ctx, err)
return
}

//加载
err = Load(tab)
_, err = io.Copy(file, ctx.Request.Body)
if err != nil {
Error(ctx, err)
return
Expand Down
14 changes: 14 additions & 0 deletions table/load.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package table

import "github.com/gin-gonic/gin"

func ApiReload(ctx *gin.Context) {
tab := ctx.Param("table")
err := Load(tab)
if err != nil {
Error(ctx, err)
return
}

OK(ctx, nil)
}

0 comments on commit e94cea6

Please sign in to comment.