func BaoHandler(w http.ResponseWriter, req *http.Request) { vars := mux.Vars(req) baoId := vars["id"] bao, err := model.GetBaoById(baoId) if err != nil { http.Error(w, "not found", 404) return } gist, err := builder.FetchGistData(bao.GistId) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } svc := cloudwatch.New(&aws.Config{Region: aws.String("us-east-1")}) params := &cloudwatch.GetMetricStatisticsInput{ EndTime: aws.Time(time.Now()), MetricName: aws.String("Invocations"), Namespace: aws.String("AWS/Lambda"), Period: aws.Int64(480 * 8), StartTime: aws.Time(time.Now().Add(-time.Hour * 24 * 20)), Statistics: []*string{ aws.String("Sum"), }, Dimensions: []*cloudwatch.Dimension{ { Name: aws.String("FunctionName"), Value: aws.String(bao.FunctionName), }, }, Unit: aws.String("Count"), } resp, err := svc.GetMetricStatistics(params) if err != nil { http.Error(w, err.Error(), 500) } fmt.Println(err) response := BaoResponse{ Id: baoId, Gist: gist, Bao: bao, Stats: resp.String(), Root: config.C["root"], DateCreated: bao.Ts.Format("3:04pm Jan 2, 2006"), } RenderTemplate(w, "bao", response) }
func handler(w http.ResponseWriter, r *http.Request) { host := r.Host host_parts := strings.Split(host, ".") var subdomain string var functionName string if len(host_parts) == 3 { subdomain = host_parts[0] fmt.Println(subdomain) functionName = destinations[subdomain] } if subdomain == "test" { http.Error(w, "all ok", http.StatusOK) return } if functionName == "" && subdomain != "" { fmt.Printf("checking for %s in the db\n", subdomain) isValidHex := bson.IsObjectIdHex(subdomain) if isValidHex == true { bao, err := model.GetBaoById(subdomain) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } fmt.Println(bao) destinations[bao.ID.Hex()] = bao.FunctionName functionName = bao.FunctionName } } if functionName == "" { http.Error(w, host+" not found", http.StatusNotFound) return } body, err := ioutil.ReadAll(r.Body) if err != nil { handlerError(err, w) return } var args Args args.Body = string(body) newHeaders := make(map[string]string) for name, content := range r.Header { if len(content) > 0 { newHeaders[name] = content[0] } } newHeaders["Host"] = r.Host args.Headers = newHeaders args.Method = r.Method args.Path = r.URL.RequestURI() reqeust, err := json.Marshal(args) if err != nil { handlerError(err, w) return } lambdaResponse, err := InvokeLambda(functionName, reqeust) if err != nil { handlerError(err, w) return } var response Response err = json.Unmarshal(lambdaResponse.Payload, &response) if err != nil { handlerError(err, w) return } for key, value := range response.Headers { w.Header().Del(key) w.Header().Add(key, value) } base64logs := fmt.Sprintf("%s", *lambdaResponse.LogResult) logdata, err := base64.StdEncoding.DecodeString(base64logs) if len(response.Headers) == 0 { // empty reponse w.WriteHeader(http.StatusInternalServerError) w.Write([]byte("Empty response, maybe there was an error?\n\nAWS Log output:\n\n")) w.Write(logdata) return } fmt.Printf("%#v\n", w.Header()) fmt.Println(response) data, err := base64.StdEncoding.DecodeString(response.Body) if err != nil { handlerError(err, w) return } w.WriteHeader(response.StatusCode) w.Write(data) }