/
server.go
170 lines (144 loc) · 4.38 KB
/
server.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package main
import (
"encoding/json"
"fmt"
"io"
"math/rand"
"net/http"
"os"
"strings"
"time"
"github.com/anachronistic/apns"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
type Post struct {
ID bson.ObjectId `bson:"_id,omitempty"`
User string
Position string
FileName string
IsHorizontal string
Filter string
Separator string
}
type Push struct {
ID bson.ObjectId `bson:"_id,omitempty"`
User string
Token string
}
func getDeviceTokenForUser(user string) string {
session, _ := mgo.Dial("127.0.0.1")
defer session.Close()
session.SetMode(mgo.Monotonic, true)
c := session.DB("chocoshot").C("push")
var result Push
c.Find(bson.M{"user": user}).One(&result)
return result.Token
}
func setPushTokenForUser(user string, token string) {
session, _ := mgo.Dial("127.0.0.1")
defer session.Close()
session.SetMode(mgo.Monotonic, true)
c := session.DB("chocoshot").C("push")
c.Insert(&Push{User: user, Token: token})
}
func sendPushToUser(user string, fromUser string) {
payload := apns.NewPayload()
payload.Alert = fromUser + " vous a envoyé un shot 😏📸"
payload.Badge = 1
payload.Sound = "bingbong.aiff"
pn := apns.NewPushNotification()
pn.DeviceToken = getDeviceTokenForUser(user)
pn.AddPayload(payload)
client := apns.NewClient("gateway.push.apple.com:2195", "./Push/Prod/PushChocoshotCert.pem", "./Push/Prod/key.unencrypted.pem")
client.Send(pn)
}
func insertDatabase(p Post) {
session, _ := mgo.Dial("127.0.0.1")
defer session.Close()
session.SetMode(mgo.Monotonic, true)
c := session.DB("chocoshot").C("post")
c.Insert(&Post{User: p.User, Position: p.Position, FileName: p.FileName, IsHorizontal: p.IsHorizontal, Filter: p.Filter, Separator: p.Separator})
}
func getFromDataBaseWithUser(u string, deletation bool) Post {
session, _ := mgo.Dial("127.0.0.1")
defer session.Close()
session.SetMode(mgo.Monotonic, true)
c := session.DB("chocoshot").C("post")
var results Post
c.Find(bson.M{"user": u}).One(&results)
if deletation == true {
c.Remove(bson.M{"filename": results.FileName})
}
return results
}
func getPost(w http.ResponseWriter, r *http.Request) {
user := r.Header.Get("user")
deletation := r.Header.Get("deletation")
var res = getFromDataBaseWithUser(user, deletation == "true")
json.NewEncoder(w).Encode(res)
}
func updatePushToken(w http.ResponseWriter, r *http.Request) {
user := strings.TrimSpace(r.FormValue("user"))
token := strings.TrimSpace(r.FormValue("token"))
setPushTokenForUser(user, token)
var res = getDeviceTokenForUser(user)
json.NewEncoder(w).Encode(res)
}
func uploadPost(w http.ResponseWriter, r *http.Request) {
user := strings.TrimSpace(r.FormValue("user"))
destination := strings.TrimSpace(r.FormValue("destination"))
position := strings.TrimSpace(r.FormValue("position"))
isHorizontal := strings.TrimSpace(r.FormValue("isHorizontal"))
filter := strings.TrimSpace(r.FormValue("filter"))
separator := strings.TrimSpace(r.FormValue("separator"))
r.ParseMultipartForm(32 << 20)
file, _, err := r.FormFile("uploadfile")
if err != nil {
fmt.Println(err)
return
}
defer file.Close()
fileName := RandomString(20)
f, err := os.OpenFile("./img/"+fileName+".png", os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
fmt.Println(err)
return
}
insertDatabase(Post{User: destination, Position: position, FileName: fileName + ".png", IsHorizontal: isHorizontal, Filter: filter, Separator: separator})
sendPushToUser(destination, user)
defer f.Close()
io.Copy(f, file)
}
func RandomString(strlen int) string {
rand.Seed(time.Now().UTC().UnixNano())
const chars = "abcdefghijklmnopqrstuvwxyz0123456789"
result := make([]byte, strlen)
for i := 0; i < strlen; i++ {
result[i] = chars[rand.Intn(len(chars))]
}
return string(result)
}
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/post", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
getPost(w, r)
} else {
uploadPost(w, r)
}
})
mux.HandleFunc("/push", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
updatePushToken(w, r)
}
})
mux.Handle("/image/", http.StripPrefix("/image/", http.FileServer(http.Dir("./img/"))))
/*if strings.HasPrefix(r.URL.RequestURI(), "/image/") {
file := strings.Replace(r.URL.RequestURI(), "/image/", "./img/", 1)
fmt.Println("deleting " + file)
defer os.Remove(file)
}*/
fmt.Println("listening at :80")
http.ListenAndServe(":80", mux)
}