2021-11-29 20:48:45 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2023-03-18 16:38:05 +00:00
|
|
|
|
|
|
|
"github.com/Luzifer/go-latestver/internal/helpers"
|
2021-11-29 20:48:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func handleSinglePage(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// get the absolute path to prevent directory traversal
|
|
|
|
urlPath, err := filepath.Abs(r.URL.Path)
|
|
|
|
if err != nil {
|
|
|
|
// if we failed to get the absolute path respond with a 400 bad request
|
|
|
|
// and stop
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-18 16:38:05 +00:00
|
|
|
for _, frontendPath := range []string{
|
2021-11-29 20:48:45 +00:00
|
|
|
filepath.Join("frontend", path.Base(urlPath)),
|
|
|
|
filepath.Join("frontend", "index.html"),
|
|
|
|
} {
|
2023-03-18 16:38:05 +00:00
|
|
|
f, err := frontendFS.Open(frontendPath)
|
2021-11-29 20:48:45 +00:00
|
|
|
switch {
|
|
|
|
case err == nil:
|
|
|
|
// file is opened, serve it
|
2023-03-18 16:38:05 +00:00
|
|
|
defer func() { helpers.LogIfErr(f.Close(), "closing frontend file after serve") }() //revive:disable-line:defer Fine here as it will only open one file
|
2021-11-29 20:48:45 +00:00
|
|
|
|
|
|
|
stat, err := f.Stat()
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, errors.Wrap(err, "stating opened file").Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if stat.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
http.ServeContent(w, r, stat.Name(), stat.ModTime(), f)
|
|
|
|
return
|
|
|
|
|
|
|
|
case os.IsNotExist(err):
|
|
|
|
// file does not exist, try next
|
|
|
|
continue
|
|
|
|
|
|
|
|
default:
|
|
|
|
// if we got an error (that wasn't that the file doesn't exist) stating the
|
|
|
|
// file, return a 500 internal server error and stop
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// No path could be opened
|
|
|
|
http.NotFound(w, r)
|
|
|
|
}
|