Skip to content

Commit 302cac6

Browse files
committed
update config
1 parent cb3755b commit 302cac6

File tree

6 files changed

+57
-54
lines changed

6 files changed

+57
-54
lines changed

cmd/api.go

+2-22
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package cmd
22

33
import (
4-
"fmt"
54
"log"
65

76
"github.com/chatmcp/mcprouter/router"
87
"github.com/chatmcp/mcprouter/service/api"
9-
"github.com/chatmcp/mcprouter/util"
108
"github.com/spf13/cobra"
119
"github.com/spf13/viper"
1210
)
@@ -27,29 +25,11 @@ var apiCmd = &cobra.Command{
2725
Short: "start api server",
2826
Long: `start api server`,
2927
Run: func(cmd *cobra.Command, args []string) {
30-
if err := util.InitConfigWithFile(apiConfigFile); err != nil {
31-
fmt.Printf("init config failed with file: %s, %v\n", apiConfigFile, err)
28+
if err := Init(); err != nil {
29+
log.Printf("init failed with error: %v", err)
3230
return
3331
}
3432

35-
log.Println("config initialized")
36-
37-
if viper.GetBool("app.use_db") && viper.GetString("app.db_name") != "" {
38-
if err := util.InitDBWithName(viper.GetString("app.db_name")); err != nil {
39-
fmt.Printf("init db failed with name: %s, %v\n", viper.GetString("app.db_name"), err)
40-
return
41-
}
42-
log.Println("db initialized")
43-
}
44-
45-
if viper.GetBool("app.use_cache") && viper.GetString("app.cache_name") == "redis" {
46-
if err := util.InitRedisWithName(viper.GetString("app.cache_name")); err != nil {
47-
fmt.Printf("init redis failed with name: %s, %v\n", viper.GetString("app.cache_name"), err)
48-
return
49-
}
50-
log.Println("redis initialized")
51-
}
52-
5333
port := viper.GetInt("api_server.port")
5434
if port == 0 {
5535
port = 8027

cmd/proxy.go

+2-22
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package cmd
22

33
import (
4-
"fmt"
54
"log"
65

76
"github.com/chatmcp/mcprouter/router"
87
"github.com/chatmcp/mcprouter/service/proxy"
9-
"github.com/chatmcp/mcprouter/util"
108
"github.com/spf13/cobra"
119
"github.com/spf13/viper"
1210
)
@@ -27,29 +25,11 @@ var proxyCmd = &cobra.Command{
2725
Short: "start proxy server",
2826
Long: `start proxy server`,
2927
Run: func(cmd *cobra.Command, args []string) {
30-
if err := util.InitConfigWithFile(proxyConfigFile); err != nil {
31-
fmt.Printf("init config failed with file: %s, %v\n", proxyConfigFile, err)
28+
if err := Init(); err != nil {
29+
log.Printf("init failed with error: %v", err)
3230
return
3331
}
3432

35-
log.Println("config initialized")
36-
37-
if viper.GetBool("app.use_db") && viper.GetString("app.db_name") != "" {
38-
if err := util.InitDBWithName(viper.GetString("app.db_name")); err != nil {
39-
fmt.Printf("init db failed with name: %s, %v\n", viper.GetString("app.db_name"), err)
40-
return
41-
}
42-
log.Println("db initialized")
43-
}
44-
45-
if viper.GetBool("app.use_cache") && viper.GetString("app.cache_name") == "redis" {
46-
if err := util.InitRedisWithName(viper.GetString("app.cache_name")); err != nil {
47-
fmt.Printf("init redis failed with name: %s, %v\n", viper.GetString("app.cache_name"), err)
48-
return
49-
}
50-
log.Println("redis initialized")
51-
}
52-
5333
port := viper.GetInt("proxy_server.port")
5434
if port == 0 {
5535
port = 8025

cmd/root.go

+36
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package cmd
22

33
import (
4+
"fmt"
5+
"log"
46
"os"
57

8+
"github.com/chatmcp/mcprouter/util"
69
"github.com/spf13/cobra"
10+
"github.com/spf13/viper"
711
)
812

913
// rootCmd represents the base command when called without any subcommands
@@ -25,6 +29,38 @@ func Execute() {
2529
}
2630
}
2731

32+
func Init() error {
33+
if err := util.InitConfigWithFile(proxyConfigFile); err != nil {
34+
fmt.Printf("init config failed with file: %s, %v\n", proxyConfigFile, err)
35+
return err
36+
}
37+
38+
log.Println("config initialized")
39+
40+
dbs := []string{viper.GetString("app.web_db_name"), viper.GetString("app.api_db_name")}
41+
if viper.GetBool("app.use_db") {
42+
for _, db := range dbs {
43+
if db != "" {
44+
if err := util.InitDBWithName(db); err != nil {
45+
fmt.Printf("init db failed with name: %s, %v\n", db, err)
46+
return err
47+
}
48+
log.Printf("db %s initialized", db)
49+
}
50+
}
51+
}
52+
53+
if viper.GetBool("app.use_cache") && viper.GetString("app.cache_name") == "redis" {
54+
if err := util.InitRedisWithName(viper.GetString("app.cache_name")); err != nil {
55+
fmt.Printf("init redis failed with name: %s, %v\n", viper.GetString("app.cache_name"), err)
56+
return err
57+
}
58+
log.Printf("redis %s initialized", viper.GetString("app.cache_name"))
59+
}
60+
61+
return nil
62+
}
63+
2864
func init() {
2965
rootCmd.PersistentFlags().StringP("version", "v", "0.0.1", "version")
3066
}

handler/proxy/mcp.go

+7
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,13 @@ func MCP(c echo.Context) error {
185185

186186
proxyInfoB, _ := json.Marshal(proxyInfo)
187187

188+
if proxyInfo.RequestMethod == "tools/call" {
189+
// if err := model.CreateServerLog(proxyInfo.ToServerLog()); err != nil {
190+
// log.Printf("save server log failed: %v\n", err)
191+
// } else {
192+
// log.Printf("save server log ok: %s\n", string(proxyInfoB))
193+
// }
194+
}
188195
log.Printf("proxyInfo: %s\n", string(proxyInfoB))
189196

190197
// notification

model/base.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@ import (
77
)
88

99
func db() *gorm.DB {
10-
name := viper.GetString("app.db_name")
10+
name := viper.GetString("app.web_db_name")
11+
12+
return util.GetClient(name)
13+
}
14+
15+
func adb() *gorm.DB {
16+
name := viper.GetString("app.api_db_name")
1117

1218
return util.GetClient(name)
1319
}

model/serverlog.go

+3-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package model
22

33
import (
4-
"encoding/json"
54
"errors"
5+
"fmt"
66
"time"
77
)
88

@@ -46,13 +46,7 @@ func CreateServerLog(sl *ServerLog) error {
4646
return errors.New("invalid server log")
4747
}
4848

49-
if sl.RequestParams != nil {
50-
sl.RequestParams, _ = json.Marshal(sl.RequestParams)
51-
}
52-
53-
if sl.ResponseResult != nil {
54-
sl.ResponseResult, _ = json.Marshal(sl.ResponseResult)
55-
}
49+
fmt.Printf("save server log: %+v\n", sl)
5650

57-
return db().Table(sl.TableName()).Create(sl).Error
51+
return adb().Table(sl.TableName()).Create(sl).Error
5852
}

0 commit comments

Comments
 (0)