-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
138 lines (110 loc) · 2.67 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
135
136
137
138
/*
* 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 (
"archive/tar"
"crypto/tls"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/transport/client"
githttp "github.com/go-git/go-git/v5/plumbing/transport/http"
)
func makeTar(src string, archiveName string) error {
if _, err := os.Stat(src); err != nil {
return err
}
tarfile, err := os.Create(archiveName)
if err != nil {
return err
}
defer tarfile.Close()
tw := tar.NewWriter(tarfile)
defer tw.Close()
err = filepath.Walk(src, func(file string, fi os.FileInfo, err error) error {
if err != nil {
return err
}
if !fi.Mode().IsRegular() {
return nil
}
header, err := tar.FileInfoHeader(fi, fi.Name())
if err != nil {
return err
}
header.Name = strings.TrimPrefix(strings.Replace(file, src, "", -1), string(filepath.Separator))
if err := tw.WriteHeader(header); err != nil {
return err
}
f, err := os.Open(file)
if err != nil {
return err
}
if _, err := io.Copy(tw, f); err != nil {
return err
}
f.Close()
return nil
})
return nil
}
func ping(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Ack")
}
func clone(w http.ResponseWriter, r *http.Request) {
repo := r.URL.Query().Get("repo")
branch := r.URL.Query().Get("branch")
if repo == "" || branch == "" {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, "Invalid params: both repo and branch required.")
return
}
archive := fmt.Sprintf("%d", time.Now().UnixNano())
dir := "repo-" + archive
os.MkdirAll(dir, os.ModePerm)
defer os.RemoveAll(dir)
defer os.Remove(archive)
_, err := git.PlainClone(dir, false, &git.CloneOptions{
URL: repo,
ReferenceName: plumbing.ReferenceName("refs/heads/" + branch),
SingleBranch: true,
Depth: 1,
})
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, err.Error())
return
}
if err := makeTar(dir, archive); err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, err.Error())
return
}
http.ServeFile(w, r, archive)
}
func main() {
port, exists := os.LookupEnv("PORT")
if !exists {
port = "8000"
}
customClient := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
client.InstallProtocol("https", githttp.NewClient(customClient))
http.HandleFunc("GET /ping", ping)
http.HandleFunc("GET /bob_resource", clone)
http.ListenAndServe(":"+port, nil)
}