You've already forked hastebin-ansi
mirror of
https://github.com/armbian/hastebin-ansi.git
synced 2026-01-06 12:30:55 -08:00
static: embed static files
This commit is contained in:
committed by
M. Efe Çetin
parent
30c44c211b
commit
94b2c702ce
@@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -13,6 +12,7 @@ import (
|
||||
"github.com/armbian/ansi-hastebin/handler"
|
||||
"github.com/armbian/ansi-hastebin/internal/keygenerator"
|
||||
"github.com/armbian/ansi-hastebin/internal/storage"
|
||||
"github.com/armbian/ansi-hastebin/static"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/chi/v5/middleware"
|
||||
"github.com/go-chi/httprate"
|
||||
@@ -67,18 +67,17 @@ func (s *Server) RegisterRoutes() {
|
||||
})
|
||||
|
||||
// Register static files
|
||||
static := os.DirFS("static")
|
||||
fileServer := http.FileServer(http.FS(static))
|
||||
fileServer := http.FileServer(http.FS(static.StaticFS))
|
||||
|
||||
s.mux.Get("/*", func(w http.ResponseWriter, r *http.Request) {
|
||||
path := strings.TrimPrefix(r.URL.Path, "/")
|
||||
if _, err := static.Open(path); err == nil {
|
||||
if _, err := static.StaticFS.Open(path); err == nil {
|
||||
fileServer.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
// If file does not exist, serve index.html
|
||||
index, err := static.Open("index.html")
|
||||
index, err := static.StaticFS.Open("index.html")
|
||||
if err != nil {
|
||||
http.Error(w, "Not found", http.StatusNotFound)
|
||||
return
|
||||
|
||||
6
static/static.go
Normal file
6
static/static.go
Normal file
@@ -0,0 +1,6 @@
|
||||
package static
|
||||
|
||||
import "embed"
|
||||
|
||||
//go:embed *.html *.css *.js *.ico *.png *.txt
|
||||
var StaticFS embed.FS
|
||||
Reference in New Issue
Block a user