-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathfetcher.go
More file actions
73 lines (65 loc) · 1.73 KB
/
fetcher.go
File metadata and controls
73 lines (65 loc) · 1.73 KB
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
package fetcher
import (
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"github.com/gorilla/mux"
)
const (
appdirectory = "/home/git/"
slugdirectory = "/apps/"
cmdstring = "/tmp/builder/build.sh"
)
// Serve will start the fetcher server and block until it stops. Since it blocks, it's a best practice to execute this func in a goroutine.
func Serve(port int) {
rtr := mux.NewRouter()
rtr.HandleFunc("/git/home/{name}/tar", getTar).Methods("GET")
rtr.HandleFunc("/git/home/{name}/slug", getSlug).Methods("GET")
rtr.HandleFunc("/git/home/health", health).Methods("GET")
rtr.HandleFunc("/git/home/{name}/push", putSlug).Methods("PUT")
hostStr := fmt.Sprintf(":%d", port)
http.ListenAndServe(hostStr, rtr)
}
func getTar(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
name := strings.Split(params["name"], ":")[0]
dat, err := ioutil.ReadFile(appdirectory + name + ".git/" + name + ".tar.gz")
if err != nil {
w.Write([]byte(name + "dosn't exist"))
}
w.Write(dat)
}
func health(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello, world!")
}
func getSlug(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
name := params["name"]
dat, err := ioutil.ReadFile(slugdirectory + name + "/slug.tgz")
if err != nil {
w.Write([]byte(name + "dosn't exist"))
}
w.Write(dat)
}
func putSlug(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
name := params["name"]
log.Println(name)
err := os.MkdirAll(slugdirectory+name, 0755)
if err != nil {
fmt.Println(err)
}
output, err := os.Create(slugdirectory + name + "/slug.tgz")
if err != nil {
fmt.Println(err)
}
defer output.Close()
defer r.Body.Close()
fmt.Println(r.ContentLength)
io.Copy(output, r.Body)
return
}