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
Show 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 (
"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
,
b
c
.
Data
)
app
,
cleanup
,
err
:=
wireApp
(
c
.
Server
,
c
.
Data
)
if
err
!=
nil
{
panic
(
err
)
}
...
...
configs/config.yaml
View file @
d046383d
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
go.mod
View file @
d046383d
...
...
@@ -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
)
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 (
"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
)
...
...
internal/server/http.go
View file @
d046383d
...
...
@@ -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
)
...
...
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