forked from rramiachraf/dumb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
65 lines (51 loc) · 1.47 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
package main
import (
"fmt"
"net"
"net/http"
"os"
"strconv"
"time"
"github.com/allegro/bigcache/v3"
"github.com/gorilla/mux"
"github.com/sirupsen/logrus"
)
var logger = logrus.New()
func main() {
c, err := bigcache.NewBigCache(bigcache.DefaultConfig(time.Hour * 24))
if err != nil {
logger.Fatalln("can't initialize caching")
}
cache = c
r := mux.NewRouter()
r.Use(securityHeaders)
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { render("home", w, nil) })
r.HandleFunc("/search", searchHandler).Methods("GET")
r.HandleFunc("/{id}-lyrics", lyricsHandler)
r.HandleFunc("/{id}/{artist-song}/{verse}/annotations", annotationsHandler)
r.HandleFunc("/images/{filename}.{ext}", proxyHandler)
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
r.HandleFunc("/albums/{artist}/{albumName}", albumHandler)
r.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
render("error", w, map[string]string{
"Status": "404",
"Error": "page not found",
})
})
server := &http.Server{
Handler: r,
WriteTimeout: 25 * time.Second,
ReadTimeout: 25 * time.Second,
}
port, _ := strconv.Atoi(os.Getenv("PORT"))
if port == 0 {
port = 5555
}
l, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
if err != nil {
logger.Fatalln(err)
}
logger.Infof("server is listening on port %d\n", port)
logger.Fatalln(server.Serve(l))
}