-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
69 lines (59 loc) · 1.61 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
// Copyright 2013 The Gorilla WebSocket Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"encoding/json"
"flag"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/centrifugal/centrifugo/libcentrifugo/auth"
"github.com/centrifugal/gocent"
)
var addr = flag.String("addr", ":8080", "http service address")
var config = struct {
Secret string `json:"secret"`
}{}
func main() {
flag.Parse()
configFile, err := os.Open("config.json")
if err != nil {
panic(err)
}
if err := json.NewDecoder(configFile).Decode(&config); err != nil {
panic(err)
}
if config.Secret = strings.TrimSpace(config.Secret); config.Secret == "" {
panic("empty secret!")
}
token := auth.GenerateClientToken(config.Secret, "42", "1451991486", "")
log.Println(token)
http.Handle("/", http.FileServer(http.Dir("./assets")))
client := gocent.NewClient("http://localhost:8000", config.Secret, 5*time.Second)
http.HandleFunc("/message", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
if r.Method != http.MethodPost {
return
}
bodyBytes, err := ioutil.ReadAll(r.Body)
if err != nil {
panic(err)
}
success, err := client.Publish("public:news", bodyBytes)
if err != nil {
log.Println("could not publish:", err)
}
if !success {
log.Println("success if false")
}
w.WriteHeader(http.StatusNoContent)
})
log.Println("Server started on " + *addr)
if err := http.ListenAndServe(*addr, nil); err != nil {
log.Fatal("ListenAndServe: ", err)
}
}