-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
81 lines (66 loc) · 1.63 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
package main
import (
"context"
"encoding/json"
"io"
"net/http"
"os"
"os/signal"
"syscall"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/hashicorp/go-hclog"
"github.com/ioki-mobility/TicTxtToe/backend"
"github.com/ioki-mobility/TicTxtToe/frontend"
"github.com/twilio/twilio-go"
)
type config struct {
Username string `json:"username"`
Password string `json:"password"`
PhoneNumber string `json:"phonenumber"`
}
func main() {
lg := hclog.Default()
ctx, can := context.WithCancel(context.Background())
f, err := os.Open("config.json")
if err != nil {
panic(err)
}
b, err := io.ReadAll(f)
if err != nil {
panic(err)
}
cfg := &config{}
err = json.Unmarshal(b, cfg)
if err != nil {
panic(err)
}
client := twilio.NewRestClientWithParams(twilio.ClientParams{
Username: cfg.Username,
Password: cfg.Password,
})
twilioPhoneNumber := cfg.PhoneNumber
bk := backend.NewBackend()
fr := frontend.New(client, bk, twilioPhoneNumber, lg)
go watchSignals(can, lg)
r := chi.NewRouter()
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
r.Post("/sms", func(w http.ResponseWriter, r *http.Request) {
fr.ServeHTTP(w, r)
})
srv := http.Server{Addr: ":4040", Handler: r}
go func() {
_ = srv.ListenAndServe()
}()
<-ctx.Done()
_ = srv.Shutdown(ctx)
}
// watchSignals waits for one of the registered OS signals. Once a signal is received it calls
// fn and exits.
func watchSignals(can context.CancelFunc, lg hclog.Logger) {
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
lg.Info("received OS signal", "signal", <-sig)
can()
}