Skip to content

Commit

Permalink
增加同步应用商店任务
Browse files Browse the repository at this point in the history
  • Loading branch information
donknap committed Dec 19, 2024
1 parent 8920795 commit 1d1cf1f
Show file tree
Hide file tree
Showing 9 changed files with 181 additions and 14 deletions.
1 change: 1 addition & 0 deletions .run/dpanel.run.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<working_directory value="$PROJECT_DIR$" />
<go_parameters value="-tags pe" />
<parameters value="server:start -f config.yaml" />
<!-- <parameters value="store:sync -f config.yaml &#45;&#45;name=onepanel" />-->
<envs>
<env name="STORAGE_LOCAL_PATH" value="$PROJECT_DIR$/data" />
<env name="DOCKER_HOST" value="unix://$USER_HOME$/.docker/run/docker.sock" />
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ clean:
docker buildx prune -a -f
docker stop buildx_buildkit_dpanel-builder0 && docker rm /buildx_buildkit_dpanel-builder0
all: clean-source js amd64 arm64 armv7
test: all
test:
docker buildx build \
-t registry.cn-hangzhou.aliyuncs.com/dpanel/dpanel:${VERSION}-lite \
--platform linux/arm64,linux/amd64 \
Expand Down
11 changes: 6 additions & 5 deletions app/common/http/controller/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,12 @@ func (self Store) Sync(http *gin.Context) {
}
}

if params.Id > 0 {
storeRow, _ := dao.Store.Where(dao.Store.ID.Eq(params.Id)).First()
storeRow.Setting.Apps = appList
storeRow.Setting.UpdatedAt = time.Now().Unix()
_, _ = dao.Store.Updates(storeRow)
if params.Id > 0 && len(appList) > 0 {
if storeRow, _ := dao.Store.Where(dao.Store.ID.Eq(params.Id)).First(); storeRow != nil {
storeRow.Setting.Apps = appList
storeRow.Setting.UpdatedAt = time.Now().Unix()
_, _ = dao.Store.Updates(storeRow)
}
}

self.JsonResponseWithoutError(http, gin.H{
Expand Down
58 changes: 58 additions & 0 deletions app/ctrl/command/store/sync.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package store

import (
"github.com/donknap/dpanel/app/ctrl/logic"
"github.com/donknap/dpanel/common/dao"
"github.com/gin-gonic/gin"
"github.com/gookit/color"
"github.com/spf13/cobra"
"github.com/we7coreteam/w7-rangine-go/v2/src/console"
"time"
)

type Sync struct {
console.Abstract
}

func (self Sync) GetName() string {
return "store:sync"
}

func (self Sync) GetDescription() string {
return "同步应用商店数据"
}

func (self Sync) Configure(command *cobra.Command) {
command.Flags().String("name", "", "商店标识")
_ = command.MarkFlagRequired("name")
}

func (self Sync) Handle(cmd *cobra.Command, args []string) {
name, _ := cmd.Flags().GetString("name")
code, err := logic.User{}.GetAuth(time.Now().Add(time.Minute))
if err != nil {
color.Error.Println(err)
return
}
store, _ := dao.Store.Where(dao.Store.Name.Eq(name)).First()
if store == nil {
color.Error.Println("商店不存在,请先添加")
return
}
out, err := logic.Proxy{}.Post("/api/common/store/sync", code, gin.H{
"id": store.ID,
"name": store.Name,
"type": store.Setting.Type,
"url": store.Setting.Url,
})
if err != nil {
color.Error.Println(err)
return
}
appList := out.Data.(map[string]interface{})
if _, ok := appList["list"]; ok {
color.Successln("商店数据同步成功", "共同步了", len(appList["list"].([]interface{})), "条应用数据")
} else {
color.Successln("商店数据同步成功")
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package command
package user

import (
"github.com/donknap/dpanel/app/common/logic"
Expand All @@ -8,24 +8,24 @@ import (
"github.com/we7coreteam/w7-rangine-go/v2/src/console"
)

type UserReset struct {
type Reset struct {
console.Abstract
}

func (self UserReset) GetName() string {
func (self Reset) GetName() string {
return "user:reset"
}

func (self UserReset) GetDescription() string {
func (self Reset) GetDescription() string {
return "重置面板用户名或是密码"
}

func (self UserReset) Configure(command *cobra.Command) {
func (self Reset) Configure(command *cobra.Command) {
command.Flags().String("password", "", "重置管理员密码")
command.Flags().String("username", "", "重置管理员用户名")
}

func (self UserReset) Handle(cmd *cobra.Command, args []string) {
func (self Reset) Handle(cmd *cobra.Command, args []string) {
founder, _ := dao.Setting.
Where(dao.Setting.GroupName.Eq(logic.SettingGroupUser)).
Where(dao.Setting.Name.Eq(logic.SettingGroupUserFounder)).First()
Expand Down
63 changes: 63 additions & 0 deletions app/ctrl/logic/proxy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package logic

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"github.com/we7coreteam/w7-rangine-go/v2/pkg/support/facade"
"io"
"log/slog"
"net/http"
"strings"
)

type message struct {
Code int `json:"code"`
Error string `json:"error"`
Data interface{} `json:"data"`
}

type Proxy struct {
}

func (self Proxy) Post(uri string, token string, payload any) (responseMessage message, err error) {
jsonData, err := json.Marshal(payload)
if err != nil {
return responseMessage, err
}
uri = fmt.Sprintf("http://127.0.0.1:%s/%s", facade.GetConfig().GetString("server.http.port"), strings.Trim(uri, "/"))

slog.Debug("cli proxy post", "uri", uri)
request, err := http.NewRequest("POST", uri, bytes.NewBuffer(jsonData))
if err != nil {
return responseMessage, err
}
request.Header.Set("Content-Type", "application/json")
request.Header.Set("Authorization", "Bearer "+token)
client := &http.Client{}
response, err := client.Do(request)
if err != nil {
return responseMessage, err
}
defer func() {
_ = response.Body.Close()
}()

buffer := new(bytes.Buffer)
_, _ = io.Copy(buffer, response.Body)

if err = json.Unmarshal(buffer.Bytes(), &responseMessage); err == nil {
switch response.StatusCode {
case 500:
return responseMessage, errors.New(responseMessage.Error)
case 401:
return responseMessage, errors.New("invalid auth token")
case 200:
return responseMessage, nil
}
} else {
return responseMessage, err
}
return responseMessage, errors.New("unknown response status code")
}
27 changes: 27 additions & 0 deletions app/ctrl/logic/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package logic

import (
"github.com/donknap/dpanel/app/common/logic"
"github.com/golang-jwt/jwt/v5"
"time"
)

type User struct {
}

func (self User) GetAuth(expireTime time.Time) (string, error) {
currentUser, err := logic.Setting{}.GetValue(logic.SettingGroupUser, logic.SettingGroupUserFounder)
if err != nil {
return "", err
}
jwtSecret := logic.User{}.GetJwtSecret()
jwtClaims := jwt.NewWithClaims(jwt.SigningMethodHS256, logic.UserInfo{
UserId: currentUser.ID,
Username: currentUser.Value.Username,
RoleIdentity: currentUser.Name,
RegisteredClaims: jwt.RegisteredClaims{
ExpiresAt: jwt.NewNumericDate(expireTime),
},
})
return jwtClaims.SignedString(jwtSecret)
}
6 changes: 4 additions & 2 deletions app/ctrl/provider.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package ctrl

import (
"github.com/donknap/dpanel/app/ctrl/command"
"github.com/donknap/dpanel/app/ctrl/command/store"
"github.com/donknap/dpanel/app/ctrl/command/user"
"github.com/we7coreteam/w7-rangine-go/v2/pkg/support/console"
)

type Provider struct {
}

func (provider *Provider) Register(console console.Console) {
console.RegisterCommand(new(command.UserReset))
console.RegisterCommand(new(user.Reset))
console.RegisterCommand(new(store.Sync))
}
15 changes: 15 additions & 0 deletions docker/script/store-sync/data.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
task:
name: store-sync
descriptionZh: |
同步某个应用商店应用
descriptionEn: |
sync appstore
script: |
/app/server/dpanel store:sync -f /app/server/config.yaml --name=${STORE_NAME}
tag:
- dpanel
- run-in-dpanel
environment:
- name: STORE_NAME
labelZh: 商店名称
labelEn: name of appstore

0 comments on commit 1d1cf1f

Please sign in to comment.