-
-
Notifications
You must be signed in to change notification settings - Fork 201
/
setup.go
48 lines (43 loc) · 1.23 KB
/
setup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package request
import (
"strings"
"github.com/admpub/nging/v5/application/library/config"
"github.com/admpub/nging/v5/application/library/config/subconfig/sdb"
"github.com/webx-top/com"
"github.com/webx-top/echo"
"github.com/webx-top/echo/code"
)
var _ echo.AfterValidate = &Setup{}
type Setup struct {
Type string `validate:"required"`
User string
Password string
Host string
Database string `validate:"required"`
Charset string `validate:"omitempty,alphanum"`
AdminUser string `validate:"username"`
AdminPass string `validate:"required,min=8,max=64"`
AdminEmail string `validate:"required,email"`
}
func (s *Setup) AfterValidate(c echo.Context) error {
if _, ok := config.DBInstallers[s.Type]; !ok {
return c.NewError(code.InvalidParameter, `不支持的数据库类型: %v`, s.Type).SetZone(`type`)
}
if s.Type == `sqlite` {
s.User = ``
s.Password = ``
s.Host = ``
if !strings.HasSuffix(s.Database, `.db`) {
s.Database += `.db`
}
} else {
if len(s.Charset) == 0 {
s.Charset = sdb.MySQLDefaultCharset
} else {
if !com.InSlice(s.Charset, sdb.MySQLSupportCharsetList) {
return c.NewError(code.InvalidParameter, `字符集参数无效`).SetZone(`charset`)
}
}
}
return nil
}