Files
go-omni/cmd/go-omni/scaffold/apiInfrastructure.go

65 lines
3.0 KiB
Go

package scaffold
import (
"fmt"
"strings"
)
// APIInfrastructure renders shared infrastructure without inventing business
// tables or routes. The API generator applies these as developer-owned seeds.
func APIInfrastructure(module, database, storage, logOutput, logFormat string) (map[string][]byte, error) {
return apiInfrastructure(module, database, storage, logOutput, logFormat, false)
}
// LegacyAPIInfrastructure renders only the known v0.13 seed baseline for a safe upgrade.
func LegacyAPIInfrastructure(module, database, storage, logOutput, logFormat string) (map[string][]byte, error) {
return apiInfrastructure(module, database, storage, logOutput, logFormat, true)
}
func apiInfrastructure(module, database, storage, logOutput, logFormat string, legacy bool) (map[string][]byte, error) {
data := templateData{Module: module, Database: database, Storage: storage, LogOutput: logOutput, LogFormat: logFormat, API: true, Modules: true}
files := map[string]string{
"internal/svc/runtimeGen.go": "templates/api-runtime.go.tmpl",
"internal/svc/options.go": "templates/api-options.go.tmpl",
"internal/config/config.go": "templates/api-config.go.tmpl",
"internal/config/loadGen.go": "templates/api-load.go.tmpl",
"internal/config/loader.go": "templates/api-loader.go.tmpl",
"etc/config.yaml": "templates/modules-config.yaml.tmpl",
"internal/svc/serviceContext.go": "templates/api-context.go.tmpl",
}
if database == "mysql" {
files["internal/svc/databaseOptions.go"] = "templates/api-databaseOptions.go.tmpl"
}
if storage != "none" {
files["internal/svc/storage.go"] = "templates/modules-storage.go.tmpl"
}
if legacy {
delete(files, "internal/config/loadGen.go")
delete(files, "internal/config/loader.go")
delete(files, "internal/svc/runtimeGen.go")
delete(files, "internal/svc/databaseOptions.go")
files["internal/svc/close.go"] = "templates/api-close.go.tmpl"
files["internal/config/config.go"] = "templates/modules-settings.go.tmpl"
for name, source := range files {
files[name] = strings.Replace(source, "templates/", "templates/legacyV013/", 1)
}
}
result := make(map[string][]byte, len(files))
for name, source := range files {
content, err := render(source, data)
if err != nil {
return nil, fmt.Errorf("render %s: %w", name, err)
}
content = strings.ReplaceAll(content, "package settings", "package config")
content = strings.ReplaceAll(content, "package servicecontext", "package svc")
content = strings.ReplaceAll(content, "/internal/settings", "/internal/config")
content = strings.ReplaceAll(content, "settings.", "config.")
if strings.HasPrefix(name, "internal/svc/") {
content = strings.ReplaceAll(content, "*Context", "*ServiceContext")
content = strings.ReplaceAll(content, "&Context{", "&ServiceContext{")
content = strings.ReplaceAll(content, "type Context struct", "type ServiceContext struct")
}
result[name] = []byte(strings.TrimRight(content, "\n") + "\n")
}
return result, nil
}