Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
K
kratos-layout
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
lizifeng
kratos-layout
Commits
d046383d
Commit
d046383d
authored
Jun 08, 2022
by
kzkzzzz
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
调整api proto
parent
15c16fe3
Changes
9
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
438 additions
and
731 deletions
+438
-731
main.go
cmd/server/main.go
+4
-19
config.yaml
configs/config.yaml
+10
-7
go.mod
go.mod
+15
-2
go.sum
go.sum
+362
-0
conf.go
internal/conf/conf.go
+41
-0
conf.pb.go
internal/conf/conf.pb.go
+0
-652
conf.proto
internal/conf/conf.proto
+0
-41
grpc.go
internal/server/grpc.go
+3
-5
http.go
internal/server/http.go
+3
-5
No files found.
cmd/server/main.go
View file @
d046383d
...
@@ -4,8 +4,6 @@ import (
...
@@ -4,8 +4,6 @@ import (
"flag"
"flag"
"github.com/go-kratos/kratos-layout/internal/conf"
"github.com/go-kratos/kratos-layout/internal/conf"
"github.com/go-kratos/kratos/v2"
"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/log"
"github.com/go-kratos/kratos/v2/middleware/tracing"
"github.com/go-kratos/kratos/v2/middleware/tracing"
"github.com/go-kratos/kratos/v2/transport/grpc"
"github.com/go-kratos/kratos/v2/transport/grpc"
...
@@ -26,7 +24,7 @@ var (
...
@@ -26,7 +24,7 @@ var (
)
)
func
init
()
{
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
{
func
newApp
(
hs
*
http
.
Server
,
gs
*
grpc
.
Server
)
*
kratos
.
App
{
...
@@ -55,23 +53,10 @@ func main() {
...
@@ -55,23 +53,10 @@ func main() {
)
)
log
.
SetLogger
(
logger
)
log
.
SetLogger
(
logger
)
c
:=
config
.
New
(
var
c
conf
.
Config
config
.
WithSource
(
conf
.
LoadFromYaml
(
flagconf
,
&
c
)
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
)
}
app
,
cleanup
,
err
:=
wireApp
(
bc
.
Server
,
b
c
.
Data
)
app
,
cleanup
,
err
:=
wireApp
(
c
.
Server
,
c
.
Data
)
if
err
!=
nil
{
if
err
!=
nil
{
panic
(
err
)
panic
(
err
)
}
}
...
...
configs/config.yaml
View file @
d046383d
server
:
server
:
http
:
http
:
addr
:
0.0.0.0:8000
addr
:
0.0.0.0:8000
timeout
:
1s
timeout
:
2
# 单位秒
grpc
:
grpc
:
addr
:
0.0.0.0:9000
addr
:
0.0.0.0:9000
timeout
:
1s
timeout
:
2
data
:
data
:
database
:
mysql
:
driver
:
mysql
dsn
:
remote:admin666@tcp(127.0.0.1:3306)/test?loc=Local&charset=utf8mb4&writeTimeout=3s&readTimeout=3s&timeout=1s&parseTime=true
source
:
root:root@tcp(127.0.0.1:3306)/test
maxConn
:
8
maxIdleConn
:
2
maxLifetime
:
1800
# 连接有效时间,单位秒
debug
:
true
redis
:
redis
:
addr
:
127.0.0.1:6379
addr
:
127.0.0.1:6379
read_timeout
:
0.2s
password
:
"
"
write_timeout
:
0.2s
db
:
15
go.mod
View file @
d046383d
...
@@ -19,14 +19,27 @@ require (
...
@@ -19,14 +19,27 @@ require (
github.com/google/subcommands v1.0.1 // indirect
github.com/google/subcommands v1.0.1 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/mux v1.8.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/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/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 v1.7.0 // indirect
go.opentelemetry.io/otel/trace 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/net v0.0.0-20220520000938-2e3eb7b945c2 // indirect
golang.org/x/sync v0.0.0-20220513210516-0976fa681c29 // indirect
golang.org/x/sync v0.0.0-20220513210516-0976fa681c29 // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135 // indirect
golang.org/x/tools v0.1.0 // indirect
gopkg.in/yaml.v3 v3.0.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
)
)
go.sum
View file @
d046383d
This diff is collapsed.
Click to expand it.
internal/conf/conf.go
0 → 100644
View file @
d046383d
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
}
internal/conf/conf.pb.go
deleted
100644 → 0
View file @
15c16fe3
This diff is collapsed.
Click to expand it.
internal/conf/conf.proto
deleted
100644 → 0
View file @
15c16fe3
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
;
}
internal/server/grpc.go
View file @
d046383d
...
@@ -6,6 +6,7 @@ import (
...
@@ -6,6 +6,7 @@ import (
"github.com/go-kratos/kratos-layout/internal/service"
"github.com/go-kratos/kratos-layout/internal/service"
"github.com/go-kratos/kratos/v2/middleware/recovery"
"github.com/go-kratos/kratos/v2/middleware/recovery"
"github.com/go-kratos/kratos/v2/transport/grpc"
"github.com/go-kratos/kratos/v2/transport/grpc"
"time"
)
)
// NewGRPCServer new a gRPC server.
// NewGRPCServer new a gRPC server.
...
@@ -15,14 +16,11 @@ func NewGRPCServer(c *conf.Server, greeter *service.GreeterService) *grpc.Server
...
@@ -15,14 +16,11 @@ func NewGRPCServer(c *conf.Server, greeter *service.GreeterService) *grpc.Server
recovery
.
Recovery
(),
recovery
.
Recovery
(),
),
),
}
}
if
c
.
Grpc
.
Network
!=
""
{
opts
=
append
(
opts
,
grpc
.
Network
(
c
.
Grpc
.
Network
))
}
if
c
.
Grpc
.
Addr
!=
""
{
if
c
.
Grpc
.
Addr
!=
""
{
opts
=
append
(
opts
,
grpc
.
Address
(
c
.
Grpc
.
Addr
))
opts
=
append
(
opts
,
grpc
.
Address
(
c
.
Grpc
.
Addr
))
}
}
if
c
.
Grpc
.
Timeout
!=
nil
{
if
c
.
Http
.
Timeout
>
0
{
opts
=
append
(
opts
,
grpc
.
Timeout
(
c
.
Grpc
.
Timeout
.
AsDuration
()
))
opts
=
append
(
opts
,
grpc
.
Timeout
(
time
.
Duration
(
c
.
Http
.
Timeout
)
*
time
.
Second
))
}
}
srv
:=
grpc
.
NewServer
(
opts
...
)
srv
:=
grpc
.
NewServer
(
opts
...
)
v1
.
RegisterGreeterServer
(
srv
,
greeter
)
v1
.
RegisterGreeterServer
(
srv
,
greeter
)
...
...
internal/server/http.go
View file @
d046383d
...
@@ -6,6 +6,7 @@ import (
...
@@ -6,6 +6,7 @@ import (
"github.com/go-kratos/kratos-layout/internal/service"
"github.com/go-kratos/kratos-layout/internal/service"
"github.com/go-kratos/kratos/v2/middleware/recovery"
"github.com/go-kratos/kratos/v2/middleware/recovery"
"github.com/go-kratos/kratos/v2/transport/http"
"github.com/go-kratos/kratos/v2/transport/http"
"time"
)
)
// NewHTTPServer new a HTTP server.
// NewHTTPServer new a HTTP server.
...
@@ -15,14 +16,11 @@ func NewHTTPServer(c *conf.Server, greeter *service.GreeterService) *http.Server
...
@@ -15,14 +16,11 @@ func NewHTTPServer(c *conf.Server, greeter *service.GreeterService) *http.Server
recovery
.
Recovery
(),
recovery
.
Recovery
(),
),
),
}
}
if
c
.
Http
.
Network
!=
""
{
opts
=
append
(
opts
,
http
.
Network
(
c
.
Http
.
Network
))
}
if
c
.
Http
.
Addr
!=
""
{
if
c
.
Http
.
Addr
!=
""
{
opts
=
append
(
opts
,
http
.
Address
(
c
.
Http
.
Addr
))
opts
=
append
(
opts
,
http
.
Address
(
c
.
Http
.
Addr
))
}
}
if
c
.
Http
.
Timeout
!=
nil
{
if
c
.
Http
.
Timeout
>
0
{
opts
=
append
(
opts
,
http
.
Timeout
(
c
.
Http
.
Timeout
.
AsDuration
()
))
opts
=
append
(
opts
,
http
.
Timeout
(
time
.
Duration
(
c
.
Http
.
Timeout
)
*
time
.
Second
))
}
}
srv
:=
http
.
NewServer
(
opts
...
)
srv
:=
http
.
NewServer
(
opts
...
)
v1
.
RegisterGreeterHTTPServer
(
srv
,
greeter
)
v1
.
RegisterGreeterHTTPServer
(
srv
,
greeter
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment