-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
39 lines (34 loc) · 793 Bytes
/
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
package main
import (
"log"
"net/http"
"os"
"time"
"github.com/matheuspolitano/chat-app-go/pkg/commHub"
"github.com/matheuspolitano/chat-app-go/pkg/handlers"
)
func main() {
args := os.Args
port := "8080"
if len(args) >= 2 {
port = args[1]
}
staticDir := http.Dir("./public")
fileServer := http.FileServer(staticDir)
hub := commHub.NewHub()
go hub.Run()
http.Handle("/static/", http.StripPrefix("/static/", fileServer))
http.Handle("/", fileServer)
http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
handlers.ServeWs(w, r, hub)
})
log.Printf("Server starting on port %s\n", port)
server := &http.Server{
Addr: ":" + port,
ReadHeaderTimeout: 3 * time.Second,
}
err := server.ListenAndServe()
if err != nil {
panic(err)
}
}