dumb/main.go

51 lines
910 B
Go
Raw Normal View History

2022-06-30 20:32:56 +00:00
package main
import (
"fmt"
"net"
"net/http"
"os"
"strconv"
"time"
"github.com/allegro/bigcache/v3"
2022-06-30 20:32:56 +00:00
"github.com/gorilla/mux"
)
func main() {
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()
r.Use(securityHeaders)
2022-06-30 20:32:56 +00:00
r.HandleFunc("/{id}-lyrics", lyricsHandler)
2022-10-11 13:23:10 +00:00
r.HandleFunc("/images/{filename}.jpg", 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))
}