mirror of https://gitee.com/answerdev/answer.git
Merge branch 'feat/assign-install-path' into 'main'
feat: support specify the installation directory See merge request opensource/answer!58
This commit is contained in:
commit
11054a36e3
|
@ -9,15 +9,20 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// confFlag is the config flag.
|
// configFilePath is the config file path
|
||||||
confFlag string
|
configFilePath string
|
||||||
|
// dataDirPath save all answer application data in this directory. like config file, upload file...
|
||||||
|
dataDirPath string
|
||||||
// dumpDataPath dump data path
|
// dumpDataPath dump data path
|
||||||
dumpDataPath string
|
dumpDataPath string
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
rootCmd.Version = Version
|
rootCmd.Version = Version
|
||||||
runCmd.Flags().StringVarP(&confFlag, "config", "c", "/data/conf/config.yaml", "config path, eg: -c config.yaml")
|
|
||||||
|
initCmd.Flags().StringVarP(&dataDirPath, "data-path", "C", "/data/", "data path, eg: -C ./data/")
|
||||||
|
|
||||||
|
runCmd.Flags().StringVarP(&configFilePath, "config", "c", "", "config path, eg: -c config.yaml")
|
||||||
|
|
||||||
dumpCmd.Flags().StringVarP(&dumpDataPath, "path", "p", "./", "dump data path, eg: -p ./dump/data/")
|
dumpCmd.Flags().StringVarP(&dumpDataPath, "path", "p", "./", "dump data path, eg: -p ./dump/data/")
|
||||||
|
|
||||||
|
@ -55,7 +60,7 @@ To run answer, use:
|
||||||
Short: "init answer application",
|
Short: "init answer application",
|
||||||
Long: `init answer application`,
|
Long: `init answer application`,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
cli.InstallAllInitialEnvironment()
|
cli.InstallAllInitialEnvironment(dataDirPath)
|
||||||
c, err := readConfig()
|
c, err := readConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("read config failed: ", err.Error())
|
fmt.Println("read config failed: ", err.Error())
|
||||||
|
@ -108,7 +113,7 @@ To run answer, use:
|
||||||
Long: `Check if the current environment meets the startup requirements`,
|
Long: `Check if the current environment meets the startup requirements`,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
fmt.Println("Start checking the required environment...")
|
fmt.Println("Start checking the required environment...")
|
||||||
if cli.CheckConfigFile(confFlag) {
|
if cli.CheckConfigFile(configFilePath) {
|
||||||
fmt.Println("config file exists [✔]")
|
fmt.Println("config file exists [✔]")
|
||||||
} else {
|
} else {
|
||||||
fmt.Println("config file not exists [x]")
|
fmt.Println("config file not exists [x]")
|
||||||
|
|
|
@ -2,9 +2,11 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/segmentfault/answer/internal/base/conf"
|
"github.com/segmentfault/answer/internal/base/conf"
|
||||||
|
"github.com/segmentfault/answer/internal/cli"
|
||||||
"github.com/segmentfault/pacman"
|
"github.com/segmentfault/pacman"
|
||||||
"github.com/segmentfault/pacman/contrib/conf/viper"
|
"github.com/segmentfault/pacman/contrib/conf/viper"
|
||||||
"github.com/segmentfault/pacman/contrib/log/zap"
|
"github.com/segmentfault/pacman/contrib/log/zap"
|
||||||
|
@ -52,8 +54,11 @@ func runApp() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func readConfig() (c *conf.AllConfig, err error) {
|
func readConfig() (c *conf.AllConfig, err error) {
|
||||||
|
if len(configFilePath) == 0 {
|
||||||
|
configFilePath = filepath.Join(cli.ConfigFilePath, cli.DefaultConfigFileName)
|
||||||
|
}
|
||||||
c = &conf.AllConfig{}
|
c = &conf.AllConfig{}
|
||||||
config, err := viper.NewWithPath(confFlag)
|
config, err := viper.NewWithPath(configFilePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,5 +16,5 @@ swaggerui:
|
||||||
address: ':80'
|
address: ':80'
|
||||||
service_config:
|
service_config:
|
||||||
secret_key: "answer"
|
secret_key: "answer"
|
||||||
web_host: "http://127.0.0.1"
|
web_host: "http://127.0.0.1:9080"
|
||||||
upload_path: "/data/upfiles"
|
upload_path: "/data/upfiles"
|
||||||
|
|
|
@ -11,7 +11,7 @@ services:
|
||||||
links:
|
links:
|
||||||
- db
|
- db
|
||||||
volumes:
|
volumes:
|
||||||
- answer-data:/data
|
- ./answer/data:/data
|
||||||
db:
|
db:
|
||||||
image: mariadb:10.4.7
|
image: mariadb:10.4.7
|
||||||
ports:
|
ports:
|
||||||
|
@ -24,5 +24,5 @@ services:
|
||||||
test: [ "CMD", "mysqladmin" ,"ping", "-uroot", "-proot"]
|
test: [ "CMD", "mysqladmin" ,"ping", "-uroot", "-proot"]
|
||||||
timeout: 20s
|
timeout: 20s
|
||||||
retries: 10
|
retries: 10
|
||||||
volumes:
|
volumes:
|
||||||
answer-data:
|
- ./answer/mysql:/var/lib/mysql
|
||||||
|
|
|
@ -16,13 +16,21 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
defaultConfigFilePath = "/data/conf/"
|
DefaultConfigFileName = "config.yaml"
|
||||||
defaultUploadFilePath = "/data/upfiles/"
|
)
|
||||||
defaultI18nPath = "/data/i18n/"
|
|
||||||
|
var (
|
||||||
|
ConfigFilePath = "/conf/"
|
||||||
|
UploadFilePath = "/upfiles/"
|
||||||
|
I18nPath = "/i18n/"
|
||||||
)
|
)
|
||||||
|
|
||||||
// InstallAllInitialEnvironment install all initial environment
|
// InstallAllInitialEnvironment install all initial environment
|
||||||
func InstallAllInitialEnvironment() {
|
func InstallAllInitialEnvironment(dataDirPath string) {
|
||||||
|
ConfigFilePath = filepath.Join(dataDirPath, ConfigFilePath)
|
||||||
|
UploadFilePath = filepath.Join(dataDirPath, UploadFilePath)
|
||||||
|
I18nPath = filepath.Join(dataDirPath, I18nPath)
|
||||||
|
|
||||||
installConfigFile()
|
installConfigFile()
|
||||||
installUploadDir()
|
installUploadDir()
|
||||||
installI18nBundle()
|
installI18nBundle()
|
||||||
|
@ -32,21 +40,21 @@ func InstallAllInitialEnvironment() {
|
||||||
|
|
||||||
func installConfigFile() {
|
func installConfigFile() {
|
||||||
fmt.Println("[config-file] try to install...")
|
fmt.Println("[config-file] try to install...")
|
||||||
defaultConfigFile := filepath.Join(defaultConfigFilePath, "config.yaml")
|
defaultConfigFile := filepath.Join(ConfigFilePath, DefaultConfigFileName)
|
||||||
|
|
||||||
// if config file already exists do nothing.
|
// if config file already exists do nothing.
|
||||||
if CheckConfigFile(defaultConfigFile) {
|
if CheckConfigFile(defaultConfigFile) {
|
||||||
fmt.Println("[config-file] already exists")
|
fmt.Printf("[config-file] %s already exists\n", defaultConfigFile)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := dir.CreatePathIsNotExist(defaultConfigFilePath); err != nil {
|
if _, err := dir.CreatePathIsNotExist(ConfigFilePath); err != nil {
|
||||||
fmt.Printf("[config-file] create directory fail %s\n", err.Error())
|
fmt.Printf("[config-file] create directory fail %s\n", err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fmt.Printf("[config-file] create directory success\n")
|
fmt.Printf("[config-file] create directory success, config file is %s\n", defaultConfigFile)
|
||||||
|
|
||||||
if err := WriterFile(defaultConfigFile, string(configs.Config)); err != nil {
|
if err := writerFile(defaultConfigFile, string(configs.Config)); err != nil {
|
||||||
fmt.Printf("[config-file] install fail %s\n", err.Error())
|
fmt.Printf("[config-file] install fail %s\n", err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -55,16 +63,16 @@ func installConfigFile() {
|
||||||
|
|
||||||
func installUploadDir() {
|
func installUploadDir() {
|
||||||
fmt.Println("[upload-dir] try to install...")
|
fmt.Println("[upload-dir] try to install...")
|
||||||
if _, err := dir.CreatePathIsNotExist(defaultUploadFilePath); err != nil {
|
if _, err := dir.CreatePathIsNotExist(UploadFilePath); err != nil {
|
||||||
fmt.Printf("[upload-dir] install fail %s\n", err.Error())
|
fmt.Printf("[upload-dir] install fail %s\n", err.Error())
|
||||||
} else {
|
} else {
|
||||||
fmt.Printf("[upload-dir] install success\n")
|
fmt.Printf("[upload-dir] install success, upload directory is %s\n", UploadFilePath)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func installI18nBundle() {
|
func installI18nBundle() {
|
||||||
fmt.Println("[i18n] try to install i18n bundle...")
|
fmt.Println("[i18n] try to install i18n bundle...")
|
||||||
if _, err := dir.CreatePathIsNotExist(defaultI18nPath); err != nil {
|
if _, err := dir.CreatePathIsNotExist(I18nPath); err != nil {
|
||||||
fmt.Println(err.Error())
|
fmt.Println(err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -76,13 +84,13 @@ func installI18nBundle() {
|
||||||
}
|
}
|
||||||
fmt.Printf("[i18n] find i18n bundle %d\n", len(i18nList))
|
fmt.Printf("[i18n] find i18n bundle %d\n", len(i18nList))
|
||||||
for _, item := range i18nList {
|
for _, item := range i18nList {
|
||||||
path := filepath.Join(defaultI18nPath, item.Name())
|
path := filepath.Join(I18nPath, item.Name())
|
||||||
content, err := i18n.I18n.ReadFile(item.Name())
|
content, err := i18n.I18n.ReadFile(item.Name())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
fmt.Printf("[i18n] install %s bundle...\n", item.Name())
|
fmt.Printf("[i18n] install %s bundle...\n", item.Name())
|
||||||
err = WriterFile(path, string(content))
|
err = writerFile(path, string(content))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("[i18n] install %s bundle fail: %s\n", item.Name(), err.Error())
|
fmt.Printf("[i18n] install %s bundle fail: %s\n", item.Name(), err.Error())
|
||||||
} else {
|
} else {
|
||||||
|
@ -91,7 +99,7 @@ func installI18nBundle() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func WriterFile(filePath, content string) error {
|
func writerFile(filePath, content string) error {
|
||||||
file, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE, 0666)
|
file, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE, 0666)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -10,7 +10,7 @@ func CheckConfigFile(configPath string) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
func CheckUploadDir() bool {
|
func CheckUploadDir() bool {
|
||||||
return dir.CheckPathExist(defaultUploadFilePath)
|
return dir.CheckPathExist(UploadFilePath)
|
||||||
}
|
}
|
||||||
|
|
||||||
func CheckDB(dataConf *data.Database) bool {
|
func CheckDB(dataConf *data.Database) bool {
|
||||||
|
|
Loading…
Reference in New Issue