-
-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
181 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("商店数据同步成功") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |