Skip to content

Commit b46d177

Browse files
authored
feat: added cors to erpc and added a test for marshalling the config (#48)
1 parent 996884a commit b46d177

File tree

3 files changed

+632
-5
lines changed

3 files changed

+632
-5
lines changed

pkg/erpc-proxy/erpc-proxy.go

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -296,11 +296,23 @@ func marshalErpcConfig(config ErpcProxyConfig) (string, error) {
296296
// Build the config structure for YAML marshaling
297297
configMap := map[string]interface{}{
298298
"logLevel": config.LogLevel,
299-
"server": map[string]interface{}{
300-
"httpHostV4": config.Server.HttpHostV4,
301-
"httpPortV4": config.Server.HttpPortV4,
302-
"maxTimeout": config.Server.MaxTimeout,
303-
},
299+
}
300+
301+
// Add server config if provided
302+
if config.Server.HttpHostV4 != "" || config.Server.HttpPortV4 > 0 || config.Server.MaxTimeout != "" {
303+
serverMap := map[string]interface{}{}
304+
if config.Server.HttpHostV4 != "" {
305+
serverMap["httpHostV4"] = config.Server.HttpHostV4
306+
}
307+
if config.Server.HttpPortV4 > 0 {
308+
serverMap["httpPortV4"] = config.Server.HttpPortV4
309+
}
310+
if config.Server.MaxTimeout != "" {
311+
serverMap["maxTimeout"] = config.Server.MaxTimeout
312+
}
313+
if len(serverMap) > 0 {
314+
configMap["server"] = serverMap
315+
}
304316
}
305317

306318
// Add database config if provided
@@ -378,6 +390,35 @@ func marshalErpcConfig(config ErpcProxyConfig) (string, error) {
378390
}
379391
projectMap["networks"] = networks
380392

393+
// Add CORS config if provided
394+
if project.Cors != nil {
395+
corsMap := map[string]interface{}{}
396+
397+
if len(project.Cors.AllowedOrigins) > 0 {
398+
corsMap["allowedOrigins"] = project.Cors.AllowedOrigins
399+
}
400+
if len(project.Cors.AllowedMethods) > 0 {
401+
corsMap["allowedMethods"] = project.Cors.AllowedMethods
402+
}
403+
if len(project.Cors.AllowedHeaders) > 0 {
404+
corsMap["allowedHeaders"] = project.Cors.AllowedHeaders
405+
}
406+
if len(project.Cors.ExposedHeaders) > 0 {
407+
corsMap["exposedHeaders"] = project.Cors.ExposedHeaders
408+
}
409+
if project.Cors.AllowCredentials {
410+
corsMap["allowCredentials"] = project.Cors.AllowCredentials
411+
}
412+
if project.Cors.MaxAge > 0 {
413+
corsMap["maxAge"] = project.Cors.MaxAge
414+
}
415+
416+
// Only add CORS section if there are any configured values
417+
if len(corsMap) > 0 {
418+
projectMap["cors"] = corsMap
419+
}
420+
}
421+
381422
projects = append(projects, projectMap)
382423
}
383424
configMap["projects"] = projects

0 commit comments

Comments
 (0)