Skip to content

Commit

Permalink
feat: implement the store layer
Browse files Browse the repository at this point in the history
Signed-off-by: daz-3ux <[email protected]>
  • Loading branch information
Daz-3ux committed Sep 22, 2023
1 parent 2051e4d commit 1b45113
Show file tree
Hide file tree
Showing 13 changed files with 139 additions and 3 deletions.
5 changes: 5 additions & 0 deletions configs/dBlog.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
-- Copyright 2023 daz-3ux(杨鹏达) <[email protected]>. 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
Expand Down
6 changes: 3 additions & 3 deletions configs/dazBlog.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
5 changes: 5 additions & 0 deletions internal/dazBlog/dazBlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))

Expand Down
26 changes: 26 additions & 0 deletions internal/dazBlog/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
5 changes: 5 additions & 0 deletions internal/dazBlog/store/doc.go
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
// Copyright 2023 daz-3ux(杨鹏达) <[email protected]>. 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"
5 changes: 5 additions & 0 deletions internal/dazBlog/store/store.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
// Copyright 2023 daz-3ux(杨鹏达) <[email protected]>. 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 (
Expand Down
5 changes: 5 additions & 0 deletions internal/dazBlog/store/user.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
// Copyright 2023 daz-3ux(杨鹏达) <[email protected]>. 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 (
Expand Down
5 changes: 5 additions & 0 deletions internal/pkg/model/post.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
// Copyright 2023 daz-3ux(杨鹏达) <[email protected]>. 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"
Expand Down
5 changes: 5 additions & 0 deletions internal/pkg/model/user.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
// Copyright 2023 daz-3ux(杨鹏达) <[email protected]>. 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"
Expand Down
61 changes: 61 additions & 0 deletions pkg/db/db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright 2023 daz-3ux(杨鹏达) <[email protected]>. 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
}
7 changes: 7 additions & 0 deletions pkg/db/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Copyright 2023 daz-3ux(杨鹏达) <[email protected]>. 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"

0 comments on commit 1b45113

Please sign in to comment.