framework1/framework/provider/app/service.go

125 lines
2.9 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package app
import (
"fmt"
"github.com/Superdanda/hade/framework"
"github.com/Superdanda/hade/framework/util"
"github.com/google/uuid"
"path/filepath"
)
type HadeApp struct {
container framework.Container
baseFolder string
appId string
configMap map[string]string // 配置加载
}
func NewHadeApp(params ...interface{}) (interface{}, error) {
if len(params) != 2 {
return nil, fmt.Errorf("HadeApp NewHadeApp expects 2 parameters")
}
// 有两个参数一个是容器一个是baseFolder
container := params[0].(framework.Container)
baseFolder := params[1].(string)
appId := uuid.New().String()
configMap := make(map[string]string)
return &HadeApp{container: container, baseFolder: baseFolder, appId: appId, configMap: configMap}, nil
}
func (h HadeApp) BaseFolder() string {
if h.baseFolder != "" {
return h.baseFolder
}
//var baseFolder string
//flag.StringVar(&baseFolder, "base_folder", "", "base_folder 参数, 默认为当前路径")
//flag.Parse()
//if baseFolder != "" {
// return baseFolder
//}
return util.GetExecDirectory()
}
func (h HadeApp) Version() string {
return "0.0.1"
}
func (h HadeApp) StorageFolder() string {
return filepath.Join(h.BaseFolder(), "storage")
}
func (h HadeApp) ConfigFolder() string {
return filepath.Join(h.BaseFolder(), "config")
}
func (h HadeApp) LogFolder() string {
if val, ok := h.configMap["log_folder"]; ok {
return val
}
return filepath.Join(h.StorageFolder(), "log")
}
func (h HadeApp) ProviderFolder() string {
return filepath.Join(h.AppFolder(), "provider")
}
func (h HadeApp) MiddlewareFolder() string {
return filepath.Join(h.HttpFolder(), "middleware")
}
func (h HadeApp) CommandFolder() string {
return filepath.Join(h.ConsoleFolder(), "command")
}
func (h HadeApp) RuntimeFolder() string {
runtimeFolder := filepath.Join(h.StorageFolder(), "runtime")
util.EnsureDir(runtimeFolder)
return runtimeFolder
}
func (h HadeApp) TestFolder() string {
return filepath.Join(h.BaseFolder(), "test")
}
func (h HadeApp) HttpFolder() string {
return filepath.Join(h.AppFolder(), "http")
}
func (h HadeApp) ConsoleFolder() string {
return filepath.Join(h.AppFolder(), "console")
}
func (h HadeApp) AppFolder() string {
return filepath.Join(h.BaseFolder(), "app")
}
func (h HadeApp) DeployFolder() string {
deployFolder := filepath.Join(h.BaseFolder(), "deploy")
util.EnsureDir(deployFolder)
return deployFolder
}
func (h HadeApp) HttpModuleFolder() string {
deployFolder := filepath.Join(h.HttpFolder(), "module")
return deployFolder
}
func (h HadeApp) InfrastructureFolder() string {
return filepath.Join(h.AppFolder(), "infrastructure")
}
func (h HadeApp) TemplateFolder() string {
return filepath.Join(h.BaseFolder(), "framework", "template")
}
func (h HadeApp) AppId() string {
return h.appId
}
func (h HadeApp) LoadAppConfig(mapString map[string]string) {
for key, val := range mapString {
h.configMap[key] = val
}
}