Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
build/

*.log

logs/
21 changes: 18 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,24 @@ export GOTOOLCHAIN=auto
MKDOCDIR = mkdir -p
SBOM_DIR = build

.PHONY: all clean check docs linux_amd64 linux_arm64 darwin_amd64 darwin_arm64 windows_amd64 windows_arm64 aix_ppc64
STAGING_DIR = build_staging

all: clean linux_amd64 linux_arm64 darwin_amd64 darwin_arm64 windows_amd64 windows_arm64 aix_ppc64
.PHONY: all build clean check docs release linux_amd64 linux_arm64 darwin_amd64 darwin_arm64 windows_amd64 windows_arm64 aix_ppc64

all: build

build: clean
@echo "Starting build process in staging directory: $(STAGING_DIR)..."
@rm -rf $(STAGING_DIR)
@mkdir -p $(STAGING_DIR)
@echo "Performing Go build (CGO_ENABLED=0)..."
CGO_ENABLED=0 go build -ldflags="$(LDFLAGS)" -o $(STAGING_DIR)/$(APP_NAME) $(CMD_PATH)
@if [ -f config.yml ]; then cp config.yml $(STAGING_DIR)/; fi
@echo "Build successful. Finalizing build directory..."; \
rm -rf $(BASE_DIR); \
mv $(STAGING_DIR) $(BASE_DIR)

release: clean linux_amd64 linux_arm64 darwin_amd64 darwin_arm64 windows_amd64 windows_arm64 aix_ppc64

linux_amd64:
@echo "Building for linux/amd64..."
Expand Down Expand Up @@ -79,7 +94,7 @@ aix_ppc64:
@rm -rf build_staging_$@

clean:
rm -rf $(BASE_DIR) build_staging_*
rm -rf $(BASE_DIR) $(STAGING_DIR) build_staging_*

check:
golangci-lint run
Expand Down
36 changes: 5 additions & 31 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,41 +51,15 @@ server:
routes:

# -----------------------------------------------------
# Example 1: Standard Echo Endpoint (Explicitly defined)
# Example: Dynamic Route with Multiple Methods and Custom Response
# -----------------------------------------------------
- path: "/api/echo"
method: "POST,PUT,PATCH"
# No custom body/status defined, so it will echo the request payload back

# -----------------------------------------------------
# Example 2: Forced Error Response
# -----------------------------------------------------
- path: "/api/error"
method: "GET,POST"
status_code: 500
response_headers:
"X-Error-Code": "ERR-999"
response_body: '{"error": "Internal Server Error Simulation"}'

# -----------------------------------------------------
# Example 3: Simulated Latency (e.g., for Timeout Testing)
# -----------------------------------------------------
- path: "/api/slow"
method: "GET"
- path: "/api/mock"
method: "GET,POST,PUT"
status_code: 200
response_body: "This response was delayed by 500ms"
delay_ms: 500

# -----------------------------------------------------
# Example 4: Custom Headers and JSON Response
# -----------------------------------------------------
- path: "/api/auth/login"
method: "POST"
status_code: 200
response_headers:
"Content-Type": "application/json"
"Authorization": "Bearer mock-token-12345"
response_body: '{"status": "success", "userId": "mock_user"}'
"X-Mock-Status": "Active"
response_body: '{"message": "This is a dynamic mock response delayed by 500ms"}'

# -----------------------------------------------------
# VJM Internal Test Routes (Legacy/Compatibility)
Expand Down
50 changes: 41 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,21 @@ func main() {
}
defer logger.Sync()

var startupReqID string
b := make([]byte, 16)
if _, err := rand.Read(b); err == nil {
b[6] = (b[6] & 0x0f) | 0x40
b[8] = (b[8] & 0x3f) | 0x80
startupReqID = fmt.Sprintf("%08x-%04x-%04x-%04x-%012x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:])
} else {
startupReqID = "startup"
}

if logger.S != nil {
logger.S.Infof("Loaded configuration: port=%d, routes=%d", config.Server.Port, len(config.Server.Routes))
logger.WithReqID(startupReqID).Infof("\x1b[38;5;45m▶ HTTP Server\x1b[0m")
logger.WithReqID(startupReqID).Infof("Loaded configuration: port=%d, routes=%d", config.Server.Port, len(config.Server.Routes))
} else {
fmt.Println("\x1b[38;5;45m▶ HTTP Server\x1b[0m")
}

// Create Echo instance
Expand Down Expand Up @@ -104,9 +117,9 @@ func main() {
for _, r := range config.Server.Routes {
route := r // localized copy for closure
if logger.S != nil {
logger.S.Infof("Registering route: [%s] %s (ResponseHeaders: %v)", route.Method, route.Path, route.ResponseHeaders)
logger.WithReqID(startupReqID).Infof("Registering route: [%s] %s", route.Method, route.Path)
} else {
fmt.Printf("Registering route: [%s] %s (ResponseHeaders: %v)\n", route.Method, route.Path, route.ResponseHeaders)
fmt.Printf("Registering route: [%s] %s\n", route.Method, route.Path)
}

methods := strings.Split(route.Method, ",")
Expand Down Expand Up @@ -138,11 +151,23 @@ func main() {
}

// Always provide a catch-all fallback for undefined routes
if logger.S != nil {
logger.WithReqID(startupReqID).Infof("Registering wildcard route: \x1b[93m[ANY] /* (Catch-all)\x1b[0m")
} else {
fmt.Println("Registering wildcard route: \x1b[93m[ANY] /* (Catch-all)\x1b[0m")
}
e.Any("/*", func(c echo.Context) error {
return handleAny(c, config.Server.TransactionIDHeader, RouteConfig{})
})

// WebSocket Echo Route
if logger.S != nil {
logger.WithReqID(startupReqID).Infof("")
logger.WithReqID(startupReqID).Infof("\x1b[38;5;45m▶ WebSocket Server\x1b[0m")
} else {
fmt.Println()
fmt.Println("\x1b[38;5;45m▶ WebSocket Server\x1b[0m")
}
wsPaths := config.Server.Websocket.Paths
wsHandler := echo.WrapHandler(websocket.Handler(func(ws *websocket.Conn) {
_, _ = io.Copy(ws, ws)
Expand All @@ -157,14 +182,16 @@ func main() {
wsEcho := echo.New()
wsEcho.HideBanner = true
wsEcho.HidePort = true
if logger.S != nil {
logger.WithReqID(startupReqID).Infof("Loaded configuration: port=%d, routes=%d", wsPort, len(wsPaths))
}
for _, wsPath := range wsPaths {
if logger.S != nil {
logger.WithReqID(startupReqID).Infof("Registering route: [%s]", wsPath)
}
wsEcho.GET(wsPath, wsHandler)
}
go func() {
if logger.S != nil {
logger.S.Infof("Starting WebSocket server on port: %d, paths: %v", wsPort, wsPaths)
}

// Output directly with the exact same format and color (ANSI Green) as the Echo framework
fmt.Printf("⇨ websocket server started on \x1b[32m[::]:%d\x1b[0m\n", wsPort)

Expand All @@ -173,11 +200,16 @@ func main() {
} else {
// Run WebSocket on the same HTTP port
if logger.S != nil {
logger.S.Infof("Starting WebSocket server on HTTP port: %d, paths: %v", config.Server.Port, wsPaths)
logger.WithReqID(startupReqID).Infof("Loaded configuration: port=%d, routes=%d", config.Server.Port, len(wsPaths))
} else {
fmt.Printf("Starting WebSocket server on HTTP port: %d, paths: %v\n", config.Server.Port, wsPaths)
fmt.Printf("Loaded configuration: port=%d, routes=%d\n", config.Server.Port, len(wsPaths))
}
for _, wsPath := range wsPaths {
if logger.S != nil {
logger.WithReqID(startupReqID).Infof("Registering route: [%s]", wsPath)
} else {
fmt.Printf("Registering route: [%s]\n", wsPath)
}
e.GET(wsPath, wsHandler)
}
}
Expand Down
Loading