-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
138 lines (109 loc) · 2.79 KB
/
main.go
File metadata and controls
138 lines (109 loc) · 2.79 KB
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
package main
import (
"context"
"encoding/gob"
"errors"
"log"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"time"
"webchat/chat"
"webchat/db"
"webchat/handler"
"github.com/gorilla/sessions"
"github.com/joho/godotenv"
)
const (
listenAddr = ":8080"
shutdownTimeout = 3 * time.Second
)
var store *sessions.CookieStore
func main() {
err := godotenv.Load()
if err != nil {
log.Fatal(".env file not found")
}
handler.Init()
db.Init()
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()
if err := runWebServer(ctx); err != nil {
log.Println(err)
}
db.Close()
}
func runWebServer(ctx context.Context) error {
secret, found := os.LookupEnv("cookie_store_secret")
if !found {
return errors.New("environment variable cookie_store_secret not found in .env")
}
store = sessions.NewCookieStore([]byte(secret))
store.MaxAge(86400)
gob.Register(db.User{})
go chat.Run()
var mux = http.NewServeMux()
var srv = &http.Server{
Addr: listenAddr,
Handler: mux,
}
mux.HandleFunc("/", baseHandler)
mux.HandleFunc("/favicon.ico", faviconHandler)
go func() {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
db.Close()
log.Fatalf("Server Listen and Serve: %v", err)
}
}()
log.Printf("Server Started: %s", listenAddr)
<-ctx.Done()
log.Println("Shutting down server gracefully")
shutdownCtx, cancel := context.WithTimeout(context.Background(), shutdownTimeout)
defer cancel()
if err := srv.Shutdown(shutdownCtx); err != nil {
return err
}
<-shutdownCtx.Done()
err := ctx.Err()
if err != nil {
return err
}
return nil
}
func baseHandler(response http.ResponseWriter, request *http.Request) {
command := request.URL.Query().Get("command")
command = strings.TrimSpace(command)
command = strings.ToUpper(command)
session, _ := store.Get(request, "cookie-name")
auth, ok := session.Values["authenticated"].(bool)
if !ok || !auth {
switch command {
case "LOGIN":
handler.Login(response, request, session)
case "SHOW_REGISTRATION_PAGE":
handler.ShowRegistrationPage(response, request, session)
case "REGISTER":
handler.Register(response, request, session)
case "SHOW_LOGIN_PAGE":
handler.ShowLoginPage(response, request, session)
default:
handler.ShowLoginPage(response, request, session)
}
return
}
switch command {
case "SHOW_CHAT_PAGE":
handler.ShowChatPage(response, request, session)
case "CREATE_WEB_SOCKET_CONNECTION":
handler.CreateWebSocketConnection(response, request, session)
case "LOGOUT":
handler.Logout(response, request, session)
default:
handler.ShowChatPage(response, request, session)
}
}
func faviconHandler(response http.ResponseWriter, request *http.Request) {
http.ServeFile(response, request, "resources/favicon.ico")
}