Commit d046383d authored by kzkzzzz's avatar kzkzzzz

调整api proto

parent 15c16fe3
......@@ -4,8 +4,6 @@ import (
"flag"
"github.com/go-kratos/kratos-layout/internal/conf"
"github.com/go-kratos/kratos/v2"
"github.com/go-kratos/kratos/v2/config"
"github.com/go-kratos/kratos/v2/config/file"
"github.com/go-kratos/kratos/v2/log"
"github.com/go-kratos/kratos/v2/middleware/tracing"
"github.com/go-kratos/kratos/v2/transport/grpc"
......@@ -26,7 +24,7 @@ var (
)
func init() {
flag.StringVar(&flagconf, "conf", "", "config path, eg: -conf config.yaml")
flag.StringVar(&flagconf, "conf", "", "config file, eg: -conf config.yaml")
}
func newApp(hs *http.Server, gs *grpc.Server) *kratos.App {
......@@ -55,23 +53,10 @@ func main() {
)
log.SetLogger(logger)
c := config.New(
config.WithSource(
file.NewSource(flagconf),
),
)
defer c.Close()
if err := c.Load(); err != nil {
panic(err)
}
var bc conf.Bootstrap
if err := c.Scan(&bc); err != nil {
panic(err)
}
var c conf.Config
conf.LoadFromYaml(flagconf, &c)
app, cleanup, err := wireApp(bc.Server, bc.Data)
app, cleanup, err := wireApp(c.Server, c.Data)
if err != nil {
panic(err)
}
......
server:
http:
addr: 0.0.0.0:8000
timeout: 1s
timeout: 2 # 单位秒
grpc:
addr: 0.0.0.0:9000
timeout: 1s
timeout: 2
data:
database:
driver: mysql
source: root:root@tcp(127.0.0.1:3306)/test
mysql:
dsn: remote:admin666@tcp(127.0.0.1:3306)/test?loc=Local&charset=utf8mb4&writeTimeout=3s&readTimeout=3s&timeout=1s&parseTime=true
maxConn: 8
maxIdleConn: 2
maxLifetime: 1800 # 连接有效时间,单位秒
debug: true
redis:
addr: 127.0.0.1:6379
read_timeout: 0.2s
write_timeout: 0.2s
password: ""
db: 15
......@@ -19,14 +19,27 @@ require (
github.com/google/subcommands v1.0.1 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/afero v1.8.2 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.12.0 // indirect
github.com/subosito/gotenv v1.4.0 // indirect
go.opentelemetry.io/otel v1.7.0 // indirect
go.opentelemetry.io/otel/trace v1.7.0 // indirect
golang.org/x/net v0.0.0-20220520000938-2e3eb7b945c2 // indirect
golang.org/x/sync v0.0.0-20220513210516-0976fa681c29 // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135 // indirect
gopkg.in/yaml.v3 v3.0.0 // indirect
golang.org/x/tools v0.1.0 // indirect
gopkg.in/ini.v1 v1.66.6 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
This diff is collapsed.
package conf
import (
"fmt"
"github.com/spf13/viper"
)
// Config 配置
type Config struct {
Server *Server
Data *Data
}
type Server struct {
Http struct {
Addr string
Timeout int
}
Grpc struct {
Addr string
Timeout int
}
}
type Data struct {
}
func LoadFromYaml(configFile string, conf interface{}) {
v := viper.New()
v.SetConfigFile(configFile)
err := v.ReadInConfig()
if err != nil {
panic(fmt.Errorf("读取配置文件失败: %s", err))
}
err = v.Unmarshal(&conf)
if err != nil {
panic(fmt.Errorf("解析配置文件失败: %s", err))
}
return
}
This diff is collapsed.
syntax = "proto3";
package kratos.api;
option go_package = "github.com/go-kratos/kratos-layout/internal/conf;conf";
import "google/protobuf/duration.proto";
message Bootstrap {
Server server = 1;
Data data = 2;
}
message Server {
message HTTP {
string network = 1;
string addr = 2;
google.protobuf.Duration timeout = 3;
}
message GRPC {
string network = 1;
string addr = 2;
google.protobuf.Duration timeout = 3;
}
HTTP http = 1;
GRPC grpc = 2;
}
message Data {
message Database {
string driver = 1;
string source = 2;
}
message Redis {
string network = 1;
string addr = 2;
google.protobuf.Duration read_timeout = 3;
google.protobuf.Duration write_timeout = 4;
}
Database database = 1;
Redis redis = 2;
}
......@@ -6,6 +6,7 @@ import (
"github.com/go-kratos/kratos-layout/internal/service"
"github.com/go-kratos/kratos/v2/middleware/recovery"
"github.com/go-kratos/kratos/v2/transport/grpc"
"time"
)
// NewGRPCServer new a gRPC server.
......@@ -15,14 +16,11 @@ func NewGRPCServer(c *conf.Server, greeter *service.GreeterService) *grpc.Server
recovery.Recovery(),
),
}
if c.Grpc.Network != "" {
opts = append(opts, grpc.Network(c.Grpc.Network))
}
if c.Grpc.Addr != "" {
opts = append(opts, grpc.Address(c.Grpc.Addr))
}
if c.Grpc.Timeout != nil {
opts = append(opts, grpc.Timeout(c.Grpc.Timeout.AsDuration()))
if c.Http.Timeout > 0 {
opts = append(opts, grpc.Timeout(time.Duration(c.Http.Timeout)*time.Second))
}
srv := grpc.NewServer(opts...)
v1.RegisterGreeterServer(srv, greeter)
......
......@@ -6,6 +6,7 @@ import (
"github.com/go-kratos/kratos-layout/internal/service"
"github.com/go-kratos/kratos/v2/middleware/recovery"
"github.com/go-kratos/kratos/v2/transport/http"
"time"
)
// NewHTTPServer new a HTTP server.
......@@ -15,14 +16,11 @@ func NewHTTPServer(c *conf.Server, greeter *service.GreeterService) *http.Server
recovery.Recovery(),
),
}
if c.Http.Network != "" {
opts = append(opts, http.Network(c.Http.Network))
}
if c.Http.Addr != "" {
opts = append(opts, http.Address(c.Http.Addr))
}
if c.Http.Timeout != nil {
opts = append(opts, http.Timeout(c.Http.Timeout.AsDuration()))
if c.Http.Timeout > 0 {
opts = append(opts, http.Timeout(time.Duration(c.Http.Timeout)*time.Second))
}
srv := http.NewServer(opts...)
v1.RegisterGreeterHTTPServer(srv, greeter)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment