init project
This commit is contained in:
100
providers/config/loader.go
Normal file
100
providers/config/loader.go
Normal file
@@ -0,0 +1,100 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"app/container"
|
||||
"app/utils"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-micro/plugins/v4/config/encoder/toml"
|
||||
"github.com/go-micro/plugins/v4/config/source/etcd"
|
||||
"github.com/rogeecn/fabfile"
|
||||
"go-micro.dev/v4/config"
|
||||
"go-micro.dev/v4/config/reader"
|
||||
"go-micro.dev/v4/config/reader/json"
|
||||
"go-micro.dev/v4/config/source"
|
||||
"go-micro.dev/v4/config/source/env"
|
||||
"go-micro.dev/v4/config/source/file"
|
||||
"go-micro.dev/v4/logger"
|
||||
)
|
||||
|
||||
var c *Config
|
||||
|
||||
type Config struct {
|
||||
App App
|
||||
Http Http
|
||||
Log Log
|
||||
}
|
||||
|
||||
func init() {
|
||||
if err := container.Container.Provide(Load); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func Load() (*Config, error) {
|
||||
var err error
|
||||
confFile := utils.ShareConfigFile
|
||||
if confFile == "" {
|
||||
confFile, err = fabfile.Find("config.toml")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
options := []config.Option{}
|
||||
|
||||
options = append(options, config.WithSource(file.NewSource(
|
||||
file.WithPath(confFile),
|
||||
source.WithEncoder(toml.NewEncoder()),
|
||||
)))
|
||||
|
||||
etcdEndpoints := etcdEndpoints()
|
||||
if len(etcdEndpoints) > 0 {
|
||||
logger.Info("etcd endpoints: ", etcdEndpoints, len(etcdEndpoints))
|
||||
options = append(options, config.WithSource(etcd.NewSource(
|
||||
etcd.WithAddress(etcdEndpoints...),
|
||||
etcd.WithPrefix("/micro/config/api.web"),
|
||||
etcd.StripPrefix(true),
|
||||
)))
|
||||
}
|
||||
|
||||
options = append(options, config.WithSource(env.NewSource()))
|
||||
|
||||
options = append(options, config.WithReader(json.NewReader(reader.WithEncoder(toml.NewEncoder()))))
|
||||
|
||||
conf, err := config.NewConfig(options...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := conf.Scan(&c); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
go func() {
|
||||
ticker := time.NewTicker(time.Second * 10)
|
||||
defer ticker.Stop()
|
||||
|
||||
for range ticker.C {
|
||||
if err := conf.Scan(&c); err != nil {
|
||||
logger.Fatal(err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
return c, nil
|
||||
}
|
||||
|
||||
func etcdEndpoints() []string {
|
||||
var endpoints []string
|
||||
envVars := strings.Split(os.Getenv("ETCD_ENDPOINTS"), ",")
|
||||
for _, env := range envVars {
|
||||
if strings.TrimSpace(env) == "" {
|
||||
continue
|
||||
}
|
||||
endpoints = append(endpoints, strings.TrimSpace(env))
|
||||
}
|
||||
return endpoints
|
||||
}
|
||||
18
providers/config/section_app.go
Normal file
18
providers/config/section_app.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package config
|
||||
|
||||
import "app/utils"
|
||||
|
||||
type App struct {
|
||||
Mode string
|
||||
}
|
||||
|
||||
func (a App) IsDebug() bool {
|
||||
return a.Mode == "debug"
|
||||
}
|
||||
|
||||
func (a App) IsRelease() bool {
|
||||
return a.Mode == "release"
|
||||
}
|
||||
func (a App) IsTesting() bool {
|
||||
return a.Mode == "testing" || utils.IsInTesting()
|
||||
}
|
||||
20
providers/config/section_http.go
Normal file
20
providers/config/section_http.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package config
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Http struct {
|
||||
Static string
|
||||
Host string
|
||||
Port uint
|
||||
Https bool
|
||||
HttpsCert string
|
||||
HttpKey string
|
||||
}
|
||||
|
||||
func (h Http) Address() string {
|
||||
return fmt.Sprintf("%s:%d", h.Host, h.Port)
|
||||
}
|
||||
|
||||
func (h Http) PortString() string {
|
||||
return fmt.Sprintf(":%d", h.Port)
|
||||
}
|
||||
5
providers/config/section_log.go
Normal file
5
providers/config/section_log.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package config
|
||||
|
||||
type Log struct {
|
||||
Level string
|
||||
}
|
||||
63
providers/http/engine.go
Normal file
63
providers/http/engine.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"app/container"
|
||||
"app/providers/config"
|
||||
"app/providers/logger"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func init() {
|
||||
if err := container.Container.Provide(NewService); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
Engine *gin.Engine
|
||||
conf *config.Config
|
||||
}
|
||||
|
||||
func (e *Service) Serve() error {
|
||||
if e.conf.Http.Https {
|
||||
return e.Engine.RunTLS(e.conf.Http.PortString(), e.conf.Http.HttpsCert, e.conf.Http.HttpKey)
|
||||
}
|
||||
return e.Engine.Run(e.conf.Http.PortString())
|
||||
}
|
||||
|
||||
func NewService(log *logger.Logger, cfg *config.Config) *Service {
|
||||
log.Info("init http service with gin...")
|
||||
|
||||
gin.DefaultWriter = &logger.LevelWriter{Logger: log, Level: "info"}
|
||||
gin.DefaultErrorWriter = &logger.LevelWriter{Logger: log, Level: "error"}
|
||||
|
||||
if cfg.App.IsDebug() {
|
||||
gin.SetMode(gin.DebugMode)
|
||||
} else {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
}
|
||||
|
||||
engine := gin.New()
|
||||
|
||||
engine.Use(gin.LoggerWithFormatter(func(param gin.LogFormatterParams) string {
|
||||
return fmt.Sprintf("%s - [%s] \"%s %s %s %d %s \"%s\" %s\"\n",
|
||||
param.ClientIP,
|
||||
param.TimeStamp.Format(time.RFC1123),
|
||||
param.Method,
|
||||
param.Path,
|
||||
param.Request.Proto,
|
||||
param.StatusCode,
|
||||
param.Latency,
|
||||
param.Request.UserAgent(),
|
||||
param.ErrorMessage,
|
||||
)
|
||||
}))
|
||||
|
||||
engine.Use(gin.Recovery())
|
||||
|
||||
return &Service{Engine: engine, conf: cfg}
|
||||
}
|
||||
101
providers/logger/logger.go
Normal file
101
providers/logger/logger.go
Normal file
@@ -0,0 +1,101 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"app/container"
|
||||
"app/providers/config"
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
"github.com/go-micro/plugins/v4/logger/zap"
|
||||
"go-micro.dev/v4/logger"
|
||||
)
|
||||
|
||||
type Logger struct {
|
||||
}
|
||||
|
||||
type LevelWriter struct {
|
||||
Logger logger.Logger
|
||||
Level string
|
||||
}
|
||||
|
||||
func (w LevelWriter) Write(p []byte) (n int, err error) {
|
||||
level, _ := logger.GetLevel(w.Level)
|
||||
w.Logger.Logf(level, strings.TrimSpace(string(p)))
|
||||
return len(p), nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
if err := container.Container.Provide(NewLogger); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func NewLogger(conf *config.Config) (*Logger, error) {
|
||||
zapLogger, err := zap.NewLogger()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
logger.DefaultLogger = zapLogger
|
||||
|
||||
return &Logger{}, nil
|
||||
}
|
||||
|
||||
func (l *Logger) Init(options ...logger.Option) error { panic("do not use") }
|
||||
func (l *Logger) Options() logger.Options { panic("do not use") }
|
||||
func (l *Logger) Fields(fields map[string]interface{}) logger.Logger { panic("do not use") }
|
||||
func (l *Logger) String() string { panic("do not use") }
|
||||
|
||||
func (l *Logger) Logf(level logger.Level, format string, v ...interface{}) {
|
||||
logger.Logf(level, format, v...)
|
||||
}
|
||||
func (l *Logger) Log(level logger.Level, v ...interface{}) {
|
||||
logger.Log(level, v...)
|
||||
}
|
||||
|
||||
func (l *Logger) Info(args ...interface{}) {
|
||||
logger.Log(logger.InfoLevel, args...)
|
||||
}
|
||||
|
||||
func (l *Logger) Infof(template string, args ...interface{}) {
|
||||
logger.Logf(logger.InfoLevel, template, args...)
|
||||
}
|
||||
|
||||
func (l *Logger) Trace(args ...interface{}) {
|
||||
logger.Log(logger.TraceLevel, args...)
|
||||
}
|
||||
|
||||
func (l *Logger) Tracef(template string, args ...interface{}) {
|
||||
logger.Logf(logger.TraceLevel, template, args...)
|
||||
}
|
||||
|
||||
func (l *Logger) Debug(args ...interface{}) {
|
||||
logger.Log(logger.DebugLevel, args...)
|
||||
}
|
||||
|
||||
func (l *Logger) Debugf(template string, args ...interface{}) {
|
||||
logger.Logf(logger.DebugLevel, template, args...)
|
||||
}
|
||||
|
||||
func (l *Logger) Warn(args ...interface{}) {
|
||||
logger.Log(logger.WarnLevel, args...)
|
||||
}
|
||||
|
||||
func (l *Logger) Warnf(template string, args ...interface{}) {
|
||||
logger.Logf(logger.WarnLevel, template, args...)
|
||||
}
|
||||
|
||||
func (l *Logger) Error(args ...interface{}) {
|
||||
logger.Log(logger.ErrorLevel, args...)
|
||||
}
|
||||
|
||||
func (l *Logger) Errorf(template string, args ...interface{}) {
|
||||
logger.Logf(logger.ErrorLevel, template, args...)
|
||||
}
|
||||
|
||||
func (l *Logger) Fatal(args ...interface{}) {
|
||||
logger.Log(logger.FatalLevel, args...)
|
||||
}
|
||||
|
||||
func (l *Logger) Fatalf(template string, args ...interface{}) {
|
||||
logger.Logf(logger.FatalLevel, template, args...)
|
||||
}
|
||||
29
providers/micro/micro.go
Normal file
29
providers/micro/micro.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package micro
|
||||
|
||||
import (
|
||||
"app/container"
|
||||
"app/providers/config"
|
||||
"app/utils"
|
||||
"log"
|
||||
|
||||
mgrpc "github.com/go-micro/plugins/v4/client/grpc"
|
||||
mhttp "github.com/go-micro/plugins/v4/server/http"
|
||||
"go-micro.dev/v4"
|
||||
)
|
||||
|
||||
func init() {
|
||||
if err := container.Container.Provide(NewService); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func NewService(conf *config.Config) micro.Service {
|
||||
service := micro.NewService(
|
||||
micro.Server(mhttp.NewServer()),
|
||||
micro.Client(mgrpc.NewClient()),
|
||||
micro.Address(conf.Http.Address()),
|
||||
)
|
||||
service.Init(micro.Name(utils.Service), micro.Version(utils.Version))
|
||||
|
||||
return service
|
||||
}
|
||||
15
providers/mysql/mysql.go
Normal file
15
providers/mysql/mysql.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package mysql
|
||||
|
||||
import (
|
||||
"app/container"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func init() {
|
||||
container.Container.Provide(NewMysqlConnection)
|
||||
}
|
||||
|
||||
func NewMysqlConnection() (*gorm.DB, error) {
|
||||
return nil, nil
|
||||
}
|
||||
9
providers/provider.go
Normal file
9
providers/provider.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package providers
|
||||
|
||||
import (
|
||||
_ "app/providers/config"
|
||||
_ "app/providers/http"
|
||||
_ "app/providers/logger"
|
||||
_ "app/providers/micro"
|
||||
_ "app/providers/mysql"
|
||||
)
|
||||
20
providers/rpc/assets.go
Normal file
20
providers/rpc/assets.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package rpc
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"app/container"
|
||||
"app/proto"
|
||||
|
||||
"go-micro.dev/v4"
|
||||
)
|
||||
|
||||
func init() {
|
||||
err := container.Container.Provide(func(svc micro.Service) proto.WebApiService {
|
||||
return proto.NewWebApiService("web.api", svc.Client())
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user