/
main.go
80 lines (66 loc) · 1.83 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main
import (
"github.com/railsgirlssb/wufoo-count-app/Godeps/_workspace/src/github.com/go-martini/martini"
"github.com/railsgirlssb/wufoo-count-app/Godeps/_workspace/src/github.com/martini-contrib/cors"
"github.com/railsgirlssb/wufoo-count-app/Godeps/_workspace/src/github.com/martini-contrib/render"
"github.com/railsgirlssb/wufoo-count-app/Godeps/_workspace/src/gopkg.in/resty.v0"
"encoding/json"
"fmt"
"os"
"strconv"
"strings"
)
type WufooConfig struct {
Account string
ApiKey string
Password string
FormIds []string
}
var wufooConfig WufooConfig
func count() (int, error) {
count := 0
type Result struct {
EntryCount string `json:"EntryCount"`
}
for _, formId := range wufooConfig.FormIds {
resp, err := resty.R().
SetHeader("Accept", "application/json").
SetBasicAuth(wufooConfig.ApiKey, wufooConfig.Password).
Get(fmt.Sprintf("https://%s.wufoo.com/api/v3/forms/%s/entries/count.json", wufooConfig.Account, formId))
if err != nil {
return count, err
}
var result Result
err = json.Unmarshal(resp.Body, &result)
if err != nil {
return count, err
}
entryCount, _ := strconv.Atoi(result.EntryCount)
count += entryCount
}
return count, nil
}
func main() {
port := os.Getenv("PORT")
if len(port) < 1 {
port = "8080"
}
wufooConfig.Account = os.Getenv("WUFOO_ACCOUNT")
wufooConfig.ApiKey = os.Getenv("WUFOO_API_KEY")
wufooConfig.Password = "any"
wufooConfig.FormIds = strings.Split(os.Getenv("WUFOO_FORM_IDS"), ",")
m := martini.Classic()
m.Use(render.Renderer())
m.Use(cors.Allow(&cors.Options{
AllowAllOrigins: true,
}))
m.Get("/", func(r render.Render) {
count, err := count()
if err != nil {
r.JSON(200, map[string]interface{}{"error": "can't fetch information"})
} else {
r.JSON(200, map[string]interface{}{"count": count})
}
})
m.RunOnAddr(":" + port)
}