-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
76 lines (60 loc) · 1.64 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
package main
// https://www.flaticon.com/free-icon/dog_1402202
import (
"io"
"log"
"net/http"
"strconv"
"time"
"github.com/gorilla/mux"
minio "github.com/minio/minio-go"
)
var store = NewStore()
func main() {
r := mux.NewRouter()
r.HandleFunc("/{reponame}/{path:.*}", TestHandler)
srv := &http.Server{
Handler: r,
Addr: "127.0.0.1:8000",
// Good practice: enforce timeouts for servers you create!
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
srv.ListenAndServe()
}
func GetArtifact(path string, writer http.ResponseWriter) {
object, err := store.Client.GetObject("mymusic", path, minio.GetObjectOptions{})
if err != nil {
writer.WriteHeader(http.StatusNotFound)
return
}
defer object.Close()
objInfo, err := object.Stat()
if err != nil {
writer.WriteHeader(http.StatusNotFound)
return
}
writer.Header().Add("Content-Type", "application/octet-stream")
writer.Header().Add("Content-Length", strconv.FormatInt(objInfo.Size, 10))
writer.WriteHeader(http.StatusOK)
io.Copy(writer, object)
}
func UploadArtifact(path string, r *http.Request, writer http.ResponseWriter) {
defer r.Body.Close()
_, err := store.Client.PutObject("mymusic", path, r.Body, r.ContentLength, minio.PutObjectOptions{})
if err != nil {
writer.WriteHeader(http.StatusBadRequest)
return
}
writer.WriteHeader(http.StatusOK)
}
func TestHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
if r.Method == "GET" {
GetArtifact(vars["path"], w)
} else if r.Method == "PUT" || r.Method == "POST" {
log.Printf("request: %v", r)
UploadArtifact(vars["path"], r, w)
}
log.Printf("Method: %s", r.Method)
}