forked from alextanhongpin/go-chat.v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
151 lines (128 loc) · 3.89 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
151
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
_ "net/http/pprof"
"os/signal"
"strings"
"syscall"
"time"
"github.com/alextanhongpin/go-chat.v2/domain"
"github.com/alextanhongpin/go-chat.v2/infra"
"github.com/alextanhongpin/go-chat.v2/pkg/chat"
"github.com/alextanhongpin/go-chat.v2/pkg/ticket"
"github.com/julienschmidt/httprouter"
)
type contextKey string
const (
port = 3000
)
type Message struct {
// From: user id, To: group id
From, To string
Type string
Text string
}
func main() {
redis := infra.NewRedis()
// NOTE: Separate the token for authorizing user, vs token for authorizing
// websocket connection.
// For simplicity, we use the same for both.
issuer := ticket.New([]byte("secret :)"), 24*time.Hour)
router := httprouter.New()
router.POST("/authenticate", newHandleAuthenticate(issuer))
router.POST("/authorize", authorize(issuer, handleAuthorize))
router.NotFound = http.FileServer(http.Dir("public"))
router.Handler(http.MethodGet, "/debug/pprof/*item", http.DefaultServeMux)
c, close := chat.New("chat", redis, issuer)
defer close()
handleWs := func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
c.ServeWS(w, r)
}
router.GET("/ws", handleWs)
log.Printf("listening to port *:%d. press ctrl + c to cancel\n", port)
srv := &http.Server{
Addr: fmt.Sprintf(":%d", port),
Handler: router,
}
Server(srv)
}
type authorizer interface {
Issue(subject string) (string, error)
Verify(token string) (string, error)
}
func authorize(t authorizer, next httprouter.Handle) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
auth := r.Header.Get("Authorization")
token := strings.ReplaceAll(auth, "Bearer ", "")
user, err := t.Verify(token)
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
ctx := context.WithValue(r.Context(), contextKey("user"), user)
r = r.WithContext(ctx)
next(w, r, ps)
}
}
func handleAuthorize(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
user, ok := r.Context().Value(contextKey("user")).(string)
if !ok {
http.Error(w, "unauthorized", http.StatusUnauthorized)
return
}
u := domain.User{Username: user}
if err := json.NewEncoder(w).Encode(u); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
}
func newHandleAuthenticate(t authorizer) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
var req domain.User
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
ticket, err := t.Issue(req.Username)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
res := domain.Credential{
AccessToken: ticket,
}
if err := json.NewEncoder(w).Encode(&res); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
}
}
func Server(srv *http.Server) {
// Create context that listens for the interrupt signal from the OS.
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()
// Initializing the server in a goroutine so that
// it won't block the graceful shutdown handling below
go func() {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %s\n", err)
}
}()
// Listen for the interrupt signal.
<-ctx.Done()
// Restore default behavior on the interrupt signal and notify user of shutdown.
stop()
log.Println("shutting down gracefully, press Ctrl+C again to force")
// The context is used to inform the server it has 5 seconds to finish
// the request it is currently handling
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Fatal("Server forced to shutdown: ", err)
}
log.Println("Server exiting")
}