-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
116 lines (95 loc) · 4.97 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
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
package handler
import (
"net/http"
"time"
"github.com/rs/cors"
"github.com/zyghq/zyg/ports"
)
func handleGetIndex(w http.ResponseWriter, _ *http.Request) {
tm := time.Now().UTC().Format(time.RFC1123)
w.Header().Set("x-datetime", tm)
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte("ok"))
if err != nil {
return
}
}
func NewServer(
authService ports.AuthServicer,
accountService ports.AccountServicer,
workspaceService ports.WorkspaceServicer,
customerService ports.CustomerServicer,
threadChatService ports.ThreadServicer,
) http.Handler {
mux := http.NewServeMux()
// initialize service handlers
ah := NewAccountHandler(accountService, workspaceService)
wh := NewWorkspaceHandler(workspaceService, accountService, customerService)
th := NewThreadChatHandler(workspaceService, threadChatService)
mux.HandleFunc("GET /{$}", handleGetIndex)
mux.HandleFunc("POST /accounts/auth/{$}", ah.handleGetOrCreateAccount)
mux.Handle("POST /pats/{$}", NewEnsureAuthAccount(ah.handleCreatePat, authService))
mux.Handle("GET /pats/{$}", NewEnsureAuthAccount(ah.handleGetPatList, authService))
mux.Handle("DELETE /pats/{patId}/{$}", NewEnsureAuthAccount(ah.handleDeletePat, authService))
mux.Handle("POST /workspaces/{$}", NewEnsureAuthAccount(wh.handleCreateWorkspace, authService))
mux.Handle("GET /workspaces/{$}", NewEnsureAuthAccount(wh.handleGetWorkspaces, authService))
mux.Handle("GET /workspaces/{workspaceId}/{$}",
NewEnsureMemberAuth(wh.handleGetWorkspace, authService))
mux.Handle("PATCH /workspaces/{workspaceId}/{$}",
NewEnsureMemberAuth(wh.handleUpdateWorkspace, authService))
mux.Handle("POST /workspaces/{workspaceId}/sk/{$}",
NewEnsureMemberAuth(wh.handleGenerateSecretKey, authService))
mux.Handle("GET /workspaces/{workspaceId}/sk/{$}",
NewEnsureMemberAuth(wh.handleGetWorkspaceSecretKey, authService))
mux.Handle("GET /workspaces/{workspaceId}/members/{$}",
NewEnsureMemberAuth(wh.handleGetWorkspaceMembers, authService))
mux.Handle("GET /workspaces/{workspaceId}/members/me/{$}",
NewEnsureMemberAuth(wh.handleGetWorkspaceMembership, authService))
mux.Handle("GET /workspaces/{workspaceId}/members/{memberId}/{$}",
NewEnsureMemberAuth(wh.handleGetWorkspaceMember, authService))
mux.Handle("POST /workspaces/{workspaceId}/customers/{$}",
NewEnsureMemberAuth(wh.handleCreateWorkspaceCustomer, authService))
mux.Handle("GET /workspaces/{workspaceId}/customers/{$}",
NewEnsureMemberAuth(wh.handleGetWorkspaceCustomers, authService))
mux.Handle("POST /workspaces/{workspaceId}/labels/{$}",
NewEnsureMemberAuth(wh.handleCreateWorkspaceLabel, authService))
mux.Handle("GET /workspaces/{workspaceId}/labels/{$}",
NewEnsureMemberAuth(wh.handleGetWorkspaceLabels, authService))
mux.Handle("PATCH /workspaces/{workspaceId}/labels/{labelId}/{$}",
NewEnsureMemberAuth(wh.handleUpdateWorkspaceLabel, authService))
mux.Handle("GET /workspaces/{workspaceId}/labels/{labelId}/{$}",
NewEnsureMemberAuth(wh.handleGetWorkspaceLabel, authService))
mux.Handle("GET /workspaces/{workspaceId}/threads/chat/{$}",
NewEnsureMemberAuth(th.handleGetThreadChats, authService))
mux.Handle("PATCH /workspaces/{workspaceId}/threads/chat/{threadId}/{$}",
NewEnsureMemberAuth(th.handleUpdateThreadChat, authService))
mux.Handle("GET /workspaces/{workspaceId}/threads/chat/with/me/{$}",
NewEnsureMemberAuth(th.handleGetMyThreadChats, authService))
mux.Handle("GET /workspaces/{workspaceId}/threads/chat/with/unassigned/{$}",
NewEnsureMemberAuth(th.handleGetUnassignedThChats, authService))
mux.Handle("GET /workspaces/{workspaceId}/threads/chat/with/labels/{labelId}/{$}",
NewEnsureMemberAuth(th.handleGetLabelledThreadChats, authService))
mux.Handle("POST /workspaces/{workspaceId}/threads/chat/{threadId}/messages/{$}",
NewEnsureMemberAuth(th.handleCreateThreadChatMessage, authService))
mux.Handle("GET /workspaces/{workspaceId}/threads/chat/{threadId}/messages/{$}",
NewEnsureMemberAuth(th.handleGetThreadChatMessages, authService))
mux.Handle("PUT /workspaces/{workspaceId}/threads/chat/{threadId}/labels/{$}",
NewEnsureMemberAuth(th.handleSetThreadChatLabel, authService))
mux.Handle("GET /workspaces/{workspaceId}/threads/chat/{threadId}/labels/{$}",
NewEnsureMemberAuth(th.handleGetThreadChatLabels, authService))
mux.Handle("DELETE /workspaces/{workspaceId}/threads/chat/{threadId}/labels/{labelId}/{$}",
NewEnsureMemberAuth(th.handleDeleteThreadChatLabel, authService))
mux.Handle("GET /workspaces/{workspaceId}/threads/chat/metrics/{$}",
NewEnsureMemberAuth(th.handleGetThreadChatMetrics, authService))
mux.Handle("POST /workspaces/{workspaceId}/widgets/{$}",
NewEnsureMemberAuth(wh.handleCreateWidget, authService))
mux.Handle("GET /workspaces/{workspaceId}/widgets/{$}",
NewEnsureMemberAuth(wh.handleGetWidgets, authService))
c := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH", "HEAD"},
AllowedHeaders: []string{"*"},
})
handler := LoggingMiddleware(c.Handler(mux))
return handler
}