Initial MVP: Go + templ Tolino-optimized ebook library scaffold
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/arnef/ebooks/internal/library"
|
||||
"github.com/arnef/ebooks/views"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
lib *library.Service
|
||||
}
|
||||
|
||||
func NewHandler(lib *library.Service) *Handler {
|
||||
return &Handler{lib: lib}
|
||||
}
|
||||
|
||||
func (h *Handler) RegisterRoutes(mux *http.ServeMux) {
|
||||
mux.HandleFunc("GET /", h.listBooks)
|
||||
mux.HandleFunc("GET /book/{id}", h.bookDetails)
|
||||
mux.HandleFunc("GET /download/{id}", h.downloadBook)
|
||||
mux.Handle("GET /static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
|
||||
}
|
||||
|
||||
func (h *Handler) listBooks(w http.ResponseWriter, r *http.Request) {
|
||||
books, err := h.lib.ListBooks()
|
||||
if err != nil {
|
||||
http.Error(w, "failed to load books", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
render(w, r.Context(), views.IndexPage(books))
|
||||
}
|
||||
|
||||
func (h *Handler) bookDetails(w http.ResponseWriter, r *http.Request) {
|
||||
id := r.PathValue("id")
|
||||
if id == "" {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
book, err := h.lib.FindBook(id)
|
||||
if err != nil {
|
||||
http.Error(w, "failed to load book", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if book == nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
render(w, r.Context(), views.BookPage(*book))
|
||||
}
|
||||
|
||||
func (h *Handler) downloadBook(w http.ResponseWriter, r *http.Request) {
|
||||
id := r.PathValue("id")
|
||||
if id == "" {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
book, err := h.lib.FindBook(id)
|
||||
if err != nil {
|
||||
http.Error(w, "failed to load book", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if book == nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
cleanBase := filepath.Clean(h.lib.BooksDir())
|
||||
cleanPath := filepath.Clean(book.Path)
|
||||
if !strings.HasPrefix(cleanPath, cleanBase+string(filepath.Separator)) && cleanPath != cleanBase {
|
||||
http.Error(w, "invalid file path", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Disposition", "attachment; filename=\""+book.Filename+"\"")
|
||||
http.ServeFile(w, r, cleanPath)
|
||||
}
|
||||
|
||||
func render(w http.ResponseWriter, ctx context.Context, page interface{ Render(context.Context, http.ResponseWriter) error }) {
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
if err := page.Render(ctx, w); err != nil && !errors.Is(err, context.Canceled) {
|
||||
http.Error(w, "render failed", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user