Example #1
0
func GistHandler(w http.ResponseWriter, req *http.Request) {

	vars := mux.Vars(req)

	gistId := vars["gist-id"]
	username := vars["username"]

	port := req.FormValue("port")

	if gistId == "" || username == "" {
		http.Error(w, "not found", 404)
		return
	}

	gist, err := builder.FetchGistData(gistId)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	var bf string
	fileList := make([]string, len(gist.Files))
	i := 0
	for k, file := range gist.Files {
		fmt.Println(file.Filename)
		if file.Filename == "Baofile" {
			bf = file.Content
		}

		fileList[i] = k
		i++
	}
	sort.Strings(fileList)

	var name string
	if gist.Description == "" {
		if len(gist.Files) > 0 {
			name = fileList[0]
		}
	} else {
		name = gist.Description
	}

	response := GistResponse{
		Name:      name,
		Filenames: fileList,
		Gist:      gist,
		Baofile:   bf,
		Port:      port,
	}
	RenderTemplate(w, "gist", response)
}
Example #2
0
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)
}