mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 05:21:57 +00:00
chore: update port so it doesn't conflict with sso
This commit is contained in:
+166
-5
@@ -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)
|
||||
|
||||
@@ -5,9 +5,13 @@ import (
|
||||
)
|
||||
|
||||
type {{Pascal .Entity}} struct {
|
||||
Id uint `gorm:"primaryKey"`
|
||||
Name string `gorm:"not null"`
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
Id uint `gorm:"primaryKey"`
|
||||
Name string `gorm:"not null"`
|
||||
CreatedBy int64 `gorm:"not null"`
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
|
||||
|
||||
CreatedUser model.User `gorm:"foreignKey:CreatedBy;references:Id"`
|
||||
}
|
||||
{{end}}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{{define "route"}}package {{Kebab .Entity}}s
|
||||
|
||||
import (
|
||||
m "gitlab.com/mbugroup/lti-api.git/internal/middleware"
|
||||
// m "gitlab.com/mbugroup/lti-api.git/internal/middleware"
|
||||
controller "gitlab.com/mbugroup/lti-api.git/internal/modules/{{Kebab .FeatName}}s/controllers"
|
||||
{{Camel .Entity}} "gitlab.com/mbugroup/lti-api.git/internal/modules/{{Kebab .FeatName}}s/services"
|
||||
user "gitlab.com/mbugroup/lti-api.git/internal/modules/users/services"
|
||||
@@ -14,10 +14,16 @@ func {{Pascal .Entity}}Routes(v1 fiber.Router, u user.UserService, s {{Camel .En
|
||||
|
||||
route := v1.Group("/{{Kebab .Entity}}s")
|
||||
|
||||
route.Get("/", m.Auth(u), ctrl.GetAll)
|
||||
route.Post("/", m.Auth(u), ctrl.CreateOne)
|
||||
route.Get("/:id", m.Auth(u), ctrl.GetOne)
|
||||
route.Patch("/:id", m.Auth(u), ctrl.UpdateOne)
|
||||
route.Delete("/:id", m.Auth(u), ctrl.DeleteOne)
|
||||
// route.Get("/", m.Auth(u), ctrl.GetAll)
|
||||
// route.Post("/", m.Auth(u), ctrl.CreateOne)
|
||||
// route.Get("/:id", m.Auth(u), ctrl.GetOne)
|
||||
// route.Patch("/:id", m.Auth(u), ctrl.UpdateOne)
|
||||
// route.Delete("/:id", m.Auth(u), ctrl.DeleteOne)
|
||||
|
||||
route.Get("/", ctrl.GetAll)
|
||||
route.Post("/", ctrl.CreateOne)
|
||||
route.Get("/:id", ctrl.GetOne)
|
||||
route.Patch("/:id", ctrl.UpdateOne)
|
||||
route.Delete("/:id", ctrl.DeleteOne)
|
||||
}
|
||||
{{end}}
|
||||
|
||||
Reference in New Issue
Block a user