-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
108 lines (92 loc) · 2.43 KB
/
server.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
package main
import (
"context"
"time"
"github.com/Urethramancer/signor/env"
"github.com/go-chi/chi"
"github.com/go-chi/httprate"
"github.com/grimdork/sweb"
"github.com/jackc/pgx/v4/pgxpool"
)
// API endpoints:
// POST /message - token, sender, destinations, subject, template name, JSON data
// Shoutyface runtime data.
type Shoutyface struct {
sweb.Server
dbp *pgxpool.Pool
mailquit chan interface{}
mailqueue chan Message
mailhost string
mailport string
mailuser string
mailpass string
mailfrom string
}
// NewServer initialises the API server.
func NewServer() (*Shoutyface, error) {
srv := &Shoutyface{
mailquit: make(chan interface{}),
mailqueue: make(chan Message),
mailhost: env.Get("MAILHOST", "localhost"),
mailport: env.Get("MAILPORT", "587"),
mailuser: env.Get("MAILUSER", ""),
mailpass: env.Get("MAILPASS", ""),
mailfrom: env.Get("MAILFROM", "root@localhost"),
}
srv.Init()
var err error
srv.dbp, err = pgxpool.Connect(context.Background(), env.Get("DATABASE_URL", "localhost"))
if err != nil {
srv.E("Database error: %s", err.Error())
return nil, err
}
var id int
row := srv.dbp.QueryRow(context.Background(), "select id from users limit 1")
err = row.Scan(&id)
if err != nil && err.Error() != "" {
err := srv.createTables()
if err != nil {
return nil, err
}
}
srv.L("Connected to database.")
srv.InitMiddleware()
srv.AddStartHook(srv.StartShouty)
srv.AddStopHook(srv.StopShouty)
srv.Route("/api/message", func(r chi.Router) {
r.Use(httprate.LimitByIP(60, 1*time.Minute))
r.Use(sweb.AddJSONHeaders)
r.Use(srv.tokencheck)
r.Post("/", srv.PostMessage)
})
srv.Route("/api", func(r chi.Router) {
r.Use(httprate.LimitByIP(60, 1*time.Minute))
r.Use(sweb.AddJSONHeaders)
r.Use(srv.admintokencheck)
r.Get("/users", srv.GetUsers)
r.Post("/user", srv.PostUser)
r.Delete("/user", srv.DeleteUser)
r.Post("/channel", srv.PostChannel)
r.Delete("/channel", srv.DeleteChannel)
r.Get("/channels", srv.GetChannels)
r.Post("/subscribe", srv.PostSubscribe)
r.Delete("/subscribe", srv.DeleteSubscription)
r.Get("/subscriptions", srv.GetSubscriptions)
})
return srv, nil
}
// StartShouty server.
func (sh *Shoutyface) StartShouty() error {
sh.L("Servcer starting.")
go func() {
sh.RunMailQueue(sh.mailquit)
}()
return nil
}
// StopShouty gracefully.
func (srv *Shoutyface) StopShouty() {
srv.mailquit <- true
srv.dbp.Close()
srv.Wait()
srv.L("Server stopped")
}