A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.
feat: support choose database driver
LinkinStar189916e84341
diff --git a/cmd/answer/main.go b/cmd/answer/main.goindex ad04ad78..c0f67e12 100644--- a/cmd/answer/main.go+++ b/cmd/answer/main.go@@ -17,7 +17,7 @@ var (// Name is the name of the projectName = "answer"// Version is the version of the project- Version = "unknown"+ Version = "development"// log levellogLevel = os.Getenv("LOG_LEVEL")// log pathdiff --git a/configs/config.yaml b/configs/config.yamlindex 84cb323f..1370d12a 100644--- a/configs/config.yaml+++ b/configs/config.yaml@@ -3,6 +3,7 @@ server:addr: 0.0.0.0:80data:database:+ driver: "mysql"connection: root:root@tcp(db:3306)/answercache:file_path: "/tmp/cache/cache.db"diff --git a/go.mod b/go.modindex 4747a460..6cb1ffcc 100644--- a/go.mod+++ b/go.mod@@ -16,6 +16,8 @@ require (github.com/jinzhu/copier v0.3.5github.com/jinzhu/now v1.1.5github.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+incompatiblegithub.com/mojocn/base64Captcha v1.3.5github.com/segmentfault/pacman v1.0.1github.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-260b3093a347github.com/segmentfault/pacman/contrib/log/zap v0.0.0-20220929065758-260b3093a347github.com/segmentfault/pacman/contrib/server/http v0.0.0-20220929065758-260b3093a347+ github.com/spf13/cobra v1.5.0github.com/stretchr/testify v1.8.0github.com/swaggo/files v0.0.0-20220728132757-551d4a08d97agithub.com/swaggo/gin-swagger v1.5.3@@ -55,7 +58,6 @@ require (github.com/magiconair/properties v1.8.6 // indirectgithub.com/mailru/easyjson v0.7.7 // indirectgithub.com/mattn/go-isatty v0.0.16 // indirect- github.com/mattn/go-sqlite3 v2.0.3+incompatible // indirectgithub.com/mitchellh/mapstructure v1.5.0 // indirectgithub.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirectgithub.com/modern-go/reflect2 v1.0.2 // indirect@@ -67,7 +69,6 @@ require (github.com/pmezard/go-difflib v1.0.0 // indirectgithub.com/spf13/afero v1.9.2 // indirectgithub.com/spf13/cast v1.5.0 // indirect- github.com/spf13/cobra v1.5.0 // indirectgithub.com/spf13/jwalterweatherman v1.1.0 // indirectgithub.com/spf13/pflag v1.0.5 // indirectgithub.com/spf13/viper v1.13.0 // indirectdiff --git a/go.sum b/go.sumindex 2ca8da01..c8cd6def 100644--- a/go.sum+++ b/go.sum@@ -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=diff --git a/internal/base/data/config.go b/internal/base/data/config.goindex d7268ab0..6bc4ec74 100644--- a/internal/base/data/config.go+++ b/internal/base/data/config.go@@ -2,6 +2,7 @@ package data// Database database configtype 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"`diff --git a/internal/base/data/data.go b/internal/base/data/data.goindex ccceef6e..cb50f7e0 100644--- a/internal/base/data/data.go+++ b/internal/base/data/data.go@@ -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 instancefunc 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}diff --git a/internal/controller/question_controller.go b/internal/controller/question_controller.goindex 8b93230e..6b9643d0 100644--- a/internal/controller/question_controller.go+++ b/internal/controller/question_controller.go@@ -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) {