-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
57 lines (45 loc) · 1.06 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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
fb "github.com/huandu/facebook"
"github.com/julienschmidt/httprouter"
"github.com/rs/cors"
)
type Share struct {
Count json.Number
}
func main() {
r := httprouter.New()
r.GET("/", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
params := r.URL.Query()
url := params.Get("url")
total_share := GetCount(url)
t := Share{
Count: total_share,
}
ts, _ := json.Marshal(t)
// Write content-type, statuscode, payload
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
fmt.Fprintf(w, "%s", ts)
})
handler := cors.Default().Handler(r)
port := os.Getenv("PORT")
if port == "" {
port = "3000"
}
log.Fatal(http.ListenAndServe(":"+port, handler))
}
func GetCount(url string) json.Number {
res, _ := fb.Get("/", fb.Params{
"id": url,
"access_token": "1034894286618560|wzqIO5AFDSXaGfp5yXVBpf7o9_M",
})
share := res["share"].(map[string]interface{})
total_share := share["share_count"].(json.Number)
return total_share
}