-
-
Notifications
You must be signed in to change notification settings - Fork 414
Expand file tree
/
Copy pathroutes.go
More file actions
163 lines (139 loc) · 7.39 KB
/
routes.go
File metadata and controls
163 lines (139 loc) · 7.39 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package main
import (
"net/http"
"os"
"path/filepath"
"time"
"github.com/justinas/alice"
"github.com/rs/zerolog"
"github.com/rs/zerolog/hlog"
)
type Middleware = alice.Constructor
func (s *server) routes() {
ex, err := os.Executable()
if err != nil {
panic(err)
}
exPath := filepath.Dir(ex)
var routerLog zerolog.Logger
logOutput := os.Stdout
if s.mode == Stdio {
logOutput = os.Stderr
}
if *logType == "json" {
routerLog = zerolog.New(logOutput).
With().
Timestamp().
Str("role", filepath.Base(os.Args[0])).
Str("host", *address).
Logger()
} else {
output := zerolog.ConsoleWriter{
Out: logOutput,
TimeFormat: time.RFC3339,
NoColor: !*colorOutput,
}
routerLog = zerolog.New(output).
With().
Timestamp().
Str("role", filepath.Base(os.Args[0])).
Str("host", *address).
Logger()
}
s.router.Handle("/health", s.GetHealth()).Methods("GET")
adminRoutes := s.router.PathPrefix("/admin").Subrouter()
adminRoutes.Use(s.authadmin)
adminRoutes.Handle("/users", s.ListUsers()).Methods("GET")
adminRoutes.Handle("/users/{id}", s.ListUsers()).Methods("GET")
adminRoutes.Handle("/users", s.AddUser()).Methods("POST")
adminRoutes.Handle("/users/{id}", s.EditUser()).Methods("PUT")
adminRoutes.Handle("/users/{id}", s.DeleteUser()).Methods("DELETE")
adminRoutes.Handle("/users/{id}/full", s.DeleteUserComplete()).Methods("DELETE")
c := alice.New()
c = c.Append(s.authalice)
c = c.Append(hlog.NewHandler(routerLog))
c = c.Append(hlog.AccessHandler(func(r *http.Request, status, size int, duration time.Duration) {
hlog.FromRequest(r).Info().
Str("method", r.Method).
Stringer("url", r.URL).
Int("status", status).
Int("size", size).
Dur("duration", duration).
Str("userid", r.Context().Value("userinfo").(Values).Get("Id")).
Msg("Got API Request")
}))
c = c.Append(hlog.RemoteAddrHandler("ip"))
c = c.Append(hlog.UserAgentHandler("user_agent"))
c = c.Append(hlog.RefererHandler("referer"))
c = c.Append(hlog.RequestIDHandler("req_id", "Request-Id"))
s.router.Handle("/session/connect", c.Then(s.Connect())).Methods("POST")
s.router.Handle("/session/disconnect", c.Then(s.Disconnect())).Methods("POST")
s.router.Handle("/session/logout", c.Then(s.Logout())).Methods("POST")
s.router.Handle("/session/status", c.Then(s.GetStatus())).Methods("GET")
s.router.Handle("/session/qr", c.Then(s.GetQR())).Methods("GET")
s.router.Handle("/session/pairphone", c.Then(s.PairPhone())).Methods("POST")
s.router.Handle("/session/history", c.Then(s.RequestHistorySync())).Methods("GET")
s.router.Handle("/webhook", c.Then(s.SetWebhook())).Methods("POST")
s.router.Handle("/webhook", c.Then(s.GetWebhook())).Methods("GET")
s.router.Handle("/webhook", c.Then(s.DeleteWebhook())).Methods("DELETE")
s.router.Handle("/webhook", c.Then(s.UpdateWebhook())).Methods("PUT")
s.router.Handle("/session/proxy", c.Then(s.SetProxy())).Methods("POST")
s.router.Handle("/session/history", c.Then(s.SetHistory())).Methods("POST")
s.router.Handle("/session/s3/config", c.Then(s.ConfigureS3())).Methods("POST")
s.router.Handle("/session/s3/config", c.Then(s.GetS3Config())).Methods("GET")
s.router.Handle("/session/s3/config", c.Then(s.DeleteS3Config())).Methods("DELETE")
s.router.Handle("/session/s3/test", c.Then(s.TestS3Connection())).Methods("POST")
s.router.Handle("/session/hmac/config", c.Then(s.ConfigureHmac())).Methods("POST")
s.router.Handle("/session/hmac/config", c.Then(s.GetHmacConfig())).Methods("GET")
s.router.Handle("/session/hmac/config", c.Then(s.DeleteHmacConfig())).Methods("DELETE")
s.router.Handle("/chat/send/text", c.Then(s.SendMessage())).Methods("POST")
s.router.Handle("/chat/delete", c.Then(s.DeleteMessage())).Methods("POST")
s.router.Handle("/chat/send/image", c.Then(s.SendImage())).Methods("POST")
s.router.Handle("/chat/send/audio", c.Then(s.SendAudio())).Methods("POST")
s.router.Handle("/chat/send/document", c.Then(s.SendDocument())).Methods("POST")
// s.router.Handle("/chat/send/template", c.Then(s.SendTemplate())).Methods("POST")
s.router.Handle("/chat/send/video", c.Then(s.SendVideo())).Methods("POST")
s.router.Handle("/chat/send/sticker", c.Then(s.SendSticker())).Methods("POST")
s.router.Handle("/chat/send/location", c.Then(s.SendLocation())).Methods("POST")
s.router.Handle("/chat/send/contact", c.Then(s.SendContact())).Methods("POST")
s.router.Handle("/chat/react", c.Then(s.React())).Methods("POST")
s.router.Handle("/chat/send/buttons", c.Then(s.SendButtons())).Methods("POST")
s.router.Handle("/chat/send/list", c.Then(s.SendList())).Methods("POST")
s.router.Handle("/chat/send/poll", c.Then(s.SendPoll())).Methods("POST")
s.router.Handle("/chat/send/edit", c.Then(s.SendEditMessage())).Methods("POST")
s.router.Handle("/chat/history", c.Then(s.GetHistory())).Methods("GET")
s.router.Handle("/chat/request-unavailable-message", c.Then(s.RequestUnavailableMessage())).Methods("POST")
s.router.Handle("/chat/archive", c.Then(s.ArchiveChat())).Methods("POST")
s.router.Handle("/status/set/text", c.Then(s.SetStatusMessage())).Methods("POST")
s.router.Handle("/call/reject", c.Then(s.RejectCall())).Methods("POST")
s.router.Handle("/user/presence", c.Then(s.SendPresence())).Methods("POST")
s.router.Handle("/user/info", c.Then(s.GetUser())).Methods("POST")
s.router.Handle("/user/check", c.Then(s.CheckUser())).Methods("POST")
s.router.Handle("/user/avatar", c.Then(s.GetAvatar())).Methods("POST")
s.router.Handle("/user/contacts", c.Then(s.GetContacts())).Methods("GET")
s.router.Handle("/user/lid/{jid}", c.Then(s.GetUserLID())).Methods("GET")
s.router.Handle("/chat/presence", c.Then(s.ChatPresence())).Methods("POST")
s.router.Handle("/chat/markread", c.Then(s.MarkRead())).Methods("POST")
s.router.Handle("/chat/downloadimage", c.Then(s.DownloadImage())).Methods("POST")
s.router.Handle("/chat/downloadvideo", c.Then(s.DownloadVideo())).Methods("POST")
s.router.Handle("/chat/downloadaudio", c.Then(s.DownloadAudio())).Methods("POST")
s.router.Handle("/chat/downloaddocument", c.Then(s.DownloadDocument())).Methods("POST")
s.router.Handle("/chat/downloadsticker", c.Then(s.DownloadSticker())).Methods("POST")
s.router.Handle("/group/create", c.Then(s.CreateGroup())).Methods("POST")
s.router.Handle("/group/list", c.Then(s.ListGroups())).Methods("GET")
s.router.Handle("/group/info", c.Then(s.GetGroupInfo())).Methods("GET")
s.router.Handle("/group/invitelink", c.Then(s.GetGroupInviteLink())).Methods("GET")
s.router.Handle("/group/photo", c.Then(s.SetGroupPhoto())).Methods("POST")
s.router.Handle("/group/photo/remove", c.Then(s.RemoveGroupPhoto())).Methods("POST")
s.router.Handle("/group/leave", c.Then(s.GroupLeave())).Methods("POST")
s.router.Handle("/group/name", c.Then(s.SetGroupName())).Methods("POST")
s.router.Handle("/group/topic", c.Then(s.SetGroupTopic())).Methods("POST")
s.router.Handle("/group/announce", c.Then(s.SetGroupAnnounce())).Methods("POST")
s.router.Handle("/group/locked", c.Then(s.SetGroupLocked())).Methods("POST")
s.router.Handle("/group/ephemeral", c.Then(s.SetDisappearingTimer())).Methods("POST")
s.router.Handle("/group/join", c.Then(s.GroupJoin())).Methods("POST")
s.router.Handle("/group/inviteinfo", c.Then(s.GetGroupInviteInfo())).Methods("POST")
s.router.Handle("/group/updateparticipants", c.Then(s.UpdateGroupParticipants())).Methods("POST")
s.router.Handle("/newsletter/list", c.Then(s.ListNewsletter())).Methods("GET")
s.router.PathPrefix("/").Handler(http.FileServer(http.Dir(exPath + "/static/")))
}