chore: update port so it doesn't conflict with sso

This commit is contained in:
Hafizh A. Y
2025-09-30 16:48:05 +07:00
parent 94a6d41a61
commit dbc1f79a36
19 changed files with 668 additions and 37 deletions
+166 -5
View File
@@ -1,6 +1,7 @@
package main
import (
"errors"
"fmt"
"log"
"os"
@@ -15,6 +16,11 @@ type Data struct {
Entity string // last ("area")
}
const (
repoBase = "gitlab.com/mbugroup/lti-api.git"
modulesBase = repoBase + "/internal/modules"
)
func main() {
if len(os.Args) < 2 {
log.Fatal("usage: make gen feat=<feature> (ex: customer | master/area)")
@@ -139,6 +145,7 @@ func main() {
log.Println("Generated:", outFile)
}
updateParentModules(d)
updateMainRoute(d)
}
@@ -150,13 +157,22 @@ func updateMainRoute(d Data) {
return
}
// entity & path
modPath := filepath.Join(append(toCamelParts(d.Parts[:len(d.Parts)-1]), toCamelCase(d.Entity)+"s")...)
modName := toCamelCase(d.Entity) + "s"
pkgName := toPascalCase(d.Entity) + "Module"
var importPath string
var modName string
var pkgName string
if len(d.Parts) > 1 {
top := d.Parts[0]
importPath = toKebab(top)
modName = toCamelCase(top)
pkgName = toPascalCase(top) + "Module"
} else {
importPath = toKebab(d.Entity) + "s"
modName = toCamelCase(d.Entity) + "s"
pkgName = toPascalCase(d.Entity) + "Module"
}
// Inject import
importLine := fmt.Sprintf("\t%[1]s \"%s/internal/modules/%s\"", modName, "gitlab.com/mbugroup/lti-api.git", modPath)
importLine := fmt.Sprintf("\t%[1]s \"%s/%s\"", modName, modulesBase, importPath)
if !strings.Contains(string(content), importLine) {
content = []byte(strings.Replace(string(content),
"// MODULE IMPORTS",
@@ -179,6 +195,151 @@ func updateMainRoute(d Data) {
log.Println("Updated:", routeFile)
}
func updateParentModules(d Data) {
if len(d.Parts) < 2 {
return
}
fullLen := len(d.Parts)
for i := 1; i < len(d.Parts); i++ {
parentParts := d.Parts[:i]
ensureParentModuleFile(parentParts)
childParts := d.Parts[:i+1]
ensureParentRoute(parentParts, childParts, fullLen)
}
}
func ensureParentModuleFile(parentParts []string) {
dir := filepath.Join(append([]string{"internal", "modules"}, toKebabParts(parentParts)...)...)
if err := os.MkdirAll(dir, 0o755); err != nil {
log.Fatalf("make parent dir: %v", err)
}
moduleFile := filepath.Join(dir, "module.go")
if _, err := os.Stat(moduleFile); err == nil {
return
} else if !errors.Is(err, os.ErrNotExist) {
log.Fatalf("check parent module: %v", err)
}
pkgName := toPackageName(parentParts[len(parentParts)-1])
structName := toPascalCase(parentParts[len(parentParts)-1]) + "Module"
content := fmt.Sprintf(`package %s
import (
"github.com/go-playground/validator/v10"
"github.com/gofiber/fiber/v2"
"gorm.io/gorm"
)
type %s struct{}
func (%s) RegisterRoutes(router fiber.Router, db *gorm.DB, validate *validator.Validate) {
RegisterRoutes(router, db, validate)
}
`, pkgName, structName, structName)
if err := os.WriteFile(moduleFile, []byte(content), 0o644); err != nil {
log.Fatalf("write parent module: %v", err)
}
log.Println("Generated:", moduleFile)
}
func ensureParentRoute(parentParts, childParts []string, fullLen int) {
dir := filepath.Join(append([]string{"internal", "modules"}, toKebabParts(parentParts)...)...)
if err := os.MkdirAll(dir, 0o755); err != nil {
log.Fatalf("make parent dir: %v", err)
}
routeFile := filepath.Join(dir, "route.go")
isLeaf := len(childParts) == fullLen
childName := childParts[len(childParts)-1]
childAlias := toCamelCase(childName)
if isLeaf {
childAlias += "s"
}
childStruct := toPascalCase(childName) + "Module"
childImportParts := toKebabParts(childParts[:len(childParts)-1])
childLast := childParts[len(childParts)-1]
if isLeaf {
childImportParts = append(childImportParts, toKebab(childLast)+"s")
} else {
childImportParts = append(childImportParts, toKebab(childLast))
}
childImportPath := modulesBase + "/" + strings.Join(childImportParts, "/")
if _, err := os.Stat(routeFile); errors.Is(err, os.ErrNotExist) {
pkgName := toPackageName(parentParts[len(parentParts)-1])
segment := toKebab(parentParts[len(parentParts)-1])
content := fmt.Sprintf(`package %s
import (
"%s"
"github.com/go-playground/validator/v10"
"github.com/gofiber/fiber/v2"
"gorm.io/gorm"
%s "%s"
// MODULE IMPORTS
)
func RegisterRoutes(router fiber.Router, db *gorm.DB, validate *validator.Validate) {
group := router.Group("/%s")
allModules := []modules.Module{
%s.%s{},
// MODULE REGISTRY
}
for _, m := range allModules {
m.RegisterRoutes(group, db, validate)
}
}
`, pkgName, modulesBase, childAlias, childImportPath, segment, childAlias, childStruct)
if err := os.WriteFile(routeFile, []byte(content), 0o644); err != nil {
log.Fatalf("write parent route: %v", err)
}
log.Println("Generated:", routeFile)
return
} else if err != nil {
log.Fatalf("check parent route: %v", err)
}
content, err := os.ReadFile(routeFile)
if err != nil {
log.Fatalf("read parent route: %v", err)
}
importLine := fmt.Sprintf("\t%s \"%s\"", childAlias, childImportPath)
if !strings.Contains(string(content), importLine) {
content = []byte(strings.Replace(string(content),
"\t// MODULE IMPORTS",
importLine+"\n\t// MODULE IMPORTS",
1))
}
registryLine := fmt.Sprintf("\t\t%s.%s{},", childAlias, childStruct)
if !strings.Contains(string(content), registryLine) {
content = []byte(strings.Replace(string(content),
"\t\t// MODULE REGISTRY",
registryLine+"\n\t\t// MODULE REGISTRY",
1))
}
if err := os.WriteFile(routeFile, content, 0o644); err != nil {
log.Fatalf("write parent route: %v", err)
}
log.Println("Updated:", routeFile)
}
func toPackageName(s string) string {
return strings.ToLower(toPascalCase(s))
}
func toPascalCase(s string) string {
sep := func(r rune) bool { return r == '_' || r == '-' || r == ' ' || r == '/' }
parts := strings.FieldsFunc(s, sep)