-
Notifications
You must be signed in to change notification settings - Fork 23
/
main.go
67 lines (52 loc) · 1.08 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
package main
import (
"fmt"
"log"
"net/http"
"os"
"time"
"webrtc-video-chat/models"
"webrtc-video-chat/routes"
"webrtc-video-chat/ws"
"github.com/rs/cors"
"github.com/urfave/negroni"
)
func determineListenAddress() (string, error) {
port := os.Getenv("PORT")
if port == "" {
return ":3000", fmt.Errorf("$PORT not set")
}
return ":" + port, nil
}
func connectDatabase() {
url := os.Getenv("DATABASE_URL")
if url == "" {
url = "user=postgres dbname=postgres password=postgres sslmode=disable"
}
db, err := models.Connect(url)
if err != nil {
log.Fatalf("Connection error: %s", err.Error())
}
models.SetDatabase(db)
}
func main() {
go connectDatabase()
hub := ws.H
go hub.Run()
c := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
})
addr, _ := determineListenAddress()
routes := routes.NewRoutes()
n := negroni.Classic()
n.Use(c)
n.UseHandler(routes)
s := &http.Server{
Addr: addr,
Handler: n,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
log.Fatal(s.ListenAndServe())
}