-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
134 lines (110 loc) · 2.63 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"context"
"fmt"
"log"
"net/http"
"net/url"
"os"
"path"
"time"
"github.com/google/uuid"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
)
var ETAG = uuid.New().String()
func StaticHandler(dir string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
r.Header.Del("If-Modified-Since")
w.Header().Set("Etag", ETAG)
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
if path.Ext(r.URL.Path) == "" {
http.ServeFile(w, r, "/dist/index.html")
return
}
http.FileServer(http.Dir(dir)).ServeHTTP(w, r)
}
}
var s3 *minio.Client
func SetS3Client() {
port := os.Getenv(("S3_PORT"))
endpoint := os.Getenv("S3_ENDPOINT")
accessKeyID := os.Getenv("S3_LOGIN")
secretAccessKey := os.Getenv("S3_PASS")
useSSL := true
if port == "" {
port = "9000"
}
if endpoint == "" {
return
}
url := fmt.Sprintf("%s:%s", endpoint, port)
s3Client, err := minio.New(url, &minio.Options{
Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
Secure: useSSL,
})
if err != nil {
log.Fatal(err)
}
s3 = s3Client
}
func GetPresignedUrl(w http.ResponseWriter, r *http.Request) {
ctx := context.Background()
bucket := r.URL.Query().Get("bucket")
name := r.URL.Query().Get("name")
expiry := time.Second * 24 * 60 * 60
var (
presignedUrl *url.URL
err error
)
method := r.URL.Query().Get("method")
if !(method == "GET" || method == "PUT") {
log.Fatal("Wrong method:", method)
}
if method == "GET" {
presignedUrl, err = s3.PresignedGetObject(ctx, bucket, name, expiry*7, url.Values{})
}
if method == "PUT" {
presignedUrl, err = s3.PresignedPutObject(ctx, bucket, name, expiry)
}
if err != nil {
log.Fatal(err)
}
w.Write([]byte(presignedUrl.String()))
}
func main() {
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
dist := os.Getenv("DIST")
if dist == "" {
dist = "/dist"
}
id := os.Getenv("GA_ID")
bucket := os.Getenv(("S3_BUCKET"))
api := os.Getenv("BASE_URL")
if api == "" {
log.Fatal("BASE_URL not set")
}
s := fmt.Sprintf(
"globalThis.VUE_APP_BASE_URL = '%s'\nglobalThis.VUE_APP_GA_ID = '%s'\nglobalThis.VUE_APP_S3_BUCKET = '%s'",
api, id, bucket,
)
err := os.WriteFile(path.Join(dist, "api.js"), []byte(s), 0644)
if err != nil {
log.Fatal(err)
}
SetS3Client()
mux := http.NewServeMux()
mux.Handle("/", StaticHandler(dist))
mux.HandleFunc("/presignedUrl", GetPresignedUrl)
log.Printf("Listening on port %s, serving: %s, api: %s", port, dist, api)
err = http.ListenAndServe(":"+port, mux)
if err != nil {
log.Fatal(err)
}
}