forked from mkasner/drops
/
drops.go
131 lines (107 loc) · 3.52 KB
/
drops.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
//Currently implementing http serving with goji
// I'll drop that in the future
package drops
import (
"fmt"
"log"
"net/http"
"github.com/mkasner/drops/element"
"github.com/mkasner/drops/event"
"github.com/mkasner/drops/protocol"
"github.com/mkasner/drops/router"
"github.com/mkasner/drops/session"
)
var (
rtr *router.Router
)
func NewDrops() {
router.InitRouter()
rtr = router.GetRouter()
session.NewSessionStore()
event.NewEventStore()
}
// GET is a shortcut for r.Handle("GET", path, handle)
func GET(path string, handle router.Handle) {
rtr.Handle("GET", path, handle)
}
// POST is a shortcut for r.Handle("POST", path, handle)
func POST(path string, handle router.Handle) {
rtr.Handle("POST", path, handle)
}
// PUT is a shortcut for r.Handle("PUT", path, handle)
func PUT(path string, handle router.Handle) {
rtr.Handle("PUT", path, handle)
}
// PATCH is a shortcut for r.Handle("PATCH", path, handle)
func PATCH(path string, handle router.Handle) {
rtr.Handle("PATCH", path, handle)
}
// DELETE is a shortcut for r.Handle("DELETE", path, handle)
func DELETE(path string, handle router.Handle) {
rtr.Handle("DELETE", path, handle)
}
func EVENT(path string, handle router.Handle) {
rtr.Handle("EVENT", path, handle)
}
func HandleFunc(path string, handle http.HandlerFunc) {
rtr.HandlerFunc("GET", path, handle)
}
//register handlers from events
func RegisterHandlers(events event.Events) error {
for _, e := range events {
rtr.Handle(e.Type, e.Route, e.Handler)
}
return nil
}
func Serve(port, assetsPath, assetsUrl string) {
if assetsPath != "" {
fmt.Printf("Assets path: %s\n", assetsPath)
rtr.ServeFiles("/"+assetsUrl+"/*filepath", http.Dir(assetsPath))
} else {
rtr.ServeFiles("/assets/*filepath", http.Dir("assets"))
}
rtr.HandlerFunc("GET", "/ws", ServeWs)
// rtr.HandlerFunc("GET", "/*", ResourceHandler)
log.Fatal(http.ListenAndServe(":"+port, rtr))
}
func ResourceHandler(w http.ResponseWriter, r *http.Request, dropsResponse *protocol.DropsResponse) *protocol.DropsResponse {
// log.Printf("Resource handler: %s - %s\n", r.Method, r.URL.Path)
var response string
if w != nil && dropsResponse.Dom != nil {
var sessionId string
sessionCookie, err := r.Cookie("session")
// fmt.Printf("sessionCookie: %+v\n", pretty.Formatter(sessionCookie))
if err != nil {
// fmt.Printf("Error fetching cookie %v\n", err)
//No cookie found
sessionId = session.CreateSession("")
sessionCookie = &http.Cookie{Name: "session", Value: sessionId, Path: "/"}
// fmt.Printf("Created Cookie: %s: %+v\n", sessionCookie.Name, sessionCookie.Value)
} else {
// log.Printf("Cookie: %s: %+v\n", sessionCookie.Name, sessionCookie.Value)
sessionId = sessionCookie.Value
if !session.SessionExist(sessionId) {
sessionId = session.CreateSession(sessionId)
}
sessionCookie = &http.Cookie{Name: "session", Value: sessionId, Path: "/"}
// sessionCookie.Value = sessionId
}
// fmt.Printf("sessionId: %s\n", sessionId)
session.SetSessionActiveDOM(sessionId, dropsResponse.Dom)
// fmt.Printf("\ndom set as active: %+v\n", pretty.Formatter(dom.Id))
// ActiveDOM = dom
buffer := element.Render(&dropsResponse.Dom.View)
response = buffer.String()
// cookie := &http.Cookie{Name: "session", Value: sessionId}
// http.Set
http.SetCookie(w, sessionCookie)
fmt.Fprint(w, response)
return nil
}
return dropsResponse
}
//Used for static file serving
func Assets(w http.ResponseWriter, r *http.Request) {
// logger.Errorf("Assets handler: %s", r.URL.Path[1:])
http.ServeFile(w, r, r.URL.Path[1:])
}