simplify env var setting, precisely allocate maps

This commit is contained in:
Reed Allman
2017-08-02 15:40:50 -07:00
parent 9ccd48eb18
commit 4ad1167284

View File

@@ -142,19 +142,6 @@ func (s *Server) serve(ctx context.Context, c *gin.Context, appName string, rout
if route.Format == "" { if route.Format == "" {
route.Format = "default" route.Format = "default"
} }
envVars := map[string]string{
"METHOD": c.Request.Method,
"APP_NAME": appName,
"ROUTE": route.Path,
"REQUEST_URL": fmt.Sprintf("%v//%v%v", func() string {
if c.Request.TLS == nil {
return "http"
}
return "https"
}(), c.Request.Host, c.Request.URL.String()),
"CALL_ID": reqID,
"FN_FORMAT": route.Format,
}
// baseVars are the vars on the route & app, not on this specific request [for hot functions] // baseVars are the vars on the route & app, not on this specific request [for hot functions]
baseVars := make(map[string]string, len(app.Config)+len(route.Config)+3) baseVars := make(map[string]string, len(app.Config)+len(route.Config)+3)
@@ -165,15 +152,29 @@ func (s *Server) serve(ctx context.Context, c *gin.Context, appName string, rout
// app config // app config
for k, v := range app.Config { for k, v := range app.Config {
k = toEnvName("", k) k = toEnvName("", k)
envVars[k] = v
baseVars[k] = v baseVars[k] = v
} }
for k, v := range route.Config { for k, v := range route.Config {
k = toEnvName("", k) k = toEnvName("", k)
envVars[k] = v
baseVars[k] = v baseVars[k] = v
} }
// envVars contains the full set of env vars, per request + base
envVars := make(map[string]string, len(baseVars)+len(params)+len(c.Request.Header)+3)
for k, v := range baseVars {
envVars[k] = v
}
envVars["CALL_ID"] = reqID
envVars["METHOD"] = c.Request.Method
envVars["REQUEST_URL"] = fmt.Sprintf("%v//%v%v", func() string {
if c.Request.TLS == nil {
return "http"
}
return "https"
}(), c.Request.Host, c.Request.URL.String())
// params // params
for _, param := range params { for _, param := range params {
envVars[toEnvName("PARAM", param.Key)] = param.Value envVars[toEnvName("PARAM", param.Key)] = param.Value