-
Notifications
You must be signed in to change notification settings - Fork 27
/
main.go
41 lines (33 loc) · 893 Bytes
/
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
package main
import (
"context"
"fmt"
"html/template"
"log"
"net/http"
"github.com/jfyne/live"
)
const (
problem = "problem"
)
func main() {
t, err := template.ParseFiles("examples/root.html", "examples/error/view.html")
if err != nil {
log.Fatal(err)
}
h, err := live.NewHandler(live.NewCookieStore("session-name", []byte("weak-secret")), live.WithTemplateRenderer(t))
if err != nil {
log.Fatal(err)
}
h.Error = func(ctx context.Context, w http.ResponseWriter, r *http.Request, err error) {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("this is a bad request"))
}
h.HandleEvent(problem, func(s *live.Socket, _ map[string]interface{}) (interface{}, error) {
return nil, fmt.Errorf("hello")
})
http.Handle("/error", h)
http.Handle("/live.js", live.Javascript{})
http.Handle("/auto.js.map", live.JavascriptMap{})
http.ListenAndServe(":8080", nil)
}