forked from asternic/wuzapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
executable file
·150 lines (125 loc) · 3.91 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"context"
"database/sql"
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"path/filepath"
"syscall"
"time"
"go.mau.fi/whatsmeow/store/sqlstore"
waLog "go.mau.fi/whatsmeow/util/log"
"github.com/gorilla/mux"
"github.com/patrickmn/go-cache"
"github.com/rs/zerolog"
_ "modernc.org/sqlite"
)
type server struct {
db *sql.DB
router *mux.Router
exPath string
}
var (
address = flag.String("address", "0.0.0.0", "Bind IP Address")
port = flag.String("port", "8080", "Listen Port")
waDebug = flag.String("wadebug", "", "Enable whatsmeow debug (INFO or DEBUG)")
logType = flag.String("logtype", "console", "Type of log output (console or json)")
sslcert = flag.String("sslcertificate", "", "SSL Certificate File")
sslprivkey = flag.String("sslprivatekey", "", "SSL Certificate Private Key File")
token = flag.String("token", "", "Token for authentication an Admin user")
container *sqlstore.Container
killchannel = make(map[int](chan bool))
userinfocache = cache.New(5*time.Minute, 10*time.Minute)
log zerolog.Logger
)
func init() {
flag.Parse()
if *logType == "json" {
log = zerolog.New(os.Stdout).With().Timestamp().Str("role", filepath.Base(os.Args[0])).Logger()
} else {
output := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339}
log = zerolog.New(output).With().Timestamp().Str("role", filepath.Base(os.Args[0])).Logger()
}
}
func main() {
ex, err := os.Executable()
if err != nil {
panic(err)
}
exPath := filepath.Dir(ex)
dbDirectory := exPath + "/dbdata"
_, err = os.Stat(dbDirectory)
if os.IsNotExist(err) {
errDir := os.MkdirAll(dbDirectory, 0751)
if errDir != nil {
panic("Could not create dbdata directory")
}
}
if token == nil || *token == "" {
*token = "1234ABC"
}
db, err := sql.Open("sqlite", exPath+"/dbdata/users.db")
if err != nil {
log.Fatal().Err(err).Msg("Could not open/create " + exPath + "/dbdata/users.db")
os.Exit(1)
}
defer db.Close()
sqlStmt := `CREATE TABLE IF NOT EXISTS users (id INTEGER NOT NULL PRIMARY KEY, name TEXT NOT NULL, token TEXT NOT NULL, webhook TEXT NOT NULL default "", jid TEXT NOT NULL default "", qrcode TEXT NOT NULL default "", connected INTEGER, expiration INTEGER, events TEXT NOT NULL default "All");`
_, err = db.Exec(sqlStmt)
if err != nil {
panic(fmt.Sprintf("%q: %s\n", err, sqlStmt))
}
CreateAdminUser(db, token)
if *waDebug != "" {
dbLog := waLog.Stdout("Database", *waDebug, true)
container, err = sqlstore.New("sqlite", "file:"+exPath+"/dbdata/main.db?_foreign_keys=on&_busy_timeout=3000", dbLog)
} else {
container, err = sqlstore.New("sqlite", "file:"+exPath+"/dbdata/main.db?_foreign_keys=on&_busy_timeout=3000", nil)
}
if err != nil {
panic(err)
}
s := &server{
router: mux.NewRouter(),
db: db,
exPath: exPath,
}
s.routes()
s.connectOnStartup()
srv := &http.Server{
Addr: *address + ":" + *port,
Handler: s.router,
}
done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
go func() {
if *sslcert != "" {
if err := srv.ListenAndServeTLS(*sslcert, *sslprivkey); err != nil && err != http.ErrServerClosed {
//log.Fatalf("listen: %s\n", err)
log.Fatal().Err(err).Msg("Startup failed")
}
} else {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
//log.Fatalf("listen: %s\n", err)
log.Fatal().Err(err).Msg("Startup failed")
}
}
}()
//wlog.Infof("Server Started. Listening on %s:%s", *address, *port)
log.Info().Str("address", *address).Str("port", *port).Msg("Server Started")
<-done
log.Info().Msg("Server Stoped")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer func() {
// extra handling here
cancel()
}()
if err := srv.Shutdown(ctx); err != nil {
log.Error().Str("error", fmt.Sprintf("%+v", err)).Msg("Server Shutdown Failed")
os.Exit(1)
}
log.Info().Msg("Server Exited Properly")
}