forked from asternic/wuzapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.go
107 lines (91 loc) · 4.56 KB
/
routes.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
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)
if *logType == "json" {
log = zerolog.New(os.Stdout).With().Timestamp().Str("role", filepath.Base(os.Args[0])).Str("host", *address).Logger()
} else {
output := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339, NoColor: !*colorOutput}
log = zerolog.New(output).With().Timestamp().Str("role", filepath.Base(os.Args[0])).Str("host", *address).Logger()
}
adminRoutes := s.router.PathPrefix("/admin").Subrouter()
adminRoutes.Use(s.authadmin)
adminRoutes.Handle("/users", s.ListUsers()).Methods("GET")
adminRoutes.Handle("/users", s.AddUser()).Methods("POST")
adminRoutes.Handle("/users/{id}", s.UpdateUser()).Methods("PATCH")
adminRoutes.Handle("/users/{id}", s.DeleteUser()).Methods("DELETE")
c := alice.New()
c = c.Append(s.authalice)
c = c.Append(hlog.NewHandler(log))
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("/webhook", c.Then(s.SetWebhook())).Methods("POST")
s.router.Handle("/webhook", c.Then(s.GetWebhook())).Methods("GET")
s.router.Handle("/chat/send/text", c.Then(s.SendMessage())).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("/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("/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("/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/name", c.Then(s.SetGroupName())).Methods("POST")
// show or hide swagger by flag
if *showSwagger {
s.router.PathPrefix("/").Handler(http.FileServer(http.Dir(exPath + "/static")))
} else {
// return response for root path
s.router.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("API is running"))
}))
}
}