update: support docker-compose

This commit is contained in:
LinkinStar 2022-09-29 21:08:00 +08:00
parent 9adfbcae67
commit 3b7be87667
8 changed files with 220 additions and 325 deletions

File diff suppressed because one or more lines are too long

View File

@ -69,12 +69,12 @@ func main() {
panic(err)
}
app, cleanup, err := initApplication(
c.Debug, c.Server, c.Data.Database, c.Data.Cache, c.I18n, c.Swaggerui, c.ServiceConfig, log.GetLogger())
err = cli.InitDB(c.Data.Database)
if err != nil {
panic(err)
}
err = cli.CommandCli.InitDB()
app, cleanup, err := initApplication(
c.Debug, c.Server, c.Data.Database, c.Data.Cache, c.I18n, c.Swaggerui, c.ServiceConfig, log.GetLogger())
if err != nil {
panic(err)
}
@ -85,7 +85,7 @@ func main() {
}
}
func newApplication(serverConf *conf.Server, server *gin.Engine, cli *cli.Cli) *pacman.Application {
func newApplication(serverConf *conf.Server, server *gin.Engine) *pacman.Application {
return pacman.NewApp(
pacman.WithName(Name),
pacman.WithVersion(Version),

View File

@ -12,7 +12,6 @@ import (
"github.com/segmentfault/answer/internal/base/middleware"
"github.com/segmentfault/answer/internal/base/server"
"github.com/segmentfault/answer/internal/base/translator"
"github.com/segmentfault/answer/internal/cli"
"github.com/segmentfault/answer/internal/controller"
"github.com/segmentfault/answer/internal/controller_backyard"
"github.com/segmentfault/answer/internal/repo"
@ -34,7 +33,6 @@ func initApplication(
serviceConf *service_config.ServiceConfig,
logConf log.Logger) (*pacman.Application, func(), error) {
panic(wire.Build(
cli.ProviderSetCli,
server.ProviderSetServer,
router.ProviderSetRouter,
controller.ProviderSetController,

View File

@ -12,7 +12,6 @@ import (
"github.com/segmentfault/answer/internal/base/middleware"
"github.com/segmentfault/answer/internal/base/server"
"github.com/segmentfault/answer/internal/base/translator"
"github.com/segmentfault/answer/internal/cli"
"github.com/segmentfault/answer/internal/controller"
"github.com/segmentfault/answer/internal/controller_backyard"
"github.com/segmentfault/answer/internal/repo"
@ -173,8 +172,7 @@ func initApplication(debug bool, serverConf *conf.Server, dbConf *data.Database,
viewRouter := router.NewViewRouter()
authUserMiddleware := middleware.NewAuthUserMiddleware(authService)
ginEngine := server.NewHTTPServer(debug, staticRouter, answerAPIRouter, swaggerRouter, viewRouter, authUserMiddleware)
cliCli := cli.NewCli(dataData)
application := newApplication(serverConf, ginEngine, cliCli)
application := newApplication(serverConf, ginEngine)
return application, func() {
cleanup2()
cleanup()

View File

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

View File

@ -1,4 +1,4 @@
version: "3.1"
version: "3.9"
services:
answer:
image: github.com/segmentfault/answer
@ -6,15 +6,19 @@ services:
- '9080:80'
restart: on-failure
depends_on:
- db
db:
condition: service_healthy
links:
- db
db:
image: mysql:8.0.30
command: --default-authentication-plugin=mysql_native_password
image: mariadb:10.4.7
ports:
- '13306:3306'
restart: on-failure
environment:
MYSQL_DATABASE: answer
MYSQL_ROOT_PASSWORD: root
healthcheck:
test: [ "CMD", "mysqladmin" ,"ping", "-uroot", "-proot"]
timeout: 20s
retries: 10

View File

@ -62,10 +62,6 @@ func InitConfig() {
os.Exit(0)
}
func InitDB() {
}
func WriterFile(filePath, content string) error {
file, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {

View File

@ -3,35 +3,21 @@ package cli
import (
"bytes"
"github.com/google/wire"
"github.com/segmentfault/answer/assets"
"github.com/segmentfault/answer/internal/base/data"
"github.com/segmentfault/answer/internal/entity"
)
// ProviderSetCli is providers.
var ProviderSetCli = wire.NewSet(NewCli)
type Cli struct {
DataSource *data.Data
}
var CommandCli *Cli
func NewCli(dataSource *data.Data) *Cli {
CommandCli = &Cli{DataSource: dataSource}
return CommandCli
}
// InitDB init db
func (c *Cli) InitDB() (err error) {
func InitDB(dataConf *data.Database) (err error) {
db := data.NewDB(false, dataConf)
// check db connection
err = c.DataSource.DB.Ping()
err = db.Ping()
if err != nil {
return err
}
exist, err := c.DataSource.DB.IsTableExist(&entity.User{})
exist, err := db.IsTableExist(&entity.User{})
if err != nil {
return err
}
@ -42,7 +28,7 @@ func (c *Cli) InitDB() (err error) {
// create table if not exist
s := &bytes.Buffer{}
s.Write(assets.AnswerSql)
_, err = c.DataSource.DB.Import(s)
_, err = db.Import(s)
if err != nil {
return err
}