/
main.go
executable file
·95 lines (81 loc) · 1.86 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"net/http"
"html/template"
"appengine"
"appengine/channel"
"appengine/datastore"
)
func
init() {
http.HandleFunc( "/", handle )
http.HandleFunc( "/_ah/channel/connected/", connected )
http.HandleFunc( "/_ah/channel/disconnected/", disconnected )
}
func
ChannelKey( ctx appengine.Context, id string ) *datastore.Key {
return datastore.NewKey( ctx, "Channels", id, 0, nil )
}
type
NULL struct {
}
func
connected( w http.ResponseWriter, r *http.Request ) {
ctx := appengine.NewContext( r )
if _, err := datastore.Put(
ctx,
ChannelKey( ctx, r.FormValue( "from" ) ),
&NULL{},
); err != nil { panic( err ) }
}
func
disconnected( w http.ResponseWriter, r *http.Request ) {
ctx := appengine.NewContext( r )
if err := datastore.Delete(
ctx,
ChannelKey( ctx, r.FormValue( "from" ) ),
); err != nil { panic( err ) }
}
func
Execute( tag string, w http.ResponseWriter, p interface{} ) {
if err := template.Must( template.ParseFiles( tag + ".html" ) ).Execute(
w,
p,
); err != nil { panic( err ) }
}
func
handle( w http.ResponseWriter, r *http.Request ) {
switch r.Method {
case "GET":
Execute( "get", w, nil )
case "POST":
wID := r.FormValue( "ID" )
wToken, err := channel.Create( appengine.NewContext( r ), wID )
if err != nil {
Execute( "get", w, map[ string ]string { "Error": err.Error() } )
} else {
Execute(
"post",
w,
map[ string ]string {
"ID" : wID,
"Token" : wToken,
},
)
}
case "PUT":
ctx := appengine.NewContext( r )
keys, err := datastore.NewQuery( "Channels" ).KeysOnly().GetAll( ctx, nil )
if err != nil { panic( err ) }
for _, key := range keys {
if err := channel.SendJSON(
ctx,
key.StringID(),
map[ string ]interface{} {
"message" : r.FormValue( "Message" ),
"ID" : r.FormValue( "ID" ),
},
); err != nil { panic( err ) }
}
}
}