|
6 | 6 | "net/http" |
7 | 7 | "os" |
8 | 8 | "strconv" |
| 9 | + "strings" |
9 | 10 | "time" |
10 | 11 |
|
11 | 12 | "github.com/armbian/ansi-hastebin/config" |
@@ -67,28 +68,27 @@ func (s *Server) RegisterRoutes() { |
67 | 68 |
|
68 | 69 | // Register static files |
69 | 70 | static := os.DirFS("static") |
70 | | - s.mux.Get("/{id}", func(w http.ResponseWriter, r *http.Request) { |
71 | | - id := chi.URLParam(r, "id") |
72 | | - if file, err := static.Open(id); err == nil { |
73 | | - defer file.Close() |
74 | | - io.Copy(w, file) |
| 71 | + fileServer := http.FileServer(http.FS(static)) |
| 72 | + |
| 73 | + s.mux.Get("/*", func(w http.ResponseWriter, r *http.Request) { |
| 74 | + path := strings.TrimPrefix(r.URL.Path, "/") |
| 75 | + if _, err := static.Open(path); err == nil { |
| 76 | + fileServer.ServeHTTP(w, r) |
75 | 77 | return |
76 | 78 | } |
77 | 79 |
|
| 80 | + // If file does not exist, serve index.html |
78 | 81 | index, err := static.Open("index.html") |
79 | 82 | if err != nil { |
80 | 83 | http.Error(w, "Not found", http.StatusNotFound) |
81 | 84 | return |
82 | 85 | } |
83 | | - |
84 | 86 | defer index.Close() |
85 | 87 |
|
86 | | - io.Copy(w, index) |
87 | | - }) |
88 | | - |
89 | | - fileServer := http.StripPrefix("/", http.FileServer(http.FS(static))) |
90 | | - s.mux.Get("/*", func(w http.ResponseWriter, r *http.Request) { |
91 | | - fileServer.ServeHTTP(w, r) |
| 88 | + if _, err := io.Copy(w, index); err != nil { |
| 89 | + http.Error(w, "Internal server error", http.StatusInternalServerError) |
| 90 | + return |
| 91 | + } |
92 | 92 | }) |
93 | 93 | } |
94 | 94 |
|
|
0 commit comments