diff --git a/configs/dBlog.sql b/configs/dBlog.sql index 53fa4f9..9de42c7 100644 --- a/configs/dBlog.sql +++ b/configs/dBlog.sql @@ -1,3 +1,8 @@ +-- Copyright 2023 daz-3ux(杨鹏达) . All rights reserved. +-- Use of this source code is governed by a MIT style +-- license that can be found in the LICENSE file. The original repo for +-- this file is https://github.com/Daz-3ux/dBlog. + -- MariaDB dump 10.19-11.1.2-MariaDB, for Linux (x86_64) -- -- Host: 127.0.0.1 Database: dazBlog diff --git a/configs/dazBlog.yaml b/configs/dazBlog.yaml index cbbd3ef..3b2084e 100644 --- a/configs/dazBlog.yaml +++ b/configs/dazBlog.yaml @@ -10,9 +10,9 @@ addr: :8081 # HTTP server listen port # mysql configs db: host: 127.0.0.1 - username: dazblog - password: dazblog - database: dazblog + username: root + password: passwd + database: dazBlog max-idle-connections: 100 max-open-connections: 100 max-connection-life-time: 10s diff --git a/go.mod b/go.mod index c5d7886..191b158 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,7 @@ require ( github.com/spf13/viper v1.16.0 go.uber.org/automaxprocs v1.5.3 go.uber.org/zap v1.25.0 + gorm.io/driver/mysql v1.5.1 gorm.io/gorm v1.25.4 ) @@ -24,6 +25,7 @@ require ( github.com/go-playground/locales v0.14.1 // indirect github.com/go-playground/universal-translator v0.18.1 // indirect github.com/go-playground/validator/v10 v10.14.0 // indirect + github.com/go-sql-driver/mysql v1.7.0 // indirect github.com/goccy/go-json v0.10.2 // indirect github.com/hashicorp/hcl v1.0.0 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect diff --git a/go.sum b/go.sum index c016489..425f205 100644 --- a/go.sum +++ b/go.sum @@ -87,6 +87,8 @@ github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJn github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= github.com/go-playground/validator/v10 v10.14.0 h1:vgvQWe3XCz3gIeFDm/HnTIbj6UGmg/+t63MyGU2n5js= github.com/go-playground/validator/v10 v10.14.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU= +github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc= +github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= @@ -571,6 +573,9 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gorm.io/driver/mysql v1.5.1 h1:WUEH5VF9obL/lTtzjmML/5e6VfFR/788coz2uaVCAZw= +gorm.io/driver/mysql v1.5.1/go.mod h1:Jo3Xu7mMhCyj8dlrb3WoCaRd1FhsVh+yMXb1jUInf5o= +gorm.io/gorm v1.25.1/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k= gorm.io/gorm v1.25.4 h1:iyNd8fNAe8W9dvtlgeRI5zSVZPsq3OpcTu37cYcpCmw= gorm.io/gorm v1.25.4/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/internal/dazBlog/dazBlog.go b/internal/dazBlog/dazBlog.go index 02f168f..d00c8f3 100644 --- a/internal/dazBlog/dazBlog.go +++ b/internal/dazBlog/dazBlog.go @@ -85,6 +85,11 @@ Find more dBlog information at: } func run() error { + // init the store layer + if err := initStore(); err != nil { + return err + } + // set Gin mode gin.SetMode(viper.GetString("runmode")) diff --git a/internal/dazBlog/helper.go b/internal/dazBlog/helper.go index 918070d..67b7e35 100644 --- a/internal/dazBlog/helper.go +++ b/internal/dazBlog/helper.go @@ -6,7 +6,9 @@ package dazBlog import ( + "github.com/Daz-3ux/dBlog/internal/dazBlog/store" "github.com/Daz-3ux/dBlog/internal/pkg/log" + "github.com/Daz-3ux/dBlog/pkg/db" "github.com/spf13/cobra" "github.com/spf13/viper" "os" @@ -70,3 +72,27 @@ func logOptions() *log.Options { OutputPaths: viper.GetStringSlice("log.output-paths"), } } + +// initStore read the DB config from viper and init the store layer +func initStore() error { + // init the store layer + dbOptions := &db.MySQLOptions{ + Host: viper.GetString("db.host"), + Username: viper.GetString("db.username"), + Password: viper.GetString("db.password"), + Database: viper.GetString("db.database"), + MaxIdleConnections: viper.GetInt("db.max-idle-connections"), + MaxOpenConnections: viper.GetInt("db.max-open-connections"), + MaxConnectionLifeTime: viper.GetDuration("db.max-connection-life-time"), + LogLevel: viper.GetInt("db.log-level"), + } + + ins, err := db.NewMySql(dbOptions) + if err != nil { + return err + } + + _ = store.NewStore(ins) + + return nil +} diff --git a/internal/dazBlog/store/doc.go b/internal/dazBlog/store/doc.go index 9797ac6..3ed7289 100644 --- a/internal/dazBlog/store/doc.go +++ b/internal/dazBlog/store/doc.go @@ -1,2 +1,7 @@ +// Copyright 2023 daz-3ux(杨鹏达) . All rights reserved. +// Use of this source code is governed by a MIT style +// license that can be found in the LICENSE file. The original repo for +// this file is https://github.com/Daz-3ux/dBlog. + // Package store defines the store layer of dazBlog package store // import "github.com/Daz-3ux/dBlog/internal/dazBlog/store" diff --git a/internal/dazBlog/store/store.go b/internal/dazBlog/store/store.go index e480143..01b62f0 100644 --- a/internal/dazBlog/store/store.go +++ b/internal/dazBlog/store/store.go @@ -1,3 +1,8 @@ +// Copyright 2023 daz-3ux(杨鹏达) . All rights reserved. +// Use of this source code is governed by a MIT style +// license that can be found in the LICENSE file. The original repo for +// this file is https://github.com/Daz-3ux/dBlog. + package store import ( diff --git a/internal/dazBlog/store/user.go b/internal/dazBlog/store/user.go index 369a4b8..5a2071c 100644 --- a/internal/dazBlog/store/user.go +++ b/internal/dazBlog/store/user.go @@ -1,3 +1,8 @@ +// Copyright 2023 daz-3ux(杨鹏达) . All rights reserved. +// Use of this source code is governed by a MIT style +// license that can be found in the LICENSE file. The original repo for +// this file is https://github.com/Daz-3ux/dBlog. + package store import ( diff --git a/internal/pkg/model/post.go b/internal/pkg/model/post.go index 8f3eb82..4e94781 100644 --- a/internal/pkg/model/post.go +++ b/internal/pkg/model/post.go @@ -1,3 +1,8 @@ +// Copyright 2023 daz-3ux(杨鹏达) . All rights reserved. +// Use of this source code is governed by a MIT style +// license that can be found in the LICENSE file. The original repo for +// this file is https://github.com/Daz-3ux/dBlog. + package model import "time" diff --git a/internal/pkg/model/user.go b/internal/pkg/model/user.go index 121c30a..3975091 100644 --- a/internal/pkg/model/user.go +++ b/internal/pkg/model/user.go @@ -1,3 +1,8 @@ +// Copyright 2023 daz-3ux(杨鹏达) . All rights reserved. +// Use of this source code is governed by a MIT style +// license that can be found in the LICENSE file. The original repo for +// this file is https://github.com/Daz-3ux/dBlog. + package model import "time" diff --git a/pkg/db/db.go b/pkg/db/db.go new file mode 100644 index 0000000..b0832c4 --- /dev/null +++ b/pkg/db/db.go @@ -0,0 +1,61 @@ +// Copyright 2023 daz-3ux(杨鹏达) . All rights reserved. +// Use of this source code is governed by a MIT style +// license that can be found in the LICENSE file. The original repo for +// this file is https://github.com/Daz-3ux/dBlog. + +package db + +import ( + "fmt" + "gorm.io/driver/mysql" + "gorm.io/gorm" + "gorm.io/gorm/logger" + "time" +) + +// MySQLOptions defines the options for connecting to MySQL +type MySQLOptions struct { + Host string + Username string + Password string + Database string + MaxIdleConnections int + MaxOpenConnections int + MaxConnectionLifeTime time.Duration + LogLevel int +} + +func (o *MySQLOptions) DSN() string { + return fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8&parseTime=%t&loc=%s", + o.Username, + o.Password, + o.Host, + o.Database, + true, + "Local") +} + +func NewMySql(opts *MySQLOptions) (*gorm.DB, error) { + logLevel := logger.Silent + if opts.LogLevel != 0 { + logLevel = logger.LogLevel(opts.LogLevel) + } + db, err := gorm.Open(mysql.Open(opts.DSN()), &gorm.Config{ + Logger: logger.Default.LogMode(logLevel), + }) + if err != nil { + return nil, err + } + + // sqlDB is the low-level SQL connection object + sqlDB, err := db.DB() + if err != nil { + return nil, err + } + + sqlDB.SetMaxIdleConns(opts.MaxIdleConnections) + sqlDB.SetMaxOpenConns(opts.MaxOpenConnections) + sqlDB.SetConnMaxLifetime(opts.MaxConnectionLifeTime) + + return db, nil +} diff --git a/pkg/db/doc.go b/pkg/db/doc.go new file mode 100644 index 0000000..a494d8d --- /dev/null +++ b/pkg/db/doc.go @@ -0,0 +1,7 @@ +// Copyright 2023 daz-3ux(杨鹏达) . All rights reserved. +// Use of this source code is governed by a MIT style +// license that can be found in the LICENSE file. The original repo for +// this file is https://github.com/Daz-3ux/dBlog. + +// Package db provides useful functions to create mysql instance +package db // import "github.com/Daz-3ux/dBlog/pkg/db"