forked from asternic/wuzapi
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
executable file
·133 lines (112 loc) · 3.42 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
package main
import (
"context"
"database/sql"
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"path/filepath"
"syscall"
"time"
"github.com/gorilla/mux"
"github.com/patrickmn/go-cache"
"github.com/rs/zerolog"
"github.com/spf13/viper"
_ "modernc.org/sqlite"
)
type server struct {
db *sql.DB
router *mux.Router
}
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")
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() {
viper.SetConfigType("yaml")
viper.AddConfigPath(".")
viper.SetConfigName("config")
err := viper.ReadInConfig()
if err != nil {
log.Fatal().Err(err).Msg(err.Error())
}
dbDirectory := "dbdata"
_, err = os.Stat(dbDirectory)
if os.IsNotExist(err) {
errDir := os.MkdirAll(dbDirectory, 0751)
if errDir != nil {
panic("Could not create dbdata directory")
}
}
db, err := sql.Open("sqlite", "./dbdata/users.db")
if err != nil {
log.Fatal().Err(err).Msg("Could not open/create ./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))
}
s := &server{
router: mux.NewRouter(),
db: db,
}
s.routes()
s.connectOnStartup()
if viper.GetString("server.port") != "" {
*port = viper.GetString("server.port")
}
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")
}