check /index.html for subpaths that 404
Some checks failed
/ build-container (push) Has been cancelled

This commit is contained in:
Finn 2024-04-08 13:21:27 -07:00
parent 76697e2682
commit 7212e51d1b

View file

@ -17,8 +17,10 @@
package main package main
import ( import (
"context"
"io" "io"
"net/http" "net/http"
"strings"
minio "github.com/minio/minio-go/v7" minio "github.com/minio/minio-go/v7"
"golang.org/x/exp/slog" "golang.org/x/exp/slog"
@ -41,18 +43,8 @@ type handler struct {
func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
domain := r.Host domain := r.Host
path := r.URL.Path path := r.URL.Path
if path == "" || path == "/" {
path = "index.html"
}
object, err := h.minio.GetObject(r.Context(), domain, path, minio.GetObjectOptions{}) object, stat, err := h.get(r.Context(), domain, path)
if err != nil {
slog.Warn("error getting object from s3", "error", err, "domain", domain, "path", path)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
stat, err := object.Stat()
if err != nil { if err != nil {
resp := minio.ToErrorResponse(err) resp := minio.ToErrorResponse(err)
if resp.StatusCode == http.StatusNotFound { if resp.StatusCode == http.StatusNotFound {
@ -62,9 +54,8 @@ func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
slog.Info("served 404", "domain", domain, "path", path) slog.Info("served 404", "domain", domain, "path", path)
return return
} }
slog.Error("error getting object from storage", "bucket", domain, "object", path, "err", err)
slog.Warn("failed to stat object", "err", err, "bucket", domain, "path", path) http.Error(w, "internal server error", http.StatusInternalServerError)
http.Error(w, err.Error(), http.StatusInternalServerError)
return return
} }
@ -79,3 +70,29 @@ func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
slog.Info("served request", "domain", domain, "path", path, "size_bytes", n) slog.Info("served request", "domain", domain, "path", path, "size_bytes", n)
} }
func (h handler) get(ctx context.Context, bucket string, path string) (*minio.Object, *minio.ObjectInfo, error) {
object, err := h.minio.GetObject(ctx, bucket, path, minio.GetObjectOptions{})
if err != nil {
return nil, nil, err
}
stat, err := object.Stat()
if err != nil {
resp := minio.ToErrorResponse(err)
if resp.StatusCode == http.StatusNotFound {
if strings.HasSuffix(path, "/index.html") {
return nil, nil, err
}
if !strings.HasSuffix(path, "/") {
path = path + "/"
}
path = path + "index.html"
return h.get(ctx, bucket, path)
}
return nil, nil, err
}
return object, &stat, nil
}