Skip to content

Commit 6962441

Browse files
masx200claude
andcommitted
删除convertSessions函数并将Session结构体统一提取到types包
- 删除go_ws_sh/GenerateRoutes.go中的convertSessions函数,简化代码结构 - 将Session结构体定义移动到types包中,实现统一管理 - 更新所有使用Session类型的文件,统一使用types.Session - 清理未使用的time导入,优化代码组织 - 确保所有引用都指向types包中的Session结构体 - 保持代码编译通过,无类型冲突 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 07cdd14 commit 6962441

12 files changed

+47
-64
lines changed

go_ws_sh/GenerateRoutes.go

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,13 @@ import (
88
)
99

1010
// GenerateRoutesHttp 根据 openapi 文件生成路由配置
11-
func GenerateRoutesHttp(credentialdb *gorm.DB, tokendb *gorm.DB, sessiondb *gorm.DB, initial_credentials types.InitialCredentials, initial_sessions []Session) []routes.RouteConfig {
11+
func GenerateRoutesHttp(credentialdb *gorm.DB, tokendb *gorm.DB, sessiondb *gorm.DB, initial_credentials types.InitialCredentials, initial_sessions []types.Session) []routes.RouteConfig {
1212
tokenRoutes := routes.GenerateTokenRoutes(credentialdb, tokendb, sessiondb)
1313
credentialRoutes := routes.GenerateCredentialRoutes(credentialdb, tokendb, sessiondb, initial_credentials)
14-
sessionRoutes := routes.GenerateSessionRoutes(credentialdb, tokendb, sessiondb, convertSessions(initial_sessions))
14+
sessionRoutes := routes.GenerateSessionRoutes(credentialdb, tokendb, sessiondb, initial_sessions)
1515

1616
allRoutes := append(tokenRoutes, credentialRoutes...)
1717
allRoutes = append(allRoutes, sessionRoutes...)
1818

1919
return allRoutes
2020
}
21-
22-
func convertSessions(sessions []Session) []routes.Session {
23-
result := make([]routes.Session, len(sessions))
24-
for i, session := range sessions {
25-
result[i] = routes.Session{
26-
Name: session.Name,
27-
Cmd: session.Cmd,
28-
Args: session.Args,
29-
Dir: session.Dir,
30-
CreatedAt: session.CreatedAt,
31-
UpdatedAt: session.UpdatedAt,
32-
}
33-
}
34-
return result
35-
}

go_ws_sh/HandleWebSocketProcess.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88

99
"github.com/hertz-contrib/websocket"
1010
"github.com/linkedin/goavro/v2"
11+
12+
"github.com/masx200/go_ws_sh/types"
1113
)
1214

1315
func SendTextMessage(conn *websocket.Conn, typestring string, body string, binaryandtextchannel *SafeChannel[WebsocketMessage]) error {
@@ -33,7 +35,7 @@ type WebsocketMessage struct {
3335
Type int
3436
}
3537

36-
func HandleWebSocketProcess(session Session, codec *goavro.Codec, conn *websocket.Conn) error {
38+
func HandleWebSocketProcess(session types.Session, codec *goavro.Codec, conn *websocket.Conn) error {
3739
defer func() {
3840
if r := recover(); r != nil {
3941
log.Println("Recovered in panic", r)

go_ws_sh/ReadSessionsFromDB.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,25 @@ import (
44
"encoding/json"
55

66
"gorm.io/gorm"
7+
"github.com/masx200/go_ws_sh/types"
78
)
89

910
// ReadAllSessions 从 sessiondb 中读取所有的 SessionStore 并转换为 Session 结构体切片
10-
func ReadAllSessions(sessiondb *gorm.DB) ([]Session, error) {
11+
func ReadAllSessions(sessiondb *gorm.DB) ([]types.Session, error) {
1112
var sessionStores []SessionStore
1213
// 查询 sessiondb 中的所有 SessionStore 记录
1314
if err := sessiondb.Find(&sessionStores).Error; err != nil {
1415
return nil, err
1516
}
1617

17-
sessions := make([]Session, 0, len(sessionStores))
18+
sessions := make([]types.Session, 0, len(sessionStores))
1819
for _, store := range sessionStores {
1920
var args []string
2021
// 将 Args 字段(字符串形式)反序列化为字符串切片
2122
if err := json.Unmarshal([]byte(store.Args), &args); err != nil {
2223
return nil, err
2324
}
24-
session := Session{
25+
session := types.Session{
2526
Name: store.Name,
2627
Cmd: store.Cmd,
2728
Args: args,
@@ -35,7 +36,7 @@ func ReadAllSessions(sessiondb *gorm.DB) ([]Session, error) {
3536
}
3637

3738
// ReadAllSessions 从 sessiondb 中读取所有的 SessionStore 并转换为 Session 结构体切片
38-
func ReadAllSessionsWithName(sessiondb *gorm.DB, name string) ([]Session, error) {
39+
func ReadAllSessionsWithName(sessiondb *gorm.DB, name string) ([]types.Session, error) {
3940
var sessionStores []SessionStore
4041
// 查询 sessiondb 中的所有 SessionStore 记录
4142
if err := sessiondb.Where(
@@ -44,14 +45,14 @@ func ReadAllSessionsWithName(sessiondb *gorm.DB, name string) ([]Session, error)
4445
return nil, err
4546
}
4647

47-
sessions := make([]Session, 0, len(sessionStores))
48+
sessions := make([]types.Session, 0, len(sessionStores))
4849
for _, store := range sessionStores {
4950
var args []string
5051
// 将 Args 字段(字符串形式)反序列化为字符串切片
5152
if err := json.Unmarshal([]byte(store.Args), &args); err != nil {
5253
return nil, err
5354
}
54-
session := Session{
55+
session := types.Session{
5556
Name: store.Name,
5657
Cmd: store.Cmd,
5758
Args: args,

go_ws_sh/ServerConfig.go

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
package go_ws_sh
22

33
import (
4-
"time"
5-
64
"github.com/masx200/go_ws_sh/types"
75
)
86

97
type ConfigServer struct {
108
SessionFile string `json:"session_file"`
119
CredentialFile string `json:"credential_file"`
12-
InitialSessions []Session `json:"initial_sessions"`
10+
InitialSessions []types.Session `json:"initial_sessions"`
1311
Servers []ServerConfig `json:"servers"`
1412

1513
TokenFile string `json:"token_file"`
@@ -18,16 +16,6 @@ type ConfigServer struct {
1816

1917
}
2018

21-
type Session struct {
22-
Name string `json:"name"`
23-
24-
Cmd string `json:"cmd"`
25-
Args []string `json:"args"`
26-
Dir string `json:"dir"`
27-
CreatedAt time.Time `json:"created_at"`
28-
UpdatedAt time.Time `json:"updated_at"`
29-
}
30-
3119
type ServerConfig struct {
3220
Alpn string `json:"alpn"`
3321
Port string `json:"port"`

go_ws_sh/ServerRouterHTTP.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ func GetSessionsHandler(credentialdb *gorm.DB, tokendb *gorm.DB, sessiondb *gorm
505505
}
506506
log.Println("Username:", username)
507507
}
508-
var sessions []Session
508+
var sessions []types.Session
509509
if body.Session.Name != "" {
510510

511511
log.Println("查询Name:", body.Session.Name)

go_ws_sh/SessionConverter.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ package go_ws_sh
22

33
import (
44
"github.com/golang-module/carbon/v2"
5+
"github.com/masx200/go_ws_sh/types"
56
)
67

7-
// SessionsToMapSlice 将 []Session 转换为 []map[string]any
8-
func SessionsToMapSlice(sessions []Session) []map[string]any {
8+
// SessionsToMapSlice 将 []types.Session 转换为 []map[string]any
9+
func SessionsToMapSlice(sessions []types.Session) []map[string]any {
910
result := make([]map[string]any, len(sessions))
1011
for i, session := range sessions {
1112
result[i] = map[string]any{

go_ws_sh/createhandleWebSocket.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"github.com/cloudwego/hertz/pkg/app"
99
"github.com/cloudwego/hertz/pkg/protocol/consts"
1010
"github.com/hertz-contrib/websocket"
11+
12+
"github.com/masx200/go_ws_sh/types"
1113
)
1214

1315
// createHandleWebSocket 创建处理WebSocket连接的函数
@@ -18,7 +20,7 @@ import (
1820
// 返回值:
1921
//
2022
// 一个函数,用于处理WebSocket连接请求
21-
func createhandleWebSocket(session Session) func(w context.Context, r *app.RequestContext) {
23+
func createhandleWebSocket(session types.Session) func(w context.Context, r *app.RequestContext) {
2224
return func(w context.Context, r *app.RequestContext) {
2325
codec, err := create_msg_codec()
2426
if err != nil {

go_ws_sh/deletesessionhandler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
)
1212

1313

14-
func DeleteSessionHandler(credentialdb *gorm.DB, tokendb *gorm.DB, sessiondb *gorm.DB, c context.Context, r *app.RequestContext, initial_sessions []Session) {
14+
func DeleteSessionHandler(credentialdb *gorm.DB, tokendb *gorm.DB, sessiondb *gorm.DB, c context.Context, r *app.RequestContext, initial_sessions []types.Session) {
1515
// 定义请求体结构体
1616
var body struct {
1717
Session struct {

go_ws_sh/generaterouteshttpsessions.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
)
1111

1212

13-
func GenerateRoutesHttpsessions(credentialdb *gorm.DB, tokendb *gorm.DB, sessiondb *gorm.DB, initial_sessions []Session) []types.RouteConfig {
13+
func GenerateRoutesHttpsessions(credentialdb *gorm.DB, tokendb *gorm.DB, sessiondb *gorm.DB, initial_sessions []types.Session) []types.RouteConfig {
1414
routes_copy_and_move := []types.RouteConfig{
1515

1616
{

go_ws_sh/movemiddleware.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
)
1414

1515

16-
func MoveMiddleware(initial_sessions []Session, credentialdb *gorm.DB, tokendb *gorm.DB, sessiondb *gorm.DB, c context.Context, r *app.RequestContext, next types.HertzNext) {
16+
func MoveMiddleware(initial_sessions []types.Session, credentialdb *gorm.DB, tokendb *gorm.DB, sessiondb *gorm.DB, c context.Context, r *app.RequestContext, next types.HertzNext) {
1717
// 定义请求体结构体
1818
var body struct {
1919
Session struct {

0 commit comments

Comments
 (0)