micro-account/cmd/app.go
2025-02-21 16:57:10 +08:00

183 lines
5.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"dubbo.apache.org/dubbo-go/v3/common/logger"
"dubbo.apache.org/dubbo-go/v3/config"
_ "dubbo.apache.org/dubbo-go/v3/imports"
"fmt"
appconfig "github.com/fonchain_enterprise/micro-account/cmd/config"
msgconfig "github.com/fonchain_enterprise/micro-account/pkg/config"
"github.com/fonchain_enterprise/micro-account/pkg/cache"
"github.com/fonchain_enterprise/micro-account/pkg/common/dingding"
_ "github.com/fonchain_enterprise/micro-account/pkg/common/filter"
"github.com/fonchain_enterprise/micro-account/pkg/m"
"github.com/fonchain_enterprise/micro-account/pkg/model"
"github.com/fonchain_enterprise/micro-account/pkg/service"
"github.com/fonchain_enterprise/utils/zap_log"
"github.com/nacos-group/nacos-sdk-go/clients"
"github.com/nacos-group/nacos-sdk-go/common/constant"
"github.com/nacos-group/nacos-sdk-go/vo"
"os"
)
func main() {
//启动新日志
if err := bootstrap(); err != nil {
panic(err)
}
//注册服务
config.SetProviderService(&service.AccountProvider{})
//config.SetProviderService(&service.CoinProvider{})
if err := config.Load(); err != nil {
panic(err)
}
//logger.SetLogger(log.GetFakeLogger())
logger.SetLogger(zap_log.GetFakeLogger())
//dubbo日志
logger.Info("帐号微服务启动成功,git commit:", os.Getenv("GIT_COMMIT"))
select {}
}
func bootstrap() (err error) {
aliConfig, err := appconfig.LoadAliEnv(m.SERVER_CONFIG)
if err != nil {
return err
}
var configEnv *appconfig.Config
if aliConfig.System.Nacos == true {
fmt.Println("使用nacos")
configEnv, err = appconfig.LoadEnvFromFileInfo(loadAli(aliConfig))
} else {
fmt.Println("使用配置文件")
configEnv, err = appconfig.LoadEnv(m.SERVER_CONFIG)
}
if err != nil {
return err
}
fmt.Printf("\n当前环境%s,版本号:%s\n", configEnv.System.Mode, configEnv.System.Version)
err = zap_log.InitZapLog("../conf/log.yaml")
if err != nil {
return err
}
//数据库
mysqlConfig := model.MysqlConfig{
Db: configEnv.Mysql.Db,
DbHost: configEnv.Mysql.DbHost,
DbPort: configEnv.Mysql.DbPort,
DbUser: configEnv.Mysql.DbUser,
DbPassWord: configEnv.Mysql.DbPassWord,
DbName: configEnv.Mysql.DbName,
}
model.LoadEnv(mysqlConfig)
//redis
redisConfig := cache.RedisConfig{
RedisDB: configEnv.Redis.RedisDB,
RedisAddr: configEnv.Redis.RedisAddr,
RedisPw: configEnv.Redis.RedisPW,
RedisDbName: configEnv.Redis.RedisDBNAme,
}
cache.LoadRedis(redisConfig)
msgInfo := msgconfig.MobileConfigTemplate{
AK: configEnv.Mobile.AK,
SK: configEnv.Mobile.SK,
URL: configEnv.Mobile.URL,
}
aliInfo := msgconfig.AliMessage{
AK: configEnv.AlMobile.AK,
AS: configEnv.AlMobile.AS,
URL: configEnv.AlMobile.URL,
}
fmt.Println("飞鸽短信配置", msgInfo)
fmt.Println("阿里短信配置", aliInfo)
msgconfig.LoadData(msgInfo, aliInfo)
dingding.LoadAccessToken(configEnv.Mobile.DingDingKey)
//logger.SetLogger()
model.SetSendPhoneNUm(configEnv.System.Mode == "prod")
fmt.Println("是否创建邮箱", configEnv.Mail.IsProd)
//external.LoadEnv(configEnv.Mail.Username, configEnv.Mail.Password, configEnv.Mail.Domain, configEnv.Mail.IsProd)
return nil
}
func loadAli(aliConfig *appconfig.AliConfig) string {
fmt.Println("ali", aliConfig)
clientConfig := constant.ClientConfig{
NamespaceId: aliConfig.Nacos.NamespaceId, // 如果需要支持多namespace我们可以场景多个client,它们有不同的NamespaceId。当namespace是public时此处填空字符串。
TimeoutMs: 5000,
NotLoadCacheAtStart: true,
LogDir: "/tmp/nacos/log",
CacheDir: "/tmp/nacos/cache",
Username: aliConfig.Nacos.Username,
Password: aliConfig.Nacos.Password,
LogLevel: "debug",
}
//配置连接信息
serverConfigs := []constant.ServerConfig{
{
IpAddr: aliConfig.Nacos.IpAddr,
ContextPath: "/nacos",
Port: 80,
Scheme: "http",
},
}
// Create naming client for service discovery
configClient, err := clients.CreateConfigClient(map[string]interface{}{
"serverConfigs": serverConfigs,
"clientConfig": clientConfig,
})
if err != nil {
logger.Fatal(err)
}
//读取文件
content, err := configClient.GetConfig(vo.ConfigParam{
DataId: aliConfig.Nacos.DataId, //此处对应之前的网页配置的名称
Group: aliConfig.Nacos.Group, //此处对应之前的网页配置的分组
})
if err != nil {
logger.Fatal(err)
}
return content
}