57 lines
1.8 KiB
Go
57 lines
1.8 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
|
|
"kra/internal/biz"
|
|
"kra/internal/service/dto"
|
|
)
|
|
|
|
type DatabaseInit struct {
|
|
DBType string `json:"dbType"`
|
|
Host string `json:"host"`
|
|
Port string `json:"port"`
|
|
UserName string `json:"userName"`
|
|
Password string `json:"password"`
|
|
DBName string `json:"dbName" binding:"required"`
|
|
DBPath string `json:"dbPath"`
|
|
Template string `json:"template"`
|
|
AdminPassword string `json:"adminPassword" binding:"required"`
|
|
}
|
|
|
|
func (s *SystemConfigService) Initialize(ctx context.Context, input *DatabaseInit, apis []*biz.API) error {
|
|
config := ""
|
|
switch input.DBType {
|
|
case "mysql":
|
|
config = "timeout=5s&parseTime=True&loc=Local&charset=utf8mb4"
|
|
case "pgsql":
|
|
config = "sslmode=disable TimeZone=Local"
|
|
}
|
|
return s.uc.Initialize(ctx, &biz.DatabaseConfig{Driver: input.DBType, Host: input.Host, Port: input.Port, User: input.UserName, Password: input.Password, Name: input.DBName, Path: input.DBPath, Config: config, Template: input.Template, AdminPassword: input.AdminPassword, APIs: apis})
|
|
}
|
|
|
|
func (s *SystemConfigService) InitializeRoutes(ctx context.Context, input *DatabaseInit, routes []dto.Route) error {
|
|
apis := make([]*biz.API, 0, len(routes))
|
|
for _, route := range routes {
|
|
path := route.Path
|
|
if routerPrefix := s.settings.RouterPrefix(); routerPrefix != "" {
|
|
path = strings.TrimPrefix(path, strings.TrimSuffix(routerPrefix, "/"))
|
|
if path == "" {
|
|
path = "/"
|
|
}
|
|
}
|
|
group, description := routeMetadata(route.Method, path)
|
|
apis = append(apis, &biz.API{Path: path, Method: route.Method, APIGroup: group, Description: description})
|
|
}
|
|
return s.Initialize(ctx, input, apis)
|
|
}
|
|
|
|
func routeGroup(path string) string {
|
|
parts := strings.Split(strings.Trim(path, "/"), "/")
|
|
if len(parts) > 0 && parts[0] != "" {
|
|
return parts[0]
|
|
}
|
|
return "base"
|
|
}
|