Skip to content

Commit

Permalink
config
Browse files Browse the repository at this point in the history
  • Loading branch information
it512 committed Dec 21, 2024
1 parent 71327bc commit 02259b0
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 77 deletions.
67 changes: 67 additions & 0 deletions wechat/cmd/wechat-proxy/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package config

import (
"context"
"log"
"net/http"

"github.com/silenceper/wechat/v2"
"github.com/silenceper/wechat/v2/cache"
"github.com/silenceper/wechat/v2/miniprogram"
miniConfig "github.com/silenceper/wechat/v2/miniprogram/config"
"github.com/spf13/viper"
"github.com/twiglab/crm/wechat/web"
)

type Web struct {
Addr string `yaml:"addr" mapstructure:"addr"`
}

func (c Web) Create(ctx context.Context) *http.Server {
return web.NewHttpServer(ctx, c.Addr, nil)
}

type Wechat struct {
AppID string `yaml:"app_id" mapstructure:"app_id"`
AppSecret string `yaml:"app_secret" mapstructure:"app_secret"`

MchId string `yaml:"mcd_id" mapstructure:"mcd_id"`
SerialNo string `yaml:"serial_no" mapstructure:"serial_no"`
APIKey string `yaml:"api_key" mapstructure:"api_key"` // 微信商户平台—>账户设置—>API安全—>设置APIv3密钥
PrivateKey string `yaml:"private_key" mapstructure:"private_key"`
}

func (c Wechat) CreateMiniProfram() *miniprogram.MiniProgram {
wc := wechat.NewWechat()
memory := cache.NewMemory()
miniCfg := &miniConfig.Config{
AppID: c.AppID,
AppSecret: c.AppSecret,
Cache: memory,
}
return wc.GetMiniProgram(miniCfg)

}

type App struct {
ID string `yaml:"id" mapstructure:"id"`
Wechat Wechat `yaml:"wechat" mapstructure:"wechat"`
Web Web `yaml:"web" mapstructure:"web"`
}

func InitConfig(config any) {
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath(".")

viper.AutomaticEnv()

if err := viper.ReadInConfig(); err != nil {
log.Fatal(err)

}

if err := viper.Unmarshal(config); err != nil {
log.Fatal(err)
}
}
28 changes: 8 additions & 20 deletions wechat/cmd/wechat-proxy/main.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package main

import (
"github.com/it512/box"
"github.com/silenceper/wechat/v2"
"github.com/silenceper/wechat/v2/cache"
miniConfig "github.com/silenceper/wechat/v2/miniprogram/config"
"github.com/twiglab/crm/wechat/config"
"log"

"github.com/it512/box"

_ "github.com/joho/godotenv/autoload"
"github.com/twiglab/crm/wechat/cmd/wechat-proxy/config"
"github.com/twiglab/crm/wechat/sns"
"github.com/twiglab/crm/wechat/web"

Expand All @@ -18,20 +16,10 @@ import (
)

func main() {
if err := config.InitConfig(); err != nil {
panic(err)
}

cfg := config.GetConfig()
cfg := &config.App{}
config.InitConfig(cfg)

wc := wechat.NewWechat()
memory := cache.NewMemory()
miniCfg := &miniConfig.Config{
AppID: cfg.Wechat.AppId,
AppSecret: cfg.Wechat.AppSecret,
Cache: memory,
}
prog := wc.GetMiniProgram(miniCfg)
prog := cfg.Wechat.CreateMiniProfram()

auth := sns.NewAuth(prog.GetAuth())

Expand All @@ -42,6 +30,6 @@ func main() {
mux.Use(middleware.Logger, middleware.Recoverer)
mux.Mount("/gqlrpc", gql.New(auth))

svr := web.NewHttpServer(ctx, cfg.App.Addr, mux)
log.Fatal(web.RunServer(ctx, svr))
svr := cfg.Web.Create(ctx)
log.Fatal(web.RunServer2(ctx, svr, mux))
}
57 changes: 0 additions & 57 deletions wechat/config/config.go

This file was deleted.

0 comments on commit 02259b0

Please sign in to comment.