-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.go
111 lines (98 loc) · 2.82 KB
/
http.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
package api
import (
"encoding/json"
"fmt"
"github.com/gorilla/mux"
"github.com/gorilla/sessions"
"net/http"
)
const storeName = "science_cursor"
var router = mux.NewRouter()
var sessionStore *sessions.CookieStore
type funcWithSession func(r *http.Request, s *sessionData) (interface{}, error)
func httpListen() {
sessionStore = sessions.NewCookieStore(apiConfig.HTTP.SessionKey)
http.ListenAndServe(fmt.Sprintf("%s:%d", apiConfig.HTTP.Address, apiConfig.HTTP.Port), router)
}
func handlerWithSession(handler funcWithSession) func(http.ResponseWriter, *http.Request) {
return func(rw http.ResponseWriter, req *http.Request) {
// Retrieves client session from cookie
clientSession, err := sessionStore.Get(req, storeName)
if err != nil {
fmt.Println("Could not retrieve the client session")
return
}
var serverSession *sessionModel
var needCreation = false
if clientSession.IsNew {
fmt.Println("Client session is new")
needCreation = true
} else {
key, exists := clientSession.Values["key"]
if !exists {
fmt.Println("Client session do not contain key")
needCreation = true
} else {
exists, _ = sessionExists(key.([]byte))
if !exists {
fmt.Println("Key is obsolete")
needCreation = true
}
}
}
if needCreation {
serverSession, err = createSession()
if err != nil {
fmt.Println("Could not create server session")
return
}
clientSession.Values["key"] = serverSession.Key
} else {
key := clientSession.Values["key"]
serverSession, err = getSession(key.([]byte))
if err != nil {
fmt.Println("Error while retrieving server session")
return
}
}
answer, err := handler(req, &serverSession.Data)
// Response handling //
if err != nil {
if apiConfig.General.Production {
http.Error(rw, "500 - Unexpected error", http.StatusInternalServerError)
} else {
http.Error(rw, fmt.Sprintf("500 - %s", err.Error()), http.StatusInternalServerError)
}
} else {
err = clientSession.Save(req, rw)
if err != nil {
fmt.Println("Could not save the client session:", err.Error())
}
if serverSession.Data.MarkedForDeletion {
err = serverSession.destroy()
if err != nil {
fmt.Println("Could not destroy the server session:", err.Error())
}
} else {
err = serverSession.save()
if err != nil {
fmt.Println("Could not save the server session:", err.Error())
}
}
if answer == nil {
rw.WriteHeader(http.StatusNoContent)
} else {
rw.WriteHeader(http.StatusOK)
data, err := json.Marshal(answer)
if err != nil {
if apiConfig.General.Production {
http.Error(rw, "500 - Unexpected error", http.StatusInternalServerError)
} else {
http.Error(rw, fmt.Sprintf("500 - %s", err.Error()), http.StatusInternalServerError)
}
}
rw.Write(data)
}
}
}
}