Merge branch 'ai_work' into 'main'

Ai work

See merge request opensource/answer!24
This commit is contained in:
linkinstar 2022-09-29 13:16:01 +00:00
commit 966a61839e
11 changed files with 420 additions and 589 deletions

View File

@ -1 +1,86 @@
# How to build and install
Before installing Answer, you need to install the base environment first.
- database
- [MySQL](http://dev.mysql.com) Version >= 5.7
You can then install Answer in several ways:
- Deploy with Docker
- binary installation
- Source installation
## Docker for Answer
Visit Docker Hub or GitHub Container registry to see all available images and tags.
### Usage
To keep your data out of Docker container, we do a volume (/var/data -> /data) here, and you can change it based on your situation.
```
# Pull image from Docker Hub.
$ docker pull answer/answer
# Create local directory for volume.
$ mkdir -p /var/data
# Run the image first
$ docker run --name=answer -p 9080:80 -v /var/data:/data answer/answer
# After the first startup, a configuration file will be generated in the /var/data directory
# /var/data/config.yaml
# Need to modify the Mysql database address in the configuration file
vim /var/data/config.yaml
# Modify database connection
# connection: [username]:[password]@tcp([host]:[port])/[DbName]
...
# After configuring the configuration file, you can start the mirror again to start the service
$ docker start answer
```
## Binary for Answer
## Install Answer using binary
1. Unzip the compressed package.
2. Use the command cd to enter the directory you just created.
3. Execute the command ./answer init.
4. Answer will generate a ./data directory in the current directory
5. Enter the data directory and modify the config.yaml file
6. Modify the database connection address to your database connection address
connection: [username]:[password]@tcp([host]:[port])/[DbName]
7. Exit the data directory and execute ./answer run -c ./data/config.yaml
## config.yaml Description
```
server:
http:
addr: 0.0.0.0:80 #Project access port number
data:
database:
connection: root:root@tcp(127.0.0.1:3306)/answer #MySQL database connection address
cache:
file_path: "/tmp/cache/cache.db" #Cache file storage path
i18n:
bundle_dir: "/data/i18n" #Internationalized file storage directory
swaggerui:
show: true #Whether to display the swaggerapi documentation, address /swagger/index.html
protocol: http #swagger protocol header
host: 127.0.0.1 #An accessible IP address or domain name
address: ':80' #accessible port number
service_config:
secret_key: "answer" #encryption key
web_host: "http://127.0.0.1" #Page access using domain name address
upload_path: "./upfiles" #upload directory
```
## Compile the image
If you have modified the source files and want to repackage the image, you can use the following statement to repackage the image
```
docker build -t answer:v1.0.0 .
```
## common problem
1. The project cannot be started, answer the main program startup depends on the configuration file config.yaml, the internationalization translation directory/i18n, the upload file storage directory/upfiles, you need to ensure that the configuration file is loaded when the project starts, answer run -c config.yaml and the correct config.yaml The configuration items that specify the i18n and upfiles directories

View File

@ -1,9 +1,85 @@
# Answer 安装指引
## 前端安装
安装 Answer 之前,您需要先安装基本环境。
- 数据库
- [MySQL](http://dev.mysql.com):版本 >= 5.7
## 后端安装
然后,您可以通过以下几种种方式来安装 Answer
- 采用 Docker 部署
- 二进制安装
- 源码安装
## 使用Docker 安装 Answer
可以从 Docker Hub 或者 GitHub Container registry 下载最新的tags 镜像
### 用法
将配置和存储目录挂在到镜像之外 volume (/var/data -> /data),你可以修改外部挂载地址
```
# 将镜像从 docker hub 拉到本地
$ docker pull answer/answer
# 创建一个挂载目录
$ mkdir -p /var/data
# 先运行一遍镜像
$ docker run --name=answer -p 9080:80 -v /var/data:/data answer/answer
# 第一次启动后会在/var/data 目录下生成配置文件
# /var/data/config.yaml
# 需要修改配置文件中的Mysql 数据库地址
vim /var/data/config.yaml
# 修改数据库连接 connection: [username]:[password]@tcp([host]:[port])/[DbName]
...
# 配置好配置文件后可以再次启动镜像即可启动服务
$ docker start answer
```
## 使用二进制 安装 Answer
可以使用编译完成的各个平台的二进制文件运行 Answer 项目
### 用法
从GitHub 最新版本的tag中下载对应平台的二进制文件压缩包
1. 解压压缩包。
2. 使用命令 cd 进入到刚刚创建的目录。
3. 执行命令 ./answer init。
4. Answer 会在当前目录生成./data 目录
5. 进入data目录修改config.yaml文件
6. 将数据库连接地址修改为你的数据库连接地址
connection: [username]:[password]@tcp([host]:[port])/[DbName]
7. 退出data 目录 执行 ./answer run -c ./data/config.yaml
## 配置文件 config.yaml 参数说明
```
server:
http:
addr: 0.0.0.0:80 #项目访问端口号
data:
database:
connection: root:root@tcp(127.0.0.1:3306)/answer #mysql数据库连接地址
cache:
file_path: "/tmp/cache/cache.db" #缓存文件存放路径
i18n:
bundle_dir: "/data/i18n" #国际化文件存放目录
swaggerui:
show: true #是否显示swaggerapi文档,地址 /swagger/index.html
protocol: http #swagger 协议头
host: 127.0.0.1 #可被访问的ip地址或域名
address: ':80' #可被访问的端口号
service_config:
secret_key: "answer" #加密key
web_host: "http://127.0.0.1" #页面访问使用域名地址
upload_path: "./upfiles" #上传目录
```
## 编译镜像
如果修改了源文件并且要重新打包镜像可以使用以下语句重新打包镜像
```
docker build -t answer:v1.0.0 .
```
## 常见问题
1. 项目无法启动,answer主程序启动依赖配置文件config.yaml 、国际化翻译目录 /i18n 、上传文件存放目录/upfiles 需要确保项目启动时加载了配置文件 answer run -c config.yaml 以及在config.yaml 正确的指定i18n 和 upfiles 目录的配置项

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,
uiRouter := router.NewUIRouter()
authUserMiddleware := middleware.NewAuthUserMiddleware(authService)
ginEngine := server.NewHTTPServer(debug, staticRouter, answerAPIRouter, swaggerRouter, uiRouter, 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,9 +1,24 @@
version: "3"
version: "3.9"
services:
answer:
build:
context: .
image: github.com/segmentfault/answer
volumes:
- ./data:/data
ports:
- '9080:80'
restart: on-failure
depends_on:
db:
condition: service_healthy
links:
- db
db:
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

@ -11,7 +11,7 @@ import (
)
var SuccessMsg = `
answer initialized successfully
answer initialized successfully.
`
var HasBeenInitializedMsg = `
@ -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
}

View File

@ -3,14 +3,17 @@ package cli
import "fmt"
var usageDoc = `
answer
Welcome to answer
USAGE
answer command
VERSION:
1.0.0
COMMANDS
init init answer config, eg:answer init
run config path, eg:answer run -c data/config.yaml
USAGE:
answer [global options] command [command options] [arguments...]
COMMANDS:
init Init config, eg:./answer init
run Start web server, eg:./answer run -c data/config.yaml
`
func Usage() {