2022-11-03 20:09:04 +08:00
|
|
|
package install
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-11-10 11:00:26 +08:00
|
|
|
"net/url"
|
2022-11-03 20:09:04 +08:00
|
|
|
"strings"
|
|
|
|
|
2023-01-28 10:40:01 +08:00
|
|
|
"github.com/answerdev/answer/internal/base/reason"
|
|
|
|
"github.com/answerdev/answer/internal/base/validator"
|
|
|
|
"github.com/answerdev/answer/pkg/checker"
|
|
|
|
"github.com/segmentfault/pacman/errors"
|
2022-11-03 20:09:04 +08:00
|
|
|
"xorm.io/xorm/schemas"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CheckConfigFileResp check config file if exist or not response
|
|
|
|
type CheckConfigFileResp struct {
|
2022-11-10 14:13:29 +08:00
|
|
|
ConfigFileExist bool `json:"config_file_exist"`
|
|
|
|
DBConnectionSuccess bool `json:"db_connection_success"`
|
|
|
|
DbTableExist bool `json:"db_table_exist"`
|
2022-11-03 20:09:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// CheckDatabaseReq check database
|
|
|
|
type CheckDatabaseReq struct {
|
2022-11-04 16:59:45 +08:00
|
|
|
DbType string `validate:"required,oneof=postgres sqlite3 mysql" json:"db_type"`
|
2022-11-03 20:09:04 +08:00
|
|
|
DbUsername string `json:"db_username"`
|
|
|
|
DbPassword string `json:"db_password"`
|
|
|
|
DbHost string `json:"db_host"`
|
|
|
|
DbName string `json:"db_name"`
|
|
|
|
DbFile string `json:"db_file"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetConnection get connection string
|
|
|
|
func (r *CheckDatabaseReq) GetConnection() string {
|
|
|
|
if r.DbType == string(schemas.SQLITE) {
|
|
|
|
return r.DbFile
|
|
|
|
}
|
|
|
|
if r.DbType == string(schemas.MYSQL) {
|
|
|
|
return fmt.Sprintf("%s:%s@tcp(%s)/%s",
|
|
|
|
r.DbUsername, r.DbPassword, r.DbHost, r.DbName)
|
|
|
|
}
|
|
|
|
if r.DbType == string(schemas.POSTGRES) {
|
|
|
|
host, port := parsePgSQLHostPort(r.DbHost)
|
2022-11-10 12:13:42 +08:00
|
|
|
return fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable",
|
2022-11-03 20:09:04 +08:00
|
|
|
host, port, r.DbUsername, r.DbPassword, r.DbName)
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func parsePgSQLHostPort(dbHost string) (host string, port string) {
|
|
|
|
if strings.Contains(dbHost, ":") {
|
|
|
|
idx := strings.LastIndex(dbHost, ":")
|
|
|
|
host, port = dbHost[:idx], dbHost[idx+1:]
|
|
|
|
} else if len(dbHost) > 0 {
|
|
|
|
host = dbHost
|
|
|
|
}
|
|
|
|
if host == "" {
|
|
|
|
host = "127.0.0.1"
|
|
|
|
}
|
|
|
|
if port == "" {
|
|
|
|
port = "5432"
|
|
|
|
}
|
|
|
|
return host, port
|
|
|
|
}
|
|
|
|
|
|
|
|
// CheckDatabaseResp check database response
|
|
|
|
type CheckDatabaseResp struct {
|
|
|
|
ConnectionSuccess bool `json:"connection_success"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// InitEnvironmentResp init environment response
|
|
|
|
type InitEnvironmentResp struct {
|
|
|
|
Success bool `json:"success"`
|
|
|
|
CreateConfigFailed bool `json:"create_config_failed"`
|
|
|
|
DefaultConfig string `json:"default_config"`
|
|
|
|
ErrType string `json:"err_type"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// InitBaseInfoReq init base info request
|
|
|
|
type InitBaseInfoReq struct {
|
2022-11-04 16:59:45 +08:00
|
|
|
Language string `validate:"required,gt=0,lte=30" json:"lang"`
|
2023-02-24 16:06:54 +08:00
|
|
|
SiteName string `validate:"required,sanitizer,gt=0,lte=30" json:"site_name"`
|
2022-11-10 11:00:26 +08:00
|
|
|
SiteURL string `validate:"required,gt=0,lte=512,url" json:"site_url"`
|
2022-11-04 16:59:45 +08:00
|
|
|
ContactEmail string `validate:"required,email,gt=0,lte=500" json:"contact_email"`
|
2023-01-11 17:34:12 +08:00
|
|
|
AdminName string `validate:"required,gt=3,lte=30" json:"name"`
|
2022-11-11 11:24:36 +08:00
|
|
|
AdminPassword string `validate:"required,gte=8,lte=32" json:"password"`
|
|
|
|
AdminEmail string `validate:"required,email,gt=0,lte=500" json:"email"`
|
2022-11-03 20:09:04 +08:00
|
|
|
}
|
2022-11-10 11:00:26 +08:00
|
|
|
|
2023-01-28 10:40:01 +08:00
|
|
|
func (r *InitBaseInfoReq) Check() (errFields []*validator.FormErrorField, err error) {
|
|
|
|
if checker.IsInvalidUsername(r.AdminName) {
|
|
|
|
errField := &validator.FormErrorField{
|
|
|
|
ErrorField: "name",
|
|
|
|
ErrorMsg: reason.UsernameInvalid,
|
|
|
|
}
|
|
|
|
errFields = append(errFields, errField)
|
|
|
|
return errFields, errors.BadRequest(reason.UsernameInvalid)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-10 11:00:26 +08:00
|
|
|
func (r *InitBaseInfoReq) FormatSiteUrl() {
|
|
|
|
parsedUrl, err := url.Parse(r.SiteURL)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
r.SiteURL = fmt.Sprintf("%s://%s", parsedUrl.Scheme, parsedUrl.Host)
|
|
|
|
}
|