dumb/main.go

89 lines
2.0 KiB
Go
Raw Normal View History

2022-06-30 21:32:56 +01:00
package main
import (
2024-07-13 22:01:12 +01:00
<<<<<<< HEAD
"context"
2024-07-13 22:01:12 +01:00
=======
2024-05-03 14:47:57 +01:00
"embed"
2024-07-13 22:01:12 +01:00
>>>>>>> upstream/main
2022-06-30 21:32:56 +01:00
"fmt"
"net"
"net/http"
"net/url"
2022-06-30 21:32:56 +01:00
"os"
"strconv"
"time"
"github.com/rramiachraf/dumb/handlers"
2024-05-02 21:29:50 +01:00
"github.com/rramiachraf/dumb/utils"
2022-06-30 21:32:56 +01:00
)
2024-05-03 14:47:57 +01:00
//go:embed static
var staticFiles embed.FS
2022-06-30 21:32:56 +01:00
func main() {
2024-07-13 22:01:12 +01:00
<<<<<<< HEAD
ctx := context.Background()
c, err := bigcache.New(ctx, bigcache.DefaultConfig(time.Hour*24))
if err != nil {
logger.Fatalln("can't initialize caching")
}
cache = c
2022-06-30 21:32:56 +01:00
r := mux.NewRouter()
r.Use(securityHeaders)
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { render("home", w, nil) })
2023-03-09 17:11:37 +01:00
r.HandleFunc("/search", searchHandler).Methods("GET")
2022-06-30 21:32:56 +01:00
r.HandleFunc("/{id}-lyrics", lyricsHandler)
r.HandleFunc("/{id}/{artist-song}/{verse}/annotations", annotationsHandler)
r.HandleFunc("/images/{filename}.{ext}", proxyHandler)
2022-06-30 21:32:56 +01:00
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",
})
})
2024-07-13 22:01:12 +01:00
=======
2024-05-02 21:29:50 +01:00
logger := utils.NewLogger(os.Stdout)
2024-07-13 22:01:12 +01:00
>>>>>>> upstream/main
2022-06-30 21:32:56 +01:00
server := &http.Server{
2024-05-03 14:47:57 +01:00
Handler: handlers.New(logger, staticFiles),
WriteTimeout: 25 * time.Second,
ReadTimeout: 25 * time.Second,
2022-06-30 21:32:56 +01:00
}
PROXY_ENV := os.Getenv("PROXY")
if PROXY_ENV != "" {
if _, err := url.ParseRequestURI(PROXY_ENV); err != nil {
2024-05-03 14:47:57 +01:00
logger.Fatal("invalid proxy")
}
logger.Info("using a custom proxy for requests")
}
2022-06-30 21:32:56 +01:00
port, _ := strconv.Atoi(os.Getenv("PORT"))
if port == 0 {
port = 5555
2024-05-02 21:29:50 +01:00
logger.Info("using default port %d", port)
2022-06-30 21:32:56 +01:00
}
l, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
if err != nil {
2024-05-03 14:47:57 +01:00
logger.Fatal(err.Error())
2022-06-30 21:32:56 +01:00
}
2024-05-02 21:29:50 +01:00
logger.Info("server is listening on port %d", port)
2022-06-30 21:32:56 +01:00
2024-05-02 21:29:50 +01:00
if err := server.Serve(l); err != nil {
2024-05-03 14:47:57 +01:00
logger.Fatal(err.Error())
2024-05-02 21:29:50 +01:00
}
2022-06-30 21:32:56 +01:00
}