forked from looplab/eventhorizon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
80 lines (69 loc) · 3.07 KB
/
handler.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
// Copyright (c) 2017 - The Event Horizon authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package handler
import (
"context"
"log"
"net/http"
eh "github.com/2908755265/eventhorizon"
"github.com/2908755265/eventhorizon/httputils"
"github.com/2908755265/eventhorizon/middleware/eventhandler/observer"
"github.com/2908755265/eventhorizon/examples/todomvc/backend/domains/todo"
)
// NewHandler returns a http.Handler that interacts exposes the command handler,
// read repo and event bus to the frontend.
func NewHandler(
commandHandler eh.CommandHandler,
eventBus eh.EventBus,
todoRepo eh.ReadRepo,
staticFolder string,
) (http.Handler, error) {
h := http.NewServeMux()
// Add the event bus as a websocket that sends the events as JSON.
eventBusHandler := httputils.NewEventBusHandler()
observerMiddleware := observer.NewMiddleware(observer.NamedGroup("todomvc"))
eventBus.AddHandler(context.Background(), eh.MatchAll{},
eh.UseEventHandlerMiddleware(eventBusHandler, observerMiddleware))
h.Handle("/api/events/", eventBusHandler)
// Add the todo read repo to query items as JSON objects.
h.Handle("/api/todos/", httputils.QueryHandler(todoRepo))
// Add handlers of all commands in JSON format.
h.Handle("/api/todos/create", httputils.CommandHandler(commandHandler, todo.CreateCommand))
h.Handle("/api/todos/delete", httputils.CommandHandler(commandHandler, todo.DeleteCommand))
h.Handle("/api/todos/add_item", httputils.CommandHandler(commandHandler, todo.AddItemCommand))
h.Handle("/api/todos/remove_item", httputils.CommandHandler(commandHandler, todo.RemoveItemCommand))
h.Handle("/api/todos/remove_completed", httputils.CommandHandler(commandHandler, todo.RemoveCompletedItemsCommand))
h.Handle("/api/todos/set_item_desc", httputils.CommandHandler(commandHandler, todo.SetItemDescriptionCommand))
h.Handle("/api/todos/check_item", httputils.CommandHandler(commandHandler, todo.CheckItemCommand))
h.Handle("/api/todos/check_all_items", httputils.CommandHandler(commandHandler, todo.CheckAllItemsCommand))
// Handle all static files, only allow what is needed.
h.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/",
"/index.html",
"/elm.js",
"/css/base.css",
"/css/styles.css":
http.ServeFile(w, r, staticFolder+r.URL.Path)
default:
w.WriteHeader(http.StatusNotFound)
}
})
// Simple HTTP request logging middleware as final handler.
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Printf("%s %s\n", r.Method, r.URL.EscapedPath())
h.ServeHTTP(w, r)
})
return handler, nil
}