-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
93 lines (74 loc) · 1.8 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
/* This file is part of artifact-local.
* Copyright 2018- Rahul De
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
*/
package main
import (
"fmt"
"io"
"net/http"
"os"
"path/filepath"
)
const DIR_NAME = "artifacts"
func ping(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Ack")
}
func receive(w http.ResponseWriter, r *http.Request) {
dir := filepath.Join(
DIR_NAME,
r.PathValue("group"),
r.PathValue("name"),
r.PathValue("runId"),
)
os.MkdirAll(dir, os.ModePerm)
artifact, err := os.Create(filepath.Join(dir, r.PathValue("artifact")))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, err.Error())
return
}
defer artifact.Close()
_, err = io.Copy(artifact, r.Body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, err.Error())
return
}
fmt.Fprint(w, "Ok")
}
func delete(w http.ResponseWriter, r *http.Request) {
artifact := filepath.Join(
DIR_NAME,
r.PathValue("group"),
r.PathValue("name"),
r.PathValue("runId"),
r.PathValue("artifact"),
)
err := os.Remove(artifact)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, err.Error())
return
}
fmt.Fprint(w, "Ok")
}
func send(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, filepath.Join(DIR_NAME, r.PathValue("artifactPath")))
}
func main() {
port, exists := os.LookupEnv("PORT")
if !exists {
port = "8001"
}
mux := http.NewServeMux()
path := "/bob_artifact/{group}/{name}/{runId}/{artifact}"
mux.HandleFunc("GET /ping", ping)
mux.HandleFunc("POST "+path, receive)
mux.HandleFunc("DELETE "+path, delete)
mux.HandleFunc("GET /bob_artifact/{artifactPath...}", send)
http.ListenAndServe(":"+port, mux)
}