-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
62 lines (52 loc) · 1.52 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
package main
import (
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/gorilla/mux"
ghandlers "github.com/gorilla/handlers"
_ "github.com/go-sql-driver/mysql"
"github.com/LukasJoswiak/wordsearch/app"
"github.com/LukasJoswiak/wordsearch/config"
"github.com/LukasJoswiak/wordsearch/handlers"
)
func main() {
config := config.New()
app, err := app.New(config)
if err != nil {
log.Fatal(err)
}
defer app.Close()
r := mux.NewRouter()
env := handlers.New(app)
env.Init(r)
logFile, err := os.OpenFile(config.LogFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
}
defer logFile.Close()
if _, err = logFile.WriteString("starting server\n"); err != nil {
log.Fatal(err)
}
loggedRouter := ghandlers.LoggingHandler(logFile, r)
// Write errors to error log.
errorLogFile, err := os.OpenFile(config.ErrorLogFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
}
defer errorLogFile.Close()
if _, err = errorLogFile.WriteString("starting server\n"); err != nil {
log.Fatal(err)
}
log.SetOutput(errorLogFile)
server := &http.Server{
Handler: ghandlers.CompressHandler(loggedRouter),
Addr: fmt.Sprintf(":%d", config.Port),
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
fmt.Printf("server listening on port %d\n", config.Port)
log.Fatal(server.ListenAndServe())
}