forked from beshoo/go-whatsapp-rest-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
127 lines (107 loc) · 4.11 KB
/
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
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
package main
import (
"flag"
"fmt"
"net/http"
"strings"
"runtime"
wapi "bitbucket.org/rockyOO7/wa-api/api"
"bitbucket.org/rockyOO7/wa-api/gen/restapi"
"bitbucket.org/rockyOO7/wa-api/gen/restapi/operations"
"bitbucket.org/rockyOO7/wa-api/gen/restapi/operations/number"
"bitbucket.org/rockyOO7/wa-api/gen/restapi/operations/profile"
"bitbucket.org/rockyOO7/wa-api/gen/restapi/operations/send"
storage "bitbucket.org/rockyOO7/wa-api/storage"
_ "github.com/jinzhu/gorm/dialects/mysql"
wa "bitbucket.org/rockyOO7/wa-api/whatsapp"
"github.com/go-openapi/loads"
"github.com/rs/cors"
log "github.com/sirupsen/logrus"
)
var portFlag = flag.Int("port", 3000, "Port to run this service on")
var dbType = flag.String("db", "", "Db Type")
var dbURL = flag.String("dburl", "", "Db url")
func main() {
swaggerSpec, err := loads.Analyzed(restapi.SwaggerJSON, "")
if err != nil {
log.Error(err)
return
}
api := operations.NewWaAPI(swaggerSpec)
server := restapi.NewServer(api)
defer server.Shutdown()
flag.Parse()
server.Port = *portFlag
router(api)
server.SetHandler(handlerCors(swaggerUI(api.Serve(nil))))
if dbURL != nil {
// storage.Init(storage.MYSQL, "root:passwd@/wapidb")
storage.Init(storage.MYSQL, *dbURL)
defer storage.Close()
sessions, err := storage.GetAllSessions()
if err != nil {
panic(fmt.Sprintf("Get sessions failed %v", err))
}
wa.StartWithSession(sessions)
}
if err := server.Serve(); err != nil {
log.Errorf("Cannot start server %v", err)
}
}
func PrintMemUsage() {
var m runtime.MemStats
runtime.ReadMemStats(&m)
// For info on each, see: https://golang.org/pkg/runtime/#MemStats
fmt.Printf("Alloc = %v MiB", bToMb(m.Alloc))
fmt.Printf("\tTotalAlloc = %v MiB", bToMb(m.TotalAlloc))
fmt.Printf("\tSys = %v MiB", bToMb(m.Sys))
fmt.Printf("\tNumGC = %v\n", m.NumGC)
}
func bToMb(b uint64) uint64 {
return b / 1024 / 1024
}
func swaggerUI(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
PrintMemUsage()
log.Infof("Url %v", r.URL.Path)
if strings.HasPrefix(r.URL.Path, "/api") {
handler.ServeHTTP(w, r)
} else {
http.FileServer(http.Dir("./static")).ServeHTTP(w, r)
}
})
}
func handlerCors(handler http.Handler) http.Handler {
corsHandler := cors.New(cors.Options{
Debug: false,
AllowedHeaders: []string{"*"},
AllowedOrigins: []string{"*"},
AllowedMethods: []string{},
MaxAge: 1000,
})
return corsHandler.Handler(handler)
}
func router(api *operations.WaAPI) {
api.ProfileScanQrHandler = profile.ScanQrHandlerFunc(wapi.ScanQr)
api.ProfileProfileHandler = profile.ProfileHandlerFunc(wapi.GetProfile)
api.ProfileSetHookHandler = profile.SetHookHandlerFunc(wapi.SetHook)
api.ProfileGetContactsHandler = profile.GetContactsHandlerFunc(wapi.GetContacts)
api.ProfileIsConnectedHandler = profile.IsConnectedHandlerFunc(wapi.IsConnected)
api.ProfileConnectHandler = profile.ConnectHandlerFunc(wapi.Connect)
api.ProfileDisconnectHandler = profile.DisconnectHandlerFunc(wapi.Disconnect)
api.ProfileLogoutHandler = profile.LogoutHandlerFunc(wapi.Logout)
api.SendSendVcardHandler = send.SendVcardHandlerFunc(wapi.SendVcard)
api.SendSendLinkHandler = send.SendLinkHandlerFunc(wapi.SendLink)
api.SendSendReadHandler = send.SendReadHandlerFunc(wapi.SendReadAck)
api.SendSendTextHandler = send.SendTextHandlerFunc(wapi.SendText)
api.SendSendLocationHandler = send.SendLocationHandlerFunc(wapi.SendLocation)
api.SendSendImageHandler = send.SendImageHandlerFunc(wapi.SendImage)
api.SendSendVideoHandler = send.SendVideoHandlerFunc(wapi.SendVideo)
api.SendSendAudioHandler = send.SendAudioHandlerFunc(wapi.SendAudio)
api.SendSendDocHandler = send.SendDocHandlerFunc(wapi.SendDoc)
api.SendSendAudioRecordHandler = send.SendAudioRecordHandlerFunc(wapi.SendAudioRecord)
api.NumberHasWhatsAppHandler = number.HasWhatsAppHandlerFunc(wapi.HasWhatApp)
api.NumberIsOnlineHandler = number.IsOnlineHandlerFunc(wapi.IsOnline)
api.NumberGetChatsHandler = number.GetChatsHandlerFunc(wapi.GetChats)
api.NumberGetAvatarHandler = number.GetAvatarHandlerFunc(wapi.GetAvatar)
}