update perubahan
This commit is contained in:
@@ -117,7 +117,7 @@ func RegisterRoutes(cfg *config.Config) *gin.Engine {
|
|||||||
Logger: *logger.Default(),
|
Logger: *logger.Default(),
|
||||||
Validator: validator.New(),
|
Validator: validator.New(),
|
||||||
})
|
})
|
||||||
pesertaGroup := v1.Group("/Peserta")
|
pesertaGroup := v1.Group("/peserta")
|
||||||
pesertaGroup.GET("/nokartu/:nokartu", pesertaHandler.GetBynokartu)
|
pesertaGroup.GET("/nokartu/:nokartu", pesertaHandler.GetBynokartu)
|
||||||
pesertaGroup.GET("/nik/:nik", pesertaHandler.GetBynik)
|
pesertaGroup.GET("/nik/:nik", pesertaHandler.GetBynik)
|
||||||
|
|
||||||
|
|||||||
@@ -3470,125 +3470,125 @@ func generateRoutes(serviceName string, svc Service, gc GlobalConfig) error {
|
|||||||
|
|
||||||
routesContentStr := string(routesContent)
|
routesContentStr := string(routesContent)
|
||||||
|
|
||||||
// Check if routes are already registered
|
var newImports []string
|
||||||
if strings.Contains(routesContentStr, fmt.Sprintf("Register%sRoutes", svc.Name)) {
|
|
||||||
fmt.Printf("⚠️ Routes for %s already registered in main routes file\n", svc.Name)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
var imports []string
|
|
||||||
var allRoutes []string
|
var allRoutes []string
|
||||||
|
|
||||||
// ✅ PERBAIKAN: Gunakan groupName dengan benar
|
// Track processed folders to avoid duplicate imports
|
||||||
// ✅ VALIDASI: Track handler folder yang sudah diimport
|
|
||||||
processedFolders := make(map[string]bool)
|
processedFolders := make(map[string]bool)
|
||||||
|
|
||||||
for groupName, grp := range svc.Endpoints {
|
for groupName, grp := range svc.Endpoints {
|
||||||
// Import berdasarkan handler folder
|
// Check and add import if not exists
|
||||||
if !processedFolders[grp.HandlerFolder] {
|
importLine := fmt.Sprintf("%sHandlers \"%s/internal/handlers/%s\"",
|
||||||
importLine := fmt.Sprintf("\t%sHandlers \"%s/internal/handlers/%s\"",
|
grp.HandlerFolder, gc.ModuleName, grp.HandlerFolder)
|
||||||
grp.HandlerFolder, gc.ModuleName, grp.HandlerFolder)
|
if !processedFolders[grp.HandlerFolder] && !strings.Contains(routesContentStr, importLine) {
|
||||||
imports = append(imports, importLine)
|
newImports = append(newImports, fmt.Sprintf("\t%s", importLine))
|
||||||
processedFolders[grp.HandlerFolder] = true
|
processedFolders[grp.HandlerFolder] = true
|
||||||
fmt.Printf("✅ Added import: %sHandlers\n", grp.HandlerFolder)
|
fmt.Printf("✅ Will add import: %sHandlers\n", grp.HandlerFolder)
|
||||||
} else {
|
} else if processedFolders[grp.HandlerFolder] {
|
||||||
fmt.Printf("⚠️ Skipped duplicate import for folder: %s\n", grp.HandlerFolder)
|
fmt.Printf("⚠️ Skipped duplicate import for folder: %s\n", grp.HandlerFolder)
|
||||||
|
} else {
|
||||||
|
fmt.Printf("⚠️ Import already exists: %sHandlers\n", grp.HandlerFolder)
|
||||||
}
|
}
|
||||||
|
|
||||||
var routesCode strings.Builder
|
var routesCode strings.Builder
|
||||||
|
|
||||||
// Gunakan groupName untuk comment dan identifier
|
// Comment for the group
|
||||||
routesCode.WriteString(fmt.Sprintf("\n\t// %s (%s) routes\n", grp.Description, groupName))
|
routesCode.WriteString(fmt.Sprintf("\n\t// %s (%s) routes\n", grp.Description, groupName))
|
||||||
|
|
||||||
// Handler instantiation menggunakan HandlerName dari config
|
// Check if handler is already instantiated
|
||||||
routesCode.WriteString(fmt.Sprintf("\t%sHandler := %sHandlers.New%sHandler(%sHandlers.%sHandlerConfig{\n",
|
handlerVar := strings.ToLower(grp.HandlerName) + "Handler"
|
||||||
strings.ToLower(grp.HandlerName),
|
handlerInstantiation := fmt.Sprintf("%s := %sHandlers.New%sHandler(%sHandlers.%sHandlerConfig{",
|
||||||
grp.HandlerFolder,
|
handlerVar, grp.HandlerFolder, grp.HandlerName, grp.HandlerFolder, grp.HandlerName)
|
||||||
grp.HandlerName,
|
if !strings.Contains(routesContentStr, handlerInstantiation) {
|
||||||
grp.HandlerFolder,
|
routesCode.WriteString(fmt.Sprintf("\t%s := %sHandlers.New%sHandler(%sHandlers.%sHandlerConfig{\n",
|
||||||
grp.HandlerName))
|
handlerVar, grp.HandlerFolder, grp.HandlerName, grp.HandlerFolder, grp.HandlerName))
|
||||||
|
routesCode.WriteString("\t\tConfig: cfg,\n")
|
||||||
|
routesCode.WriteString("\t\tLogger: *logger.Default(),\n")
|
||||||
|
routesCode.WriteString("\t\tValidator: validator.New(),\n")
|
||||||
|
routesCode.WriteString("\t})\n")
|
||||||
|
fmt.Printf("✅ Will add handler instantiation: %s\n", handlerVar)
|
||||||
|
} else {
|
||||||
|
fmt.Printf("⚠️ Handler already instantiated: %s\n", handlerVar)
|
||||||
|
}
|
||||||
|
|
||||||
routesCode.WriteString("\t\tConfig: cfg,\n")
|
// Check if group is already created
|
||||||
routesCode.WriteString("\t\tLogger: *logger.Default(),\n")
|
groupVar := strings.ToLower(grp.HandlerName) + "Group"
|
||||||
routesCode.WriteString("\t\tValidator: validator.New(),\n")
|
groupCreation := fmt.Sprintf("%s := v1.Group(\"/%s\")", groupVar, groupName)
|
||||||
routesCode.WriteString("\t})\n")
|
if !strings.Contains(routesContentStr, groupCreation) {
|
||||||
|
routesCode.WriteString(fmt.Sprintf("\t%s := v1.Group(\"/%s\")\n", groupVar, groupName))
|
||||||
// ✅ GUNAKAN groupName untuk route group path
|
fmt.Printf("✅ Will add group: %s\n", groupVar)
|
||||||
routesCode.WriteString(fmt.Sprintf("\t%sGroup := v1.Group(\"/%s\")\n",
|
} else {
|
||||||
strings.ToLower(grp.HandlerName), groupName)) // ← Gunakan groupName di sini
|
fmt.Printf("⚠️ Group already exists: %s\n", groupVar)
|
||||||
|
}
|
||||||
|
|
||||||
// Process functions
|
// Process functions
|
||||||
for fname, fcfg := range grp.Functions {
|
for fname, fcfg := range grp.Functions {
|
||||||
td := processFunctionData(svc, grp, fname, fcfg, gc)
|
td := processFunctionData(svc, grp, fname, fcfg, gc)
|
||||||
|
|
||||||
for _, endpoint := range td.Endpoints {
|
for _, endpoint := range td.Endpoints {
|
||||||
handlerVar := strings.ToLower(grp.HandlerName) + "Handler"
|
// Loop through methods and use specific routes
|
||||||
groupVar := strings.ToLower(grp.HandlerName) + "Group"
|
|
||||||
|
|
||||||
// ✅ MODIFIKASI: Loop through methods dan gunakan specific routes
|
|
||||||
for _, method := range fcfg.Methods {
|
for _, method := range fcfg.Methods {
|
||||||
var cleanPath string
|
var cleanPath string
|
||||||
|
|
||||||
// ✅ Pilih path berdasarkan method
|
// Select path based on method
|
||||||
switch strings.ToUpper(method) {
|
switch strings.ToUpper(method) {
|
||||||
case "GET":
|
case "GET":
|
||||||
cleanPath = fcfg.GetRoutes
|
cleanPath = fcfg.GetRoutes
|
||||||
if cleanPath == "" {
|
if cleanPath == "" {
|
||||||
cleanPath = fcfg.GetPath // fallback ke get_path
|
cleanPath = fcfg.GetPath
|
||||||
}
|
}
|
||||||
case "POST":
|
case "POST":
|
||||||
cleanPath = fcfg.PostRoutes
|
cleanPath = fcfg.PostRoutes
|
||||||
if cleanPath == "" {
|
if cleanPath == "" {
|
||||||
cleanPath = fcfg.PostPath // fallback ke post_path
|
cleanPath = fcfg.PostPath
|
||||||
}
|
}
|
||||||
case "PUT":
|
case "PUT":
|
||||||
cleanPath = fcfg.PutRoutes
|
cleanPath = fcfg.PutRoutes
|
||||||
if cleanPath == "" {
|
if cleanPath == "" {
|
||||||
cleanPath = fcfg.PutPath // fallback ke put_path
|
cleanPath = fcfg.PutPath
|
||||||
}
|
}
|
||||||
case "DELETE":
|
case "DELETE":
|
||||||
cleanPath = fcfg.DeleteRoutes
|
cleanPath = fcfg.DeleteRoutes
|
||||||
if cleanPath == "" {
|
if cleanPath == "" {
|
||||||
cleanPath = fcfg.DeletePath // fallback ke delete_path
|
cleanPath = fcfg.DeletePath
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
fmt.Printf("⚠️ Unsupported HTTP method: %s for function %s\n", method, fname)
|
fmt.Printf("⚠️ Unsupported HTTP method: %s for function %s\n", method, fname)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// ✅ Final fallback ke path jika specific route kosong
|
// Final fallback to path if specific route empty
|
||||||
if cleanPath == "" {
|
if cleanPath == "" {
|
||||||
cleanPath = fcfg.Path
|
cleanPath = fcfg.Path
|
||||||
}
|
}
|
||||||
|
|
||||||
// ✅ Bersihkan path - hapus prefix groupName jika ada
|
// Clean path - remove prefix groupName if present and ensure lowercase
|
||||||
if strings.HasPrefix(cleanPath, "/"+groupName) {
|
if strings.HasPrefix(cleanPath, "/"+groupName) {
|
||||||
cleanPath = strings.TrimPrefix(cleanPath, "/"+groupName)
|
cleanPath = strings.TrimPrefix(cleanPath, "/"+groupName)
|
||||||
}
|
}
|
||||||
if cleanPath == "" {
|
if cleanPath == "" {
|
||||||
cleanPath = "/"
|
cleanPath = "/"
|
||||||
}
|
}
|
||||||
|
// Ensure path is lowercase for consistency
|
||||||
|
cleanPath = strings.ToLower(cleanPath)
|
||||||
|
|
||||||
// ✅ Generate route berdasarkan method
|
// Generate route based on method
|
||||||
var routeLine string
|
var routeLine string
|
||||||
switch strings.ToUpper(method) {
|
switch strings.ToUpper(method) {
|
||||||
case "GET":
|
case "GET":
|
||||||
routeLine = fmt.Sprintf("%s.GET(\"%s\", %s.Get%s)\n",
|
routeLine = fmt.Sprintf("\t%s.GET(\"%s\", %s.Get%s)", groupVar, cleanPath, handlerVar, endpoint.Name)
|
||||||
groupVar, cleanPath, handlerVar, endpoint.Name)
|
|
||||||
case "POST":
|
case "POST":
|
||||||
routeLine = fmt.Sprintf("%s.POST(\"%s\", %s.Create%s)\n",
|
routeLine = fmt.Sprintf("\t%s.POST(\"%s\", %s.Create%s)", groupVar, cleanPath, handlerVar, endpoint.Name)
|
||||||
groupVar, cleanPath, handlerVar, endpoint.Name)
|
|
||||||
case "PUT":
|
case "PUT":
|
||||||
routeLine = fmt.Sprintf("%s.PUT(\"%s\", %s.Update%s)\n",
|
routeLine = fmt.Sprintf("\t%s.PUT(\"%s\", %s.Update%s)", groupVar, cleanPath, handlerVar, endpoint.Name)
|
||||||
groupVar, cleanPath, handlerVar, endpoint.Name)
|
|
||||||
case "DELETE":
|
case "DELETE":
|
||||||
routeLine = fmt.Sprintf("%s.DELETE(\"%s\", %s.Delete%s)\n",
|
routeLine = fmt.Sprintf("\t%s.DELETE(\"%s\", %s.Delete%s)", groupVar, cleanPath, handlerVar, endpoint.Name)
|
||||||
groupVar, cleanPath, handlerVar, endpoint.Name)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ✅ PERBAIKAN: Check if route already exists
|
// Check if route already exists
|
||||||
if !strings.Contains(routesContentStr, strings.TrimSpace(routeLine)) {
|
if !strings.Contains(routesContentStr, routeLine) {
|
||||||
routesCode.WriteString(routeLine)
|
routesCode.WriteString(routeLine + "\n")
|
||||||
fmt.Printf("✅ Added route: %s %s\n", method, cleanPath)
|
fmt.Printf("✅ Will add route: %s %s\n", method, cleanPath)
|
||||||
} else {
|
} else {
|
||||||
fmt.Printf("⚠️ Skipped duplicate route: %s %s\n", method, cleanPath)
|
fmt.Printf("⚠️ Skipped duplicate route: %s %s\n", method, cleanPath)
|
||||||
}
|
}
|
||||||
@@ -3596,81 +3596,55 @@ func generateRoutes(serviceName string, svc Service, gc GlobalConfig) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
allRoutes = append(allRoutes, routesCode.String())
|
if routesCode.Len() > 0 {
|
||||||
|
allRoutes = append(allRoutes, routesCode.String())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ✅ PERBAIKAN: Insert imports setelah "api-service/internal/database"
|
// Insert new imports if any
|
||||||
if len(imports) > 0 {
|
if len(newImports) > 0 {
|
||||||
// ✅ PERBAIKAN: Hilangkan newline di awal, langsung import lines saja
|
importSection := strings.Join(newImports, "\n") + "\n"
|
||||||
importSection := strings.Join(imports, "\n") + "\n"
|
|
||||||
|
|
||||||
// ✅ PERBAIKAN: Cari posisi setelah "api-service/internal/database"
|
// Find the database import marker
|
||||||
databaseImportMarker := fmt.Sprintf("\"%s/internal/database\"", gc.ModuleName)
|
databaseImportMarker := fmt.Sprintf("\"%s/internal/database\"", gc.ModuleName)
|
||||||
if strings.Contains(routesContentStr, databaseImportMarker) {
|
if idx := strings.Index(routesContentStr, databaseImportMarker); idx != -1 {
|
||||||
// Temukan posisi marker
|
// Find end of that line
|
||||||
markerPos := strings.Index(routesContentStr, databaseImportMarker)
|
endOfLine := strings.Index(routesContentStr[idx:], "\n") + idx + 1
|
||||||
// Temukan akhir baris dari marker
|
routesContentStr = routesContentStr[:endOfLine] + importSection + routesContentStr[endOfLine:]
|
||||||
endOfLinePos := strings.Index(routesContentStr[markerPos:], "\n") + markerPos
|
fmt.Printf("✅ Inserted new imports after database import\n")
|
||||||
// Insert import section setelah baris marker
|
|
||||||
routesContentStr = routesContentStr[:endOfLinePos+1] + importSection + routesContentStr[endOfLinePos+1:]
|
|
||||||
fmt.Printf("✅ Inserted imports after database import\n")
|
|
||||||
} else {
|
} else {
|
||||||
// Fallback: Insert setelah "import (" jika marker tidak ditemukan
|
// Fallback to after import (
|
||||||
importMarker := "import ("
|
if idx := strings.Index(routesContentStr, "import ("); idx != -1 {
|
||||||
if strings.Contains(routesContentStr, importMarker) {
|
importStart := idx + len("import (")
|
||||||
importIndex := strings.Index(routesContentStr, importMarker) + len(importMarker)
|
routesContentStr = routesContentStr[:importStart] + "\n" + importSection + routesContentStr[importStart:]
|
||||||
routesContentStr = routesContentStr[:importIndex] + "\n" + importSection + routesContentStr[importIndex:]
|
|
||||||
fmt.Printf("⚠️ Database import not found, inserted after import (\n")
|
fmt.Printf("⚠️ Database import not found, inserted after import (\n")
|
||||||
} else {
|
|
||||||
fmt.Printf("⚠️ Could not find import section to insert imports\n")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ✅ PERBAIKAN: Try new format first, then fallback to old format
|
// Find insertion point for routes
|
||||||
var publishedRoutesMarker string
|
|
||||||
var insertionPoint int
|
var insertionPoint int
|
||||||
|
|
||||||
// Try the new multi-line format first
|
|
||||||
newFormatMarker := "// =============================================================================\n// PUBLISHED ROUTES\n// ============================================================================="
|
newFormatMarker := "// =============================================================================\n// PUBLISHED ROUTES\n// ============================================================================="
|
||||||
if strings.Contains(routesContentStr, newFormatMarker) {
|
if idx := strings.Index(routesContentStr, newFormatMarker); idx != -1 {
|
||||||
publishedRoutesMarker = newFormatMarker
|
insertionPoint = idx + len(newFormatMarker)
|
||||||
fmt.Printf("✅ Found new format PUBLISHED ROUTES marker\n")
|
fmt.Printf("✅ Found new format PUBLISHED ROUTES marker\n")
|
||||||
|
} else if idx := strings.Index(routesContentStr, "// PUBLISHED ROUTES"); idx != -1 {
|
||||||
|
insertionPoint = idx + len("// PUBLISHED ROUTES")
|
||||||
|
fmt.Printf("⚠️ Found old format PUBLISHED ROUTES marker\n")
|
||||||
} else {
|
} else {
|
||||||
// Fallback to old single-line format
|
return fmt.Errorf("PUBLISHED ROUTES marker not found in routes.go")
|
||||||
oldFormatMarker := "// PUBLISHED ROUTES"
|
|
||||||
if strings.Contains(routesContentStr, oldFormatMarker) {
|
|
||||||
publishedRoutesMarker = oldFormatMarker
|
|
||||||
fmt.Printf("⚠️ Found old format PUBLISHED ROUTES marker, using fallback\n")
|
|
||||||
} else {
|
|
||||||
return fmt.Errorf("PUBLISHED ROUTES marker not found in routes.go (tried both new and old formats)")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
insertionPoint = strings.Index(routesContentStr, publishedRoutesMarker) + len(publishedRoutesMarker)
|
// Insert new routes
|
||||||
|
if len(allRoutes) > 0 {
|
||||||
// If we want to upgrade to the new format, we can replace the old marker with the new one
|
newRoutesContent := routesContentStr[:insertionPoint] + "\n" + strings.Join(allRoutes, "\n") + routesContentStr[insertionPoint:]
|
||||||
if publishedRoutesMarker == "// PUBLISHED ROUTES" {
|
|
||||||
// Replace old marker with new format
|
|
||||||
newRoutesContent := routesContentStr[:insertionPoint] + "\n" + strings.Join(allRoutes, "\n") + "\n" + routesContentStr[insertionPoint:]
|
|
||||||
|
|
||||||
// Now replace the old marker with the new format
|
|
||||||
newRoutesContent = strings.Replace(newRoutesContent, "// PUBLISHED ROUTES", newFormatMarker, 1)
|
|
||||||
|
|
||||||
err = ioutil.WriteFile(routesFilePath, []byte(newRoutesContent), 0644)
|
err = ioutil.WriteFile(routesFilePath, []byte(newRoutesContent), 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to write updated routes file: %w", err)
|
return fmt.Errorf("failed to write updated routes file: %w", err)
|
||||||
}
|
}
|
||||||
fmt.Printf("✅ Updated main routes file with %s routes and upgraded marker format\n", svc.Name)
|
fmt.Printf("✅ Updated main routes file with new routes for %s\n", svc.Name)
|
||||||
} else {
|
} else {
|
||||||
// Use existing new format
|
fmt.Printf("⏭️ No new routes to add for %s\n", svc.Name)
|
||||||
newRoutesContent := routesContentStr[:insertionPoint] + "\n" + strings.Join(allRoutes, "\n") + "\n" + routesContentStr[insertionPoint:]
|
|
||||||
|
|
||||||
err = ioutil.WriteFile(routesFilePath, []byte(newRoutesContent), 0644)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to write updated routes file: %w", err)
|
|
||||||
}
|
|
||||||
fmt.Printf("✅ Updated main routes file with %s routes\n", svc.Name)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
Reference in New Issue
Block a user