feat: support choose database driver

This commit is contained in:
LinkinStar 2022-10-12 17:12:17 +08:00
parent 1e561157ba
commit 189916e843
7 changed files with 15 additions and 5 deletions

View File

@ -17,7 +17,7 @@ var (
// Name is the name of the project
Name = "answer"
// Version is the version of the project
Version = "unknown"
Version = "development"
// log level
logLevel = os.Getenv("LOG_LEVEL")
// log path

View File

@ -3,6 +3,7 @@ server:
addr: 0.0.0.0:80
data:
database:
driver: "mysql"
connection: root:root@tcp(db:3306)/answer
cache:
file_path: "/tmp/cache/cache.db"

5
go.mod
View File

@ -16,6 +16,8 @@ require (
github.com/jinzhu/copier v0.3.5
github.com/jinzhu/now v1.1.5
github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible
github.com/lib/pq v1.10.2
github.com/mattn/go-sqlite3 v2.0.3+incompatible
github.com/mojocn/base64Captcha v1.3.5
github.com/segmentfault/pacman v1.0.1
github.com/segmentfault/pacman/contrib/cache/memory v0.0.0-20220929065758-260b3093a347
@ -23,6 +25,7 @@ require (
github.com/segmentfault/pacman/contrib/i18n v0.0.0-20220929065758-260b3093a347
github.com/segmentfault/pacman/contrib/log/zap v0.0.0-20220929065758-260b3093a347
github.com/segmentfault/pacman/contrib/server/http v0.0.0-20220929065758-260b3093a347
github.com/spf13/cobra v1.5.0
github.com/stretchr/testify v1.8.0
github.com/swaggo/files v0.0.0-20220728132757-551d4a08d97a
github.com/swaggo/gin-swagger v1.5.3
@ -55,7 +58,6 @@ require (
github.com/magiconair/properties v1.8.6 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/mattn/go-sqlite3 v2.0.3+incompatible // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
@ -67,7 +69,6 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/afero v1.9.2 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/cobra 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.13.0 // indirect

1
go.sum
View File

@ -385,6 +385,7 @@ github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.1.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.3.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.10.2 h1:AqzbZs4ZoCBp+GtejcpCpcxM3zlSMx29dXbUSeVtJb8=
github.com/lib/pq v1.10.2/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/lightstep/lightstep-tracer-common/golang/gogo v0.0.0-20190605223551-bc2310a04743/go.mod h1:qklhhLq1aX+mtWk9cPHPzaBjWImj5ULL6C7HFJtXQMM=
github.com/lightstep/lightstep-tracer-go v0.18.1/go.mod h1:jlF1pusYV4pidLvZ+XD0UBX0ZE6WURAspgAczcDHrL4=

View File

@ -2,6 +2,7 @@ package data
// Database database config
type Database struct {
Driver string `json:"driver" mapstructure:"driver"`
Connection string `json:"connection" mapstructure:"connection"`
ConnMaxLifeTime int `json:"conn_max_life_time" mapstructure:"conn_max_life_time"`
MaxOpenConn int `json:"max_open_conn" mapstructure:"max_open_conn"`

View File

@ -4,12 +4,15 @@ import (
"time"
_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"
"github.com/segmentfault/pacman/cache"
"github.com/segmentfault/pacman/contrib/cache/memory"
"github.com/segmentfault/pacman/log"
"xorm.io/core"
"xorm.io/xorm"
ormlog "xorm.io/xorm/log"
"xorm.io/xorm/schemas"
)
// Data data
@ -29,7 +32,10 @@ func NewData(db *xorm.Engine, cache cache.Cache) (*Data, func(), error) {
// NewDB new database instance
func NewDB(debug bool, dataConf *Database) (*xorm.Engine, error) {
engine, err := xorm.NewEngine("mysql", dataConf.Connection)
if dataConf.Driver == "" {
dataConf.Driver = string(schemas.MYSQL)
}
engine, err := xorm.NewEngine(dataConf.Driver, dataConf.Connection)
if err != nil {
return nil, err
}

View File

@ -125,7 +125,7 @@ func (qc *QuestionController) SimilarQuestion(ctx *gin.Context) {
// @Produce json
// @Param data body schema.QuestionSearch true "QuestionSearch"
// @Success 200 {string} string ""
// @Router /answer/api/v1/question/page [post]
// @Router /answer/api/v1/question/page [get]
func (qc *QuestionController) Index(ctx *gin.Context) {
req := &schema.QuestionSearch{}
if handler.BindAndCheck(ctx, req) {