Skip to content

Commit

Permalink
一大堆改动
Browse files Browse the repository at this point in the history
  • Loading branch information
zgwit committed Oct 28, 2024
1 parent 89387c7 commit 6270b2c
Show file tree
Hide file tree
Showing 21 changed files with 222 additions and 26 deletions.
23 changes: 23 additions & 0 deletions admin/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package admin

import (
"github.com/gin-gonic/gin"
"github.com/god-jason/bucket/api"
)

func init() {
api.Register("GET", "me", me)
api.Register("GET", "logout", logout)
api.Register("POST", "password", password)
}

func me(ctx *gin.Context) {
id := ctx.GetString("user")

if id == "" {
Fail(ctx, "未登录")
return
}

OK(ctx, gin.H{"id": id})
}
21 changes: 21 additions & 0 deletions admin/boot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package admin

import (
"github.com/god-jason/bucket/boot"
"github.com/god-jason/bucket/web"
)

func init() {
boot.Register("admin", &boot.Task{
Startup: Startup,
Shutdown: nil,
Depends: []string{"config", "web"},
})
}

func Startup() error {

web.Engine.POST("api/login", login)

return nil
}
10 changes: 10 additions & 0 deletions admin/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package admin

import "github.com/god-jason/bucket/config"

const MODULE = "admin"

func init() {
config.Register(MODULE, "password", md5hash("123456"))
config.Register(MODULE, "lock", false)
}
45 changes: 45 additions & 0 deletions admin/login.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package admin

import (
"crypto/md5"
"encoding/hex"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
"github.com/god-jason/bucket/config"
)

type loginObj struct {
Password string `json:"password"`
Remember bool `json:"remember"`
}

func md5hash(text string) string {
h := md5.New()
h.Write([]byte(text))
sum := h.Sum(nil)
return hex.EncodeToString(sum)
}

func login(ctx *gin.Context) {
session := sessions.Default(ctx)

var obj loginObj
if err := ctx.ShouldBind(&obj); err != nil {
Error(ctx, err)
return
}

password := config.GetString(MODULE, "password")
if password != obj.Password {
Fail(ctx, "密码错误")
return
}

//_, _ = db.Engine.InsertOne(&types.UserEvent{UserId: user.id, ModEvent: types.ModEvent{Type: "登录"}})

//存入session
session.Set("user", "admin")
_ = session.Save()

OK(ctx, gin.H{"id": "admin"})
}
22 changes: 22 additions & 0 deletions admin/logout.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package admin

import (
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
)

func logout(ctx *gin.Context) {
session := sessions.Default(ctx)
u := session.Get("user")
if u == nil {
Fail(ctx, "未登录")
return
}

//user := u.(int64)
//_, _ = db.Engine.InsertOne(&types.UserEvent{UserId: user, ModEvent: types.ModEvent{Type: "退出"}})

session.Clear()
_ = session.Save()
OK(ctx, nil)
}
40 changes: 40 additions & 0 deletions admin/password.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package admin

import (
"github.com/gin-gonic/gin"
"github.com/god-jason/bucket/config"
)

type passwordObj struct {
Old string `json:"old"`
New string `json:"new"`
}

func password(ctx *gin.Context) {
if config.GetBool(MODULE, "lock") {
Fail(ctx, "禁止修改密码")
return
}

var obj passwordObj
if err := ctx.ShouldBind(&obj); err != nil {
Error(ctx, err)
return
}

if obj.Old != config.GetString(MODULE, "password") {
Fail(ctx, "密码错误")
return
}

//更新密码
config.Set(MODULE, "password", obj.New)

err := config.Store()
if err != nil {
Error(ctx, err)
return
}

OK(ctx, nil)
}
33 changes: 33 additions & 0 deletions admin/reply.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package admin

import (
"github.com/gin-gonic/gin"
"net/http"
)

type ReplyData[T any] struct {
Data T `json:"data"`
Error string `json:"error,omitempty"`
}

type ReplyList[T any] struct {
Data []T `json:"data"`
Total int64 `json:"total"`
Error string `json:"error,omitempty"`
}

func List(ctx *gin.Context, data any, total int64) {
ctx.JSON(http.StatusOK, gin.H{"data": data, "total": total})
}

func OK(ctx *gin.Context, data any) {
ctx.JSON(http.StatusOK, gin.H{"data": data})
}

func Fail(ctx *gin.Context, err string) {
ctx.JSON(http.StatusOK, gin.H{"error": err})
}

func Error(ctx *gin.Context, err error) {
ctx.JSON(http.StatusOK, gin.H{"error": err.Error()})
}
2 changes: 1 addition & 1 deletion function/boot.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ func init() {
boot.Register("function", &boot.Task{
Startup: Startup, //启动
Shutdown: nil,
Depends: []string{"log", "database"},
Depends: []string{"log"},
})
}

Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
_ "github.com/god-jason/bucket/admin"
"github.com/god-jason/bucket/api"
"github.com/god-jason/bucket/boot"
_ "github.com/god-jason/bucket/function"
Expand Down
33 changes: 17 additions & 16 deletions accumulate/accumulate.go → pkg/accumulate/accumulate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ import (
"strings"
)

type Result struct {
Target string
Filter map[string]any
Document map[string]any
}

type Field struct {
Key string
Value any
Expand All @@ -21,10 +15,17 @@ type Field struct {
_value gval.Evaluable
}

type Result struct {
Target string
MetaFields map[string]any
TargetFields map[string]any
}

// Accumulation 累积器,空间换时间,主要用于统计
type Accumulation struct {
Target string `json:"target"`
Filter map[string]any `json:"filter"`
Document map[string]any `json:"document"`
Target string `json:"target"`
MetaFields map[string]any `json:"meta_fields"`
TargetFields map[string]any `json:"target_fields"`

_target gval.Evaluable
_filter map[string]gval.Evaluable
Expand All @@ -40,7 +41,7 @@ func (a *Accumulation) Init() (err error) {
}

a._filter = make(map[string]gval.Evaluable)
for key, value := range a.Filter {
for key, value := range a.MetaFields {
if val, ok := value.(string); ok {
if expr, has := strings.CutPrefix(val, "="); has {
a._filter[key], err = calc.New(expr)
Expand All @@ -51,7 +52,7 @@ func (a *Accumulation) Init() (err error) {
}
}

for key, value := range a.Document {
for key, value := range a.TargetFields {

f := &Field{Key: key, Value: value}

Expand Down Expand Up @@ -93,19 +94,19 @@ func (a *Accumulation) Evaluate(args any) (result *Result, err error) {
}

//过滤器
ret.Filter = make(map[string]any)
ret.MetaFields = make(map[string]any)
for key, value := range a._filter {
if value != nil {
ret.Filter[key], err = a._target(context.Background(), args)
ret.MetaFields[key], err = a._target(context.Background(), args)
if err != nil {
return
}
} else {
ret.Filter[key] = a.Filter[key]
ret.MetaFields[key] = a.MetaFields[key]
}
}

ret.Document = make(map[string]any)
ret.TargetFields = make(map[string]any)

for _, f := range a._fields {
key := f.Key
Expand All @@ -129,7 +130,7 @@ func (a *Accumulation) Evaluate(args any) (result *Result, err error) {
}
}

ret.Document[key] = val
ret.TargetFields[key] = val
}

return &ret, nil
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
18 changes: 9 additions & 9 deletions table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package table
import (
"errors"
"github.com/dop251/goja"
"github.com/god-jason/bucket/accumulate"
"github.com/god-jason/bucket/log"
"github.com/god-jason/bucket/mongodb"
"github.com/god-jason/bucket/pkg/accumulate"
"github.com/god-jason/bucket/pkg/exception"
"github.com/god-jason/bucket/pkg/javascript"
"github.com/god-jason/bucket/pool"
Expand Down Expand Up @@ -156,8 +156,8 @@ func (t *Table) Insert(doc any) (id string, err error) {
continue
}

if len(ret.Document) > 0 {
_, err = mongodb.UpdateMany(ret.Target, ret.Filter, bson.M{"$inc": ret.Document}, true)
if len(ret.TargetFields) > 0 {
_, err = mongodb.UpdateMany(ret.Target, ret.MetaFields, bson.M{"$inc": ret.TargetFields}, true)
if err != nil {
log.Error(err)
}
Expand Down Expand Up @@ -302,8 +302,8 @@ func (t *Table) Delete(id string) error {
log.Error(err)
continue
}
if len(ret.Document) > 0 {
_, err = mongodb.UpdateMany(ret.Target, ret.Filter, bson.M{"$dec": ret.Document}, false)
if len(ret.TargetFields) > 0 {
_, err = mongodb.UpdateMany(ret.Target, ret.MetaFields, bson.M{"$dec": ret.TargetFields}, false)
if err != nil {
log.Error(err)
}
Expand Down Expand Up @@ -408,8 +408,8 @@ func (t *Table) Update(id string, update any) error {
log.Error(err)
continue
}
if len(ret.Document) > 0 {
_, err = mongodb.UpdateMany(ret.Target, ret.Filter, bson.M{"$dec": ret.Document}, true)
if len(ret.TargetFields) > 0 {
_, err = mongodb.UpdateMany(ret.Target, ret.MetaFields, bson.M{"$dec": ret.TargetFields}, true)
if err != nil {
log.Error(err)
}
Expand All @@ -427,8 +427,8 @@ func (t *Table) Update(id string, update any) error {
log.Error(err)
continue
}
if len(ret.Document) > 0 {
_, err = mongodb.UpdateMany(ret.Target, ret.Filter, bson.M{"$inc": ret.Document}, true)
if len(ret.TargetFields) > 0 {
_, err = mongodb.UpdateMany(ret.Target, ret.MetaFields, bson.M{"$inc": ret.TargetFields}, true)
if err != nil {
log.Error(err)
}
Expand Down

0 comments on commit 6270b2c

Please sign in to comment.