Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
demo-go
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
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
彭芳
demo-go
Commits
e4755882
Commit
e4755882
authored
Feb 21, 2023
by
彭芳
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[refactor]: add database conf
parent
0cac1efc
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
37 additions
and
20 deletions
+37
-20
main.go
cmd/demo/main.go
+2
-3
config.yaml
configs/config.yaml
+4
-0
go.mod
go.mod
+0
-4
go.sum
go.sum
+0
-5
conf.pb.go
internal/conf/conf.pb.go
+0
-0
conf.proto
internal/conf/conf.proto
+3
-6
data.go
internal/data/data.go
+26
-2
http.go
internal/server/http.go
+2
-0
No files found.
cmd/demo/main.go
View file @
e4755882
...
...
@@ -10,7 +10,6 @@ import (
"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/http"
_
"go.uber.org/automaxprocs"
...
...
@@ -53,8 +52,8 @@ func main() {
"service.id"
,
id
,
"service.name"
,
Name
,
"service.version"
,
Version
,
"trace.id"
,
tracing
.
TraceID
(),
"span.id"
,
tracing
.
SpanID
(),
//
"trace.id", tracing.TraceID(),
//
"span.id", tracing.SpanID(),
)
c
:=
config
.
New
(
config
.
WithSource
(
...
...
configs/config.yaml
View file @
e4755882
...
...
@@ -6,3 +6,6 @@ data:
database
:
driver
:
mysql
source
:
adstest:Ey5M6GowXu!7@tcp(ads-mysql-test.adsdesk.cn:13316)/adsdesk_test2?charset=utf8&parseTime=True&loc=Local
max_idle
:
50
max_open
:
100
max_lifetime
:
30s
\ No newline at end of file
go.mod
View file @
e4755882
...
...
@@ -23,8 +23,6 @@ require (
require (
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-playground/form/v4 v4.2.0 // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
...
...
@@ -45,8 +43,6 @@ require (
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/ugorji/go/codec v1.2.7 // indirect
go.opentelemetry.io/otel v1.7.0 // indirect
go.opentelemetry.io/otel/trace v1.7.0 // indirect
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 // indirect
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
golang.org/x/net v0.4.0 // indirect
...
...
go.sum
View file @
e4755882
...
...
@@ -36,9 +36,7 @@ github.com/go-kratos/aegis v0.1.2/go.mod h1:jYeSQ3Gesba478zEnujOiG5QdsyF3Xk/8owF
github.com/go-kratos/kratos/v2 v2.4.1 h1:NFQy8Ha4Xu6T3Q40JlKzspvlMa5IGvIHhJw5+sqyV4c=
github.com/go-kratos/kratos/v2 v2.4.1/go.mod h1:5acyLj4EgY428AJnZl2EwCrMV1OVlttQFBum+SghMiA=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0=
github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/go-ole/go-ole v1.2.5/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A=
...
...
@@ -154,11 +152,8 @@ github.com/ugorji/go v1.2.7/go.mod h1:nF9osbDWLy6bDVv/Rtoh6QgnvNDpmCalQV5urGCCS6
github.com/ugorji/go/codec v1.2.7 h1:YPXUKf7fYbp/y8xloBqZOw2qaVggbfwMlI8WM3wZUJ0=
github.com/ugorji/go/codec v1.2.7/go.mod h1:WGN1fab3R1fzQlVQTkfxVtIBhWDRqOviHU95kRgeqEY=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.opentelemetry.io/otel v1.7.0 h1:Z2lA3Tdch0iDcrhJXDIlC94XE+bxok1F9B+4Lz/lGsM=
go.opentelemetry.io/otel v1.7.0/go.mod h1:5BdUoMIz5WEs0vt0CUEMtSSaTSHBBVwrhnz7+nrD5xk=
go.opentelemetry.io/otel/sdk v1.7.0 h1:4OmStpcKVOfvDOgCt7UriAPtKolwIhxpnSNI/yK+1B0=
go.opentelemetry.io/otel/sdk v1.7.0/go.mod h1:uTEOTwaqIVuTGiJN7ii13Ibp75wJmYUDe374q6cZwUU=
go.opentelemetry.io/otel/trace v1.7.0 h1:O37Iogk1lEkMRXewVtZ1BBTVn5JEp8GrJvP92bJqC6o=
go.opentelemetry.io/otel/trace v1.7.0/go.mod h1:fzLSB9nqR2eXzxPXb2JW9IKE+ScyXA48yyE4TNvoHqU=
go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI=
go.uber.org/automaxprocs v1.5.1 h1:e1YG66Lrk73dn4qhg8WFSvhF0JuFQF0ERIp4rpuV8Qk=
...
...
internal/conf/conf.pb.go
View file @
e4755882
This diff is collapsed.
Click to expand it.
internal/conf/conf.proto
View file @
e4755882
...
...
@@ -16,19 +16,16 @@ message Server {
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
;
int64
max_idle
=
3
;
int64
max_open
=
4
;
google.protobuf.Duration
max_lifetime
=
5
;
}
message
Redis
{
string
network
=
1
;
...
...
internal/data/data.go
View file @
e4755882
...
...
@@ -27,6 +27,14 @@ func NewData(c *conf.Data, logger log.Logger) (*Data, func(), error) {
cleanup
:=
func
()
{
log
.
NewHelper
(
logger
)
.
Info
(
"closing the data resources"
)
}
db
,
err
:=
newDb
(
c
)
if
err
!=
nil
{
return
nil
,
cleanup
,
err
}
return
&
Data
{
db
:
db
},
cleanup
,
nil
}
func
newDb
(
c
*
conf
.
Data
)
(
*
gorm
.
DB
,
error
)
{
db
,
err
:=
gorm
.
Open
(
mysql
.
New
(
mysql
.
Config
{
DriverName
:
c
.
Database
.
Driver
,
DSN
:
c
.
Database
.
Source
,
...
...
@@ -34,9 +42,25 @@ func NewData(c *conf.Data, logger log.Logger) (*Data, func(), error) {
Logger
:
NewGormLogger
(),
})
if
err
!=
nil
{
return
nil
,
cleanup
,
err
return
nil
,
err
}
return
&
Data
{
db
:
db
},
cleanup
,
nil
sqlDb
,
err
:=
db
.
DB
()
if
err
!=
nil
{
return
nil
,
err
}
// the maximum number of connections in the idle connection pool.
if
c
.
Database
.
MaxIdle
>
0
{
sqlDb
.
SetMaxIdleConns
(
int
(
c
.
Database
.
MaxIdle
))
}
// the maximum number of open connections to the database.
if
c
.
Database
.
MaxOpen
>
0
{
sqlDb
.
SetMaxOpenConns
(
int
(
c
.
Database
.
MaxOpen
))
}
// the maximum amount of time a connection may be reused.
if
c
.
Database
.
MaxLifetime
!=
nil
{
sqlDb
.
SetConnMaxLifetime
(
c
.
Database
.
MaxLifetime
.
AsDuration
())
}
return
db
,
nil
}
func
NewGormLogger
()
logger
.
Interface
{
...
...
internal/server/http.go
View file @
e4755882
...
...
@@ -15,6 +15,8 @@ func NewHTTPServer(c *conf.Server, web *router.Web, logger log.Logger) *kratos_h
var
opts
=
[]
kratos_http
.
ServerOption
{
kratos_http
.
Middleware
(
recovery
.
Recovery
(),
// tracing.Server(),
// logging.Server(logger),
),
}
if
c
.
Http
.
Network
!=
""
{
...
...
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