Merge branch 'develop' of gitee.com:fusenpack/fusenapi into develop

This commit is contained in:
laodaming 2023-09-19 11:42:17 +08:00
commit 17117191f0
29 changed files with 208 additions and 124 deletions

7
env.yaml Normal file
View File

@ -0,0 +1,7 @@
nacos:
- server.fusen.3718.cn:8848
- server.fusen.3718.cn:8849
username: ...
password: ...
namespace: fs_server_api_dev
group: FS-SERVER-API

View File

@ -7,6 +7,7 @@ import (
"time"
"fusenapi/utils/auth"
"fusenapi/utils/fsconfig"
{{.importPackages}}
)
@ -16,8 +17,10 @@ var configFile = flag.String("f", "etc/{{.serviceName}}.yaml", "the config file"
func main() {
flag.Parse()
cfgContent := fsconfig.StartNacosConfig(configFile,nil)
var c config.Config
conf.MustLoad(*configFile, &c)
conf.LoadConfigFromYamlBytes([]byte(cfgContent),&c)
c.Timeout = int64(time.Second * 15)
server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) {
}))

View File

@ -13,7 +13,7 @@ func main() {
flag.Parse()
var c gateway.GatewayConf
conf.MustLoad(*configFile, &c)
c.Timeout = int64(time.Second * 15)
gw := gateway.MustNewServer(c)
defer gw.Stop()

View File

@ -18,8 +18,10 @@ var configFile = flag.String("f", "etc/{{.serviceName}}.yaml", "the config file"
func main() {
flag.Parse()
cfgContent := fsconfig.StartNacosConfig(configFile,nil)
var c config.Config
conf.MustLoad(*configFile, &c)
conf.LoadConfigFromYamlBytes([]byte(cfgContent),&c)
c.Timeout = int64(time.Second * 15)
ctx := svc.NewServiceContext(c)

View File

@ -7,6 +7,7 @@ import (
"time"
"fusenapi/utils/auth"
"fusenapi/utils/fsconfig"
{{.importPackages}}
)
@ -16,8 +17,10 @@ var configFile = flag.String("f", "etc/{{.serviceName}}.yaml", "the config file"
func main() {
flag.Parse()
cfgContent := fsconfig.StartNacosConfig(configFile,nil)
var c config.Config
conf.MustLoad(*configFile, &c)
conf.LoadConfigFromYamlBytes([]byte(cfgContent),&c)
c.Timeout = int64(time.Second * 15)
server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) {

View File

@ -13,7 +13,7 @@ func main() {
flag.Parse()
var c gateway.GatewayConf
conf.MustLoad(*configFile, &c)
c.Timeout = int64(time.Second * 15)
gw := gateway.MustNewServer(c)
defer gw.Stop()

View File

@ -18,8 +18,10 @@ var configFile = flag.String("f", "etc/{{.serviceName}}.yaml", "the config file"
func main() {
flag.Parse()
cfgContent := fsconfig.StartNacosConfig(configFile,nil)
var c config.Config
conf.MustLoad(*configFile, &c)
conf.LoadConfigFromYamlBytes([]byte(cfgContent),&c)
c.Timeout = int64(time.Second * 15)
ctx := svc.NewServiceContext(c)

View File

@ -6,6 +6,7 @@ import (
"net/http"
"fusenapi/utils/auth"
"fusenapi/utils/fsconfig"
"fusenapi/server/auth/internal/config"
"fusenapi/server/auth/internal/handler"
@ -20,8 +21,10 @@ var configFile = flag.String("f", "etc/auth.yaml", "the config file")
func main() {
flag.Parse()
cfgContent := fsconfig.StartNacosConfig(*configFile, nil)
var c config.Config
conf.MustLoad(*configFile, &c)
conf.LoadConfigFromYamlBytes([]byte(cfgContent), &c)
server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) {
}))
defer server.Stop()

View File

@ -6,6 +6,7 @@ import (
"net/http"
"fusenapi/utils/auth"
"fusenapi/utils/fsconfig"
"fusenapi/server/base/internal/config"
"fusenapi/server/base/internal/handler"
@ -20,8 +21,9 @@ var configFile = flag.String("f", "etc/base.yaml", "the config file")
func main() {
flag.Parse()
cfgContent := fsconfig.StartNacosConfig(*configFile, nil)
var c config.Config
conf.MustLoad(*configFile, &c)
conf.LoadConfigFromYamlBytes([]byte(cfgContent), &c)
server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) {
}))

View File

@ -9,6 +9,7 @@ import (
"fusenapi/server/canteen/internal/handler"
"fusenapi/server/canteen/internal/svc"
"fusenapi/utils/auth"
"fusenapi/utils/fsconfig"
"github.com/zeromicro/go-zero/core/conf"
"github.com/zeromicro/go-zero/rest"
@ -19,9 +20,9 @@ var configFile = flag.String("f", "etc/canteen.yaml", "the config file")
func main() {
flag.Parse()
cfgContent := fsconfig.StartNacosConfig(*configFile, nil)
var c config.Config
conf.MustLoad(*configFile, &c)
conf.LoadConfigFromYamlBytes([]byte(cfgContent), &c)
server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) {

View File

@ -9,7 +9,6 @@ import (
"fusenapi/utils/auth"
"net/http"
"github.com/zeromicro/go-zero/core/conf"
"github.com/zeromicro/go-zero/rest"
)
@ -19,7 +18,6 @@ func main() {
flag.Parse()
var c config2.Config
conf.MustLoad(*configFile, &c)
server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) {

View File

@ -9,6 +9,7 @@ import (
"fusenapi/server/home-user-auth/internal/handler"
"fusenapi/server/home-user-auth/internal/svc"
"fusenapi/utils/auth"
"fusenapi/utils/fsconfig"
"github.com/zeromicro/go-zero/core/conf"
"github.com/zeromicro/go-zero/rest"
@ -19,9 +20,9 @@ var configFile = flag.String("f", "etc/home-user-auth.yaml", "the config file")
func main() {
flag.Parse()
cfgContent := fsconfig.StartNacosConfig(*configFile, nil)
var c config.Config
conf.MustLoad(*configFile, &c)
conf.LoadConfigFromYamlBytes([]byte(cfgContent), &c)
server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) {

View File

@ -7,6 +7,7 @@ import (
"time"
"fusenapi/utils/auth"
"fusenapi/utils/fsconfig"
"fusenapi/server/info/internal/config"
"fusenapi/server/info/internal/handler"
@ -21,8 +22,10 @@ var configFile = flag.String("f", "etc/info.yaml", "the config file")
func main() {
flag.Parse()
cfgContent := fsconfig.StartNacosConfig(*configFile, nil)
var c config.Config
conf.MustLoad(*configFile, &c)
conf.LoadConfigFromYamlBytes([]byte(cfgContent), &c)
c.Timeout = int64(time.Second * 15)
server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) {
}))

View File

@ -9,6 +9,7 @@ import (
"fusenapi/server/map-library/internal/handler"
"fusenapi/server/map-library/internal/svc"
"fusenapi/utils/auth"
"fusenapi/utils/fsconfig"
"github.com/zeromicro/go-zero/core/conf"
"github.com/zeromicro/go-zero/rest"
@ -19,8 +20,9 @@ var configFile = flag.String("f", "etc/map-library.yaml", "the config file")
func main() {
flag.Parse()
cfgContent := fsconfig.StartNacosConfig(*configFile, nil)
var c config.Config
conf.MustLoad(*configFile, &c)
conf.LoadConfigFromYamlBytes([]byte(cfgContent), &c)
server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) {

View File

@ -6,6 +6,7 @@ import (
"net/http"
"fusenapi/utils/auth"
"fusenapi/utils/fsconfig"
"fusenapi/server/pay/internal/config"
"fusenapi/server/pay/internal/handler"
@ -20,8 +21,10 @@ var configFile = flag.String("f", "etc/pay.yaml", "the config file")
func main() {
flag.Parse()
cfgContent := fsconfig.StartNacosConfig(*configFile, nil)
var c config.Config
conf.MustLoad(*configFile, &c)
conf.LoadConfigFromYamlBytes([]byte(cfgContent), &c)
server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) {
}))
defer server.Stop()

View File

@ -9,6 +9,7 @@ import (
"fusenapi/server/product-model/internal/handler"
"fusenapi/server/product-model/internal/svc"
"fusenapi/utils/auth"
"fusenapi/utils/fsconfig"
"github.com/zeromicro/go-zero/core/conf"
"github.com/zeromicro/go-zero/rest"
@ -19,8 +20,9 @@ var configFile = flag.String("f", "etc/product-model.yaml", "the config file")
func main() {
flag.Parse()
cfgContent := fsconfig.StartNacosConfig(*configFile, nil)
var c config.Config
conf.MustLoad(*configFile, &c)
conf.LoadConfigFromYamlBytes([]byte(cfgContent), &c)
server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) {

View File

@ -6,6 +6,7 @@ import (
"net/http"
"fusenapi/utils/auth"
"fusenapi/utils/fsconfig"
"fusenapi/server/product-template-tag/internal/config"
"fusenapi/server/product-template-tag/internal/handler"
@ -20,8 +21,10 @@ var configFile = flag.String("f", "etc/product-template-tag.yaml", "the config f
func main() {
flag.Parse()
cfgContent := fsconfig.StartNacosConfig(*configFile, nil)
var c config.Config
conf.MustLoad(*configFile, &c)
conf.LoadConfigFromYamlBytes([]byte(cfgContent), &c)
server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) {
}))
defer server.Stop()

View File

@ -9,6 +9,7 @@ import (
"fusenapi/server/product-template/internal/handler"
"fusenapi/server/product-template/internal/svc"
"fusenapi/utils/auth"
"fusenapi/utils/fsconfig"
"github.com/zeromicro/go-zero/core/conf"
"github.com/zeromicro/go-zero/rest"
@ -19,8 +20,9 @@ var configFile = flag.String("f", "etc/product-template.yaml", "the config file"
func main() {
flag.Parse()
cfgContent := fsconfig.StartNacosConfig(*configFile, nil)
var c config.Config
conf.MustLoad(*configFile, &c)
conf.LoadConfigFromYamlBytes([]byte(cfgContent), &c)
server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) {

View File

@ -9,6 +9,7 @@ import (
"fusenapi/server/product/internal/handler"
"fusenapi/server/product/internal/svc"
"fusenapi/utils/auth"
"fusenapi/utils/fsconfig"
"github.com/zeromicro/go-zero/core/conf"
"github.com/zeromicro/go-zero/rest"
@ -19,8 +20,9 @@ var configFile = flag.String("f", "etc/product.yaml", "the config file")
func main() {
flag.Parse()
cfgContent := fsconfig.StartNacosConfig(*configFile, nil)
var c config.Config
conf.MustLoad(*configFile, &c)
conf.LoadConfigFromYamlBytes([]byte(cfgContent), &c)
server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) {

View File

@ -6,6 +6,7 @@ import (
"net/http"
"fusenapi/utils/auth"
"fusenapi/utils/fsconfig"
"fusenapi/server/resource/internal/config"
"fusenapi/server/resource/internal/handler"
@ -20,8 +21,10 @@ var configFile = flag.String("f", "etc/resource.yaml", "the config file")
func main() {
flag.Parse()
cfgContent := fsconfig.StartNacosConfig(*configFile, nil)
var c config.Config
conf.MustLoad(*configFile, &c)
conf.LoadConfigFromYamlBytes([]byte(cfgContent), &c)
server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) {
}))
defer server.Stop()

View File

@ -7,6 +7,7 @@ import (
"time"
"fusenapi/utils/auth"
"fusenapi/utils/fsconfig"
"fusenapi/server/shopping-cart/internal/config"
"fusenapi/server/shopping-cart/internal/handler"
@ -21,8 +22,10 @@ var configFile = flag.String("f", "etc/shopping-cart.yaml", "the config file")
func main() {
flag.Parse()
cfgContent := fsconfig.StartNacosConfig(*configFile, nil)
var c config.Config
conf.MustLoad(*configFile, &c)
conf.LoadConfigFromYamlBytes([]byte(cfgContent), &c)
c.Timeout = int64(time.Second * 15)
server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) {
}))

View File

@ -9,6 +9,7 @@ import (
"fusenapi/server/upload/internal/handler"
"fusenapi/server/upload/internal/svc"
"fusenapi/utils/auth"
"fusenapi/utils/fsconfig"
"github.com/zeromicro/go-zero/core/conf"
"github.com/zeromicro/go-zero/rest"
@ -19,8 +20,9 @@ var configFile = flag.String("f", "etc/upload.yaml", "the config file")
func main() {
flag.Parse()
cfgContent := fsconfig.StartNacosConfig(*configFile, nil)
var c config.Config
conf.MustLoad(*configFile, &c)
conf.LoadConfigFromYamlBytes([]byte(cfgContent), &c)
server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) {

View File

@ -9,6 +9,7 @@ import (
"fusenapi/server/webset/internal/handler"
"fusenapi/server/webset/internal/svc"
"fusenapi/utils/auth"
"fusenapi/utils/fsconfig"
"github.com/zeromicro/go-zero/core/conf"
"github.com/zeromicro/go-zero/rest"
@ -19,8 +20,9 @@ var configFile = flag.String("f", "etc/webset.yaml", "the config file")
func main() {
flag.Parse()
cfgContent := fsconfig.StartNacosConfig(*configFile, nil)
var c config.Config
conf.MustLoad(*configFile, &c)
conf.LoadConfigFromYamlBytes([]byte(cfgContent), &c)
server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) {

View File

@ -8,6 +8,7 @@ import (
"net/http"
"fusenapi/utils/auth"
"fusenapi/utils/fsconfig"
"fusenapi/server/websocket/internal/config"
"fusenapi/server/websocket/internal/handler"
@ -22,8 +23,10 @@ var configFile = flag.String("f", "etc/websocket.yaml", "the config file")
func main() {
flag.Parse()
cfgContent := fsconfig.StartNacosConfig(*configFile, nil)
var c config.Config
conf.MustLoad(*configFile, &c)
conf.LoadConfigFromYamlBytes([]byte(cfgContent), &c)
server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) {
}))
defer server.Stop()

View File

@ -74,7 +74,7 @@ func AutoGetEtcYaml() *string {
dirs = dirs[0 : len(dirs)-1]
// 列出所有 curPath 下的文件夹
files, err := ioutil.ReadDir(curPath)
files, err := os.ReadDir(curPath)
if err != nil {
log.Println(err)
continue

View File

@ -1,7 +1,10 @@
package autoconfig
import "testing"
import (
"log"
"testing"
)
func TestAutoConfig(t *testing.T) {
AutoGetEtcYaml()
log.Println(*AutoGetEtcYaml())
}

View File

@ -119,7 +119,7 @@ func Array2MapByKey[KEY comparable, VALUE any](arrSrc []VALUE, fieldName string)
}
fv := srcv.FieldByName(fieldName)
k := fv.Interface().(KEY)
result[k] = srcv.Interface().(VALUE)
result[k] = arr.Index(i).Interface().(VALUE)
}
return result
@ -166,7 +166,7 @@ func Array2MapByKeyTag[KEY comparable, VALUE any](arrSrc []VALUE, tag string) (r
fv = fv.Elem()
}
k := fv.Interface().(KEY)
result[k] = srcv.Interface().(VALUE)
result[k] = arr.Index(i).Interface().(VALUE)
}
return

View File

@ -1,53 +1,142 @@
package fsconfig
import (
"fmt"
"log"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/nacos-group/nacos-sdk-go/v2/clients"
"github.com/nacos-group/nacos-sdk-go/v2/common/constant"
"github.com/nacos-group/nacos-sdk-go/v2/vo"
"gopkg.in/yaml.v2"
)
type EnvConfig struct {
Host string `yaml:"host"`
Port uint64 `yaml:"port"`
UserName string `yaml:"username"`
Password string `yaml:"password"`
NamespaceId string `yaml:"namespace"`
DataId string `yaml:"dataid"`
Group string `yaml:"group"`
NacosServers []string `yaml:"nacos"`
UserName string `yaml:"username"`
Password string `yaml:"password"`
NamespaceId string `yaml:"namespace"`
DataId string `yaml:"dataid"`
Group string `yaml:"group"`
}
var OptPathDir = "/opt"
var optPathDirs = []string{"/opt", "./", "../", "../../"}
var nacosConfig *EnvConfig
func GetEnvCofing() *EnvConfig {
return nacosConfig
if nacosConfig != nil {
return nacosConfig
}
for _, optDir := range optPathDirs {
if optDir[len(optDir)-1] != '/' {
optDir = optDir + "/"
}
for _, yname := range []string{"env.yaml", "env.yml"} {
f, err := os.Open(optDir + yname)
if err != nil {
// log.Println(err)
continue
}
cfg := &EnvConfig{}
err = yaml.NewDecoder(f).Decode(&cfg)
if err != nil {
// log.Println(err)
continue
}
nacosConfig = cfg
return nacosConfig
}
}
panic("Can't find env.yaml or env.yml in the specified directories")
}
func init() {
if OptPathDir[len(OptPathDir)-1] != '/' {
OptPathDir = OptPathDir + "/"
}
for _, yname := range []string{"env.yaml", "env.yml"} {
f, err := os.Open(OptPathDir + "/" + yname)
if err != nil {
log.Println(err)
continue
}
cfg := &EnvConfig{}
err = yaml.NewDecoder(f).Decode(&cfg)
if err != nil {
log.Println(err)
continue
}
nacosConfig = cfg
return
}
panic(fmt.Sprintf("can't find %s(env.yaml|env.yml) ", OptPathDir))
}
func StartNacosConfig(configFile string, OnChange func(namespace, group, dataId, data string)) string {
env := GetEnvCofing()
// 创建serverConfig
// 支持多个;至少一个ServerConfig
var serverConfig []constant.ServerConfig
for _, s := range env.NacosServers {
sp := strings.Split(s, ":")
host := sp[0]
port, err := strconv.ParseUint(sp[1], 10, 64)
if err != nil {
panic(err)
}
serverConfig = append(serverConfig, constant.ServerConfig{
IpAddr: host,
Port: port,
})
}
// 创建clientConfig
clientConfig := constant.ClientConfig{
NamespaceId: env.NamespaceId, // 如果需要支持多namespace我们可以场景多个client,它们有不同的NamespaceId。当namespace是public时此处填空字符串。
TimeoutMs: 50000,
NotLoadCacheAtStart: true,
LogLevel: "debug",
LogDir: "/tmp/nacos/log",
CacheDir: "/tmp/nacos/cache",
Username: env.UserName,
Password: env.Password,
}
// 创建服务发现客户端的另一种方式 (推荐)
// namingClient, err := clients.NewNamingClient(
// vo.NacosClientParam{
// ClientConfig: &clientConfig,
// ServerConfigs: serverConfig,
// },
// )
// if err != nil {
// log.Fatalf("初始化nacos失败: %s", err.Error())
// }
// log.Println(namingClient)
// 创建 Nacos 配置客户端
configClient, err := clients.CreateConfigClient(map[string]interface{}{
"clientConfig": clientConfig,
"serverConfigs": serverConfig,
})
if err != nil {
log.Fatalf("Failed to create Nacos config client: %v", err)
}
cfgYamls := strings.Split(configFile, "/")
cfgYaml := cfgYamls[len(cfgYamls)-1]
yamlExt := filepath.Ext(cfgYaml)
if !(yamlExt == ".yaml" || yamlExt == ".yml") {
log.Panic(configFile)
}
// 获取配置
content, err := configClient.GetConfig(vo.ConfigParam{
DataId: cfgYaml,
Group: env.Group,
OnChange: OnChange,
})
if err != nil {
log.Fatalf("Failed to get config from Nacos: %v", err)
}
return content
// log.Println(content)
}

View File

@ -2,69 +2,9 @@ package fsconfig_test
import (
"fusenapi/utils/fsconfig"
"log"
"testing"
"github.com/nacos-group/nacos-sdk-go/v2/clients"
"github.com/nacos-group/nacos-sdk-go/v2/common/constant"
"github.com/nacos-group/nacos-sdk-go/v2/vo"
)
func TestCase1(t *testing.T) {
env := fsconfig.GetEnvCofing()
// 创建serverConfig
// 支持多个;至少一个ServerConfig
serverConfig := []constant.ServerConfig{
{
IpAddr: env.Host,
Port: uint64(env.Port),
},
}
// 创建clientConfig
clientConfig := constant.ClientConfig{
NamespaceId: env.NamespaceId, // 如果需要支持多namespace我们可以场景多个client,它们有不同的NamespaceId。当namespace是public时此处填空字符串。
TimeoutMs: 50000,
NotLoadCacheAtStart: true,
LogLevel: "debug",
LogDir: "/tmp/nacos",
CacheDir: "/tmp/nacos",
Username: env.UserName,
Password: env.Password,
}
// 创建服务发现客户端的另一种方式 (推荐)
// namingClient, err := clients.NewNamingClient(
// vo.NacosClientParam{
// ClientConfig: &clientConfig,
// ServerConfigs: serverConfig,
// },
// )
// if err != nil {
// log.Fatalf("初始化nacos失败: %s", err.Error())
// }
// log.Println(namingClient)
// 创建 Nacos 配置客户端
configClient, err := clients.CreateConfigClient(map[string]interface{}{
"clientConfig": clientConfig,
"serverConfigs": serverConfig,
})
if err != nil {
log.Fatalf("Failed to create Nacos config client: %v", err)
}
// 获取配置
content, err := configClient.GetConfig(vo.ConfigParam{
DataId: "auth.yaml",
Group: env.Group,
})
if err != nil {
log.Fatalf("Failed to get config from Nacos: %v", err)
}
log.Println(content)
fsconfig.StartNacosConfig("auth.yaml", nil)
}