2022-06-30 20:32:56 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
2022-07-01 22:40:13 +00:00
|
|
|
"github.com/allegro/bigcache/v3"
|
2022-06-30 20:32:56 +00:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2022-07-01 22:40:13 +00:00
|
|
|
c, err := bigcache.NewBigCache(bigcache.DefaultConfig(time.Hour * 2))
|
|
|
|
if err != nil {
|
|
|
|
fatal("can't initialize caching")
|
|
|
|
}
|
|
|
|
cache = c
|
|
|
|
|
2022-06-30 20:32:56 +00:00
|
|
|
r := mux.NewRouter()
|
|
|
|
|
2022-07-01 22:40:13 +00:00
|
|
|
r.Use(securityHeaders)
|
|
|
|
|
2022-10-26 22:19:03 +00:00
|
|
|
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { render("home", w, nil) })
|
2022-06-30 20:32:56 +00:00
|
|
|
r.HandleFunc("/{id}-lyrics", lyricsHandler)
|
2022-10-24 19:51:39 +00:00
|
|
|
r.HandleFunc("/images/{filename}.{ext}", proxyHandler)
|
2022-06-30 20:32:56 +00:00
|
|
|
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
|
|
|
|
|
|
|
|
server := &http.Server{
|
|
|
|
Handler: r,
|
|
|
|
WriteTimeout: 10 * time.Second,
|
|
|
|
ReadTimeout: 10 * 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 {
|
|
|
|
fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
info(fmt.Sprintf("server is listening on port %d", port))
|
|
|
|
|
|
|
|
fatal(server.Serve(l))
|
|
|
|
}
|