forked from pachyderm/pachyderm
/
http.go
75 lines (64 loc) · 1.79 KB
/
http.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
package server
import (
"fmt"
"io"
"net/http"
"strings"
"github.com/pachyderm/pachyderm/src/client/pfs"
"github.com/julienschmidt/httprouter"
"golang.org/x/net/context"
)
// HTTPPort specifies the port the server will listen on
const HTTPPort = 652
const apiVersion = "v1"
type flushWriter struct {
f http.Flusher
w io.Writer
}
func (fw *flushWriter) Write(p []byte) (n int, err error) {
n, err = fw.w.Write(p)
if fw.f != nil {
fw.f.Flush()
}
return
}
// HTTPServer serves GetFile requests over HTTP
// e.g. http://localhost:30652/v1/pfs/repos/foo/commits/b7a1923be56744f6a3f1525ec222dc3b/files/ttt.log
type HTTPServer struct {
driver *driver
*httprouter.Router
}
func newHTTPServer(address string, etcdAddresses []string, etcdPrefix string, cacheSize int64) (*HTTPServer, error) {
d, err := newDriver(address, etcdAddresses, etcdPrefix, cacheSize)
if err != nil {
return nil, err
}
router := httprouter.New()
s := &HTTPServer{d, router}
router.GET(fmt.Sprintf("/%v/pfs/repos/:repoName/commits/:commitID/files/*filePath", apiVersion), s.getFileHandler)
return s, nil
}
func (s *HTTPServer) getFileHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
pfsFile := &pfs.File{
Commit: &pfs.Commit{
ID: ps.ByName("commitID"),
Repo: &pfs.Repo{
Name: ps.ByName("repoName"),
},
},
Path: ps.ByName("filePath"),
}
filePaths := strings.Split(ps.ByName("filePath"), "/")
fileName := filePaths[len(filePaths)-1]
file, err := s.driver.getFile(context.Background(), pfsFile, 0, 0)
if err != nil {
panic(err)
}
w.Header().Add("Content-Type", "application/octet-stream")
w.Header().Add("Content-Disposition", fmt.Sprintf("attachment; filename=\"%v\"", fileName))
fw := flushWriter{w: w}
if f, ok := w.(http.Flusher); ok {
fw.f = f
}
io.Copy(&fw, file)
}