forked from Alttaf/ably-hack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
72 lines (61 loc) · 1.7 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
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"time"
"ably-text/env"
"ably-text/twitter"
"github.com/ably/ably-go/ably"
_ "github.com/joho/godotenv/autoload"
"github.com/julienschmidt/httprouter"
)
func Index(w http.ResponseWriter, _ *http.Request, _ httprouter.Params) {
fmt.Fprint(w, "Welcome!\n")
}
func Hello(w http.ResponseWriter, _ *http.Request, ps httprouter.Params) {
w.Header().Add("Access-Control-Allow-Origin", "*")
ablyAPIKey, err := env.RetrieveValue("ABLY_API_KEY")
if err != nil {
fmt.Fprint(w, err.Error())
return
}
ablyClient, err := ably.NewRealtime(ably.WithKey(ablyAPIKey))
if err != nil {
fmt.Fprintf(w, "error when attempting to create client :: "+err.Error())
return
}
twitterQuery := ps.ByName("name")
channel := ablyClient.Channels.Get("test")
/* Publish a message to the test channel */
ctx, _ := context.WithTimeout(context.Background(), 120*time.Second)
// TODO: the cancel function returned by context.WithTimeout should be called, not discarded, to avoid a context leak
go func() {
for i := 0; i < 5; i++ {
data, err := twitter.RetrieveTweets(twitterQuery)
if err != nil {
fmt.Fprintf(w, "could not get twitter data")
}
fmt.Println(data)
err = channel.Publish(ctx, "test", data)
if err != nil {
fmt.Fprintf(w, "there was an err %v", err)
}
time.Sleep(20 * time.Second)
}
}()
fmt.Fprintf(w, "Hello Ably, %s!\n", ps.ByName("name"))
}
func main() {
port, present := os.LookupEnv("PORT")
if !present {
port = "8080"
}
fmt.Printf("starting Server on port %s", ":"+port)
router := httprouter.New()
router.GET("/", Index)
router.GET("/hello/:name", Hello)
log.Fatal(http.ListenAndServe(":"+port, router))
}