-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
51 lines (39 loc) · 1.21 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
package main
import (
"fmt"
"log"
"net/http"
"os"
)
var archiveExtension = "tar.gz"
var archiveMimeType = "application/gzip"
func download(w http.ResponseWriter, r *http.Request) {
mountPath := os.Getenv("RAILWAY_VOLUME_MOUNT_PATH")
if mountPath == "" {
http.Error(w, "No volume mounted to this service, please mount a volume first.", http.StatusInternalServerError)
return
}
password := r.Header.Get("password")
if password == "" || password != os.Getenv("PASSWORD") {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
volumeName := os.Getenv("RAILWAY_VOLUME_NAME")
fileName := fmt.Sprintf("%s.%s", volumeName, archiveExtension)
fmt.Printf("Volume path: %s\n", mountPath)
fmt.Printf("Volume name: %s\n", volumeName)
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s", fileName))
w.Header().Set("Content-Type", archiveMimeType)
if err := compress(mountPath, w); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func main() {
http.HandleFunc("/", download)
port := os.Getenv("PORT")
if port == "" {
port = "3000"
}
fmt.Printf("Server running at http://localhost:%s\n", port)
log.Fatal(http.ListenAndServe(":" + port, nil))
}