docs: redirect documentation files to S3

This commit is contained in:
“Naeel”
2026-09-01 21:16:31 +03:00
parent 35dd57c13b
commit 8bbb5adf21
2 changed files with 6 additions and 27 deletions
+5 -26
View File
@@ -3,12 +3,11 @@ package main
import ( import (
"context" "context"
"fmt" "fmt"
"io"
"log" "log"
"mime"
"net/http" "net/http"
"path/filepath" "path/filepath"
"strings" "strings"
"time"
s3 "github.com/minio/minio-go/v7" s3 "github.com/minio/minio-go/v7"
) )
@@ -76,38 +75,18 @@ func docsHandler(w http.ResponseWriter, r *http.Request) {
var lastErr error var lastErr error
for _, key := range candidates { for _, key := range candidates {
obj, err := s3Client.GetObject(ctx, bucketName, key, s3.GetObjectOptions{}) _, err := s3Client.StatObject(ctx, bucketName, key, s3.GetObjectOptions{})
if err != nil { if err != nil {
lastErr = err lastErr = err
continue continue
} }
stat, err := obj.Stat()
presigned, err := s3Client.PresignedGetObject(ctx, bucketName, key, 15*time.Minute, nil)
if err != nil { if err != nil {
lastErr = err lastErr = err
_ = obj.Close()
continue continue
} }
http.Redirect(w, r, presigned.String(), http.StatusFound)
// Determine content-type
ext := filepath.Ext(key)
ctype := mime.TypeByExtension(ext)
if ctype == "" {
// fallback for HTML
if ext == ".html" || strings.HasSuffix(key, "index.html") {
ctype = "text/html; charset=utf-8"
} else {
ctype = "application/octet-stream"
}
}
w.Header().Set("Content-Type", ctype)
w.Header().Set("Content-Length", fmt.Sprintf("%d", stat.Size))
w.Header().Set("Last-Modified", stat.LastModified.Format(http.TimeFormat))
if _, err := io.Copy(w, obj); err != nil {
log.Printf("Error streaming object %s: %v", key, err)
}
_ = obj.Close()
return return
} }
+1 -1
View File
@@ -24,7 +24,7 @@ var (
s3Prefix = os.Getenv("S3_PREFIX") s3Prefix = os.Getenv("S3_PREFIX")
) )
const VERSION = "0.0.3" const VERSION = "0.0.4"
func main() { func main() {
if hostname == "" { if hostname == "" {