docs: redirect documentation files to S3
This commit is contained in:
+5
-26
@@ -3,12 +3,11 @@ package main
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"mime"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
s3 "github.com/minio/minio-go/v7"
|
||||
)
|
||||
@@ -76,38 +75,18 @@ func docsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
var lastErr error
|
||||
for _, key := range candidates {
|
||||
obj, err := s3Client.GetObject(ctx, bucketName, key, s3.GetObjectOptions{})
|
||||
_, err := s3Client.StatObject(ctx, bucketName, key, s3.GetObjectOptions{})
|
||||
if err != nil {
|
||||
lastErr = err
|
||||
continue
|
||||
}
|
||||
stat, err := obj.Stat()
|
||||
|
||||
presigned, err := s3Client.PresignedGetObject(ctx, bucketName, key, 15*time.Minute, nil)
|
||||
if err != nil {
|
||||
lastErr = err
|
||||
_ = obj.Close()
|
||||
continue
|
||||
}
|
||||
|
||||
// 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()
|
||||
http.Redirect(w, r, presigned.String(), http.StatusFound)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -24,7 +24,7 @@ var (
|
||||
s3Prefix = os.Getenv("S3_PREFIX")
|
||||
)
|
||||
|
||||
const VERSION = "0.0.3"
|
||||
const VERSION = "0.0.4"
|
||||
|
||||
func main() {
|
||||
if hostname == "" {
|
||||
|
||||
Reference in New Issue
Block a user