feat: embed static files into binary
This commit is contained in:
@ -22,7 +22,7 @@ func TestAlbum(t *testing.T) {
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
l := utils.NewLogger(os.Stdout)
|
||||
m := New(l)
|
||||
m := New(l, &assets{})
|
||||
|
||||
m.ServeHTTP(rr, r)
|
||||
|
||||
|
@ -20,7 +20,7 @@ func TestAnnotations(t *testing.T) {
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
l := utils.NewLogger(os.Stdout)
|
||||
m := New(l)
|
||||
m := New(l, &assets{})
|
||||
|
||||
m.ServeHTTP(rr, r)
|
||||
|
||||
|
@ -2,12 +2,7 @@ package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"mime"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/a-h/templ"
|
||||
gorillaHandlers "github.com/gorilla/handlers"
|
||||
@ -16,7 +11,7 @@ import (
|
||||
"github.com/rramiachraf/dumb/views"
|
||||
)
|
||||
|
||||
func New(logger *utils.Logger) *mux.Router {
|
||||
func New(logger *utils.Logger, staticFiles static) *mux.Router {
|
||||
r := mux.NewRouter()
|
||||
|
||||
r.Use(utils.MustHeaders)
|
||||
@ -31,25 +26,7 @@ func New(logger *utils.Logger) *mux.Router {
|
||||
r.HandleFunc("/search", search(logger)).Methods("GET")
|
||||
r.HandleFunc("/{annotation-id}/{artist-song}/{verse}/annotations", annotations(logger)).Methods("GET")
|
||||
r.HandleFunc("/instances.json", instances(logger)).Methods("GET")
|
||||
r.PathPrefix("/static/").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
url := strings.Replace(r.URL.Path, "/static", "static", 1)
|
||||
f, err := os.Open(url)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
views.ErrorPage(http.StatusNotFound, "page not found")
|
||||
return
|
||||
}
|
||||
|
||||
defer f.Close()
|
||||
|
||||
mimeType := mime.TypeByExtension(path.Ext(r.URL.Path))
|
||||
w.Header().Set("content-type", mimeType)
|
||||
|
||||
if _, err := io.Copy(w, f); err != nil {
|
||||
logger.Error(err.Error())
|
||||
}
|
||||
|
||||
})
|
||||
r.PathPrefix("/static/").HandlerFunc(staticAssets(logger, staticFiles))
|
||||
r.PathPrefix("/{annotation-id}/{artist-song}-lyrics").HandlerFunc(lyrics(logger)).Methods("GET")
|
||||
r.PathPrefix("/{annotation-id}/{artist-song}").HandlerFunc(lyrics(logger)).Methods("GET")
|
||||
r.PathPrefix("/{annotation-id}").HandlerFunc(lyrics(logger)).Methods("GET")
|
||||
|
13
handlers/handler_test.go
Normal file
13
handlers/handler_test.go
Normal file
@ -0,0 +1,13 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"os"
|
||||
"path"
|
||||
)
|
||||
|
||||
type assets struct{}
|
||||
|
||||
func (assets) Open(p string) (fs.File, error) {
|
||||
return os.Open(path.Join("./", p))
|
||||
}
|
@ -19,7 +19,7 @@ func TestInstancesList(t *testing.T) {
|
||||
rr := httptest.NewRecorder()
|
||||
l := utils.NewLogger(os.Stdout)
|
||||
|
||||
m := New(l)
|
||||
m := New(l, &assets{})
|
||||
m.ServeHTTP(rr, r)
|
||||
|
||||
c := rr.Result().Header.Get("content-type")
|
||||
|
@ -33,7 +33,7 @@ func testLyrics(t *testing.T, url string) {
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
l := utils.NewLogger(os.Stdout)
|
||||
m := New(l)
|
||||
m := New(l, &assets{})
|
||||
|
||||
m.ServeHTTP(rr, r)
|
||||
|
||||
|
@ -53,7 +53,7 @@ func imageProxy(l *utils.Logger) http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Add("Content-type", mime.TypeByExtension("."+ext))
|
||||
w.Header().Set("Content-type", mime.TypeByExtension("."+ext))
|
||||
w.Header().Add("Cache-Control", "max-age=1296000")
|
||||
if _, err = io.Copy(w, res.Body); err != nil {
|
||||
l.Error("unable to write image, %s", err.Error())
|
||||
|
@ -20,7 +20,7 @@ func TestImageProxy(t *testing.T) {
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
l := utils.NewLogger(os.Stdout)
|
||||
m := New(l)
|
||||
m := New(l, &assets{})
|
||||
|
||||
m.ServeHTTP(rr, r)
|
||||
|
||||
|
@ -21,7 +21,7 @@ func TestSearch(t *testing.T) {
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
l := utils.NewLogger(os.Stdout)
|
||||
m := New(l)
|
||||
m := New(l, &assets{})
|
||||
|
||||
m.ServeHTTP(rr, r)
|
||||
|
||||
|
38
handlers/static.go
Normal file
38
handlers/static.go
Normal file
@ -0,0 +1,38 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"io"
|
||||
"io/fs"
|
||||
"mime"
|
||||
"net/http"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/rramiachraf/dumb/utils"
|
||||
"github.com/rramiachraf/dumb/views"
|
||||
)
|
||||
|
||||
type static interface {
|
||||
Open(string) (fs.File, error)
|
||||
}
|
||||
|
||||
func staticAssets(logger *utils.Logger, embededFiles static) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
url := strings.Replace(r.URL.Path, "/static", "static", 1)
|
||||
f, err := embededFiles.Open(url)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
views.ErrorPage(http.StatusNotFound, "page not found")
|
||||
return
|
||||
}
|
||||
|
||||
defer f.Close()
|
||||
|
||||
mimeType := mime.TypeByExtension(path.Ext(r.URL.Path))
|
||||
w.Header().Set("content-type", mimeType)
|
||||
|
||||
if _, err := io.Copy(w, f); err != nil {
|
||||
logger.Error(err.Error())
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user