-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
171 lines (145 loc) · 3.74 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main
import (
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
)
func main() {
// TODO custom port
http.Handle("/", &HTTPFSHandler{})
log.Println("Listening on :8080...")
log.Fatal(http.ListenAndServe(":8080", nil))
}
// HTTPFS represents a httpfs handler
type HTTPFSHandler struct {
path string
err error
isFile bool
}
// ServeHTTP satisfies http.Handler interface
func (h *HTTPFSHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// build file/dir path
h.path = fmt.Sprintf("%s%s", UserHomeDir(), r.URL)
h.err = nil
// find if path is file or directory
isFile, err := IsFile(h.path)
if err != nil {
h.err = err
}
h.isFile = isFile
switch {
case r.Method == "GET":
// cat and ls
h.HandleGet(w, r)
return
case r.Method == "POST":
// touch and mkdir
h.HandlePost(w, r)
return
case r.Method == "PUT":
// echo
h.HandlePut(w, r)
return
case r.Method == "DELETE":
// rm
// TODO
return
}
}
// Handlers
// HandleGet handles GET requests
func (h *HTTPFSHandler) HandleGet(w http.ResponseWriter, r *http.Request) {
if h.isFile {
h.CatFile(w, r)
return
}
h.LsDir(w, r)
}
// HandlePost handles POST requests
func (h *HTTPFSHandler) HandlePost(w http.ResponseWriter, r *http.Request) {
// if file exists we exit
if h.err == nil {
h.err = errors.New("file/dir exists")
h.HandleConflict(w, r)
return
}
// parse form looking for type
r.ParseForm()
t := r.PostFormValue("type")
if t == "" {
h.err = errors.New("type missing")
h.HandleBadRequest(w, r)
return
}
if t == "file" {
h.TouchFile(w, r)
return
}
if t == "dir" {
// TODO
return
}
h.err = errors.New("wrong type")
h.HandleBadRequest(w, r)
}
// HandlePut handles PUT requests
func (h *HTTPFSHandler) HandlePut(w http.ResponseWriter, r *http.Request) {
// TODO
}
// HandleNotFound handles NotFound scenarios
func (h *HTTPFSHandler) HandleNotFound(w http.ResponseWriter, r *http.Request) {
log.Printf("path=%s,method=%s,response_code=%d,err=%s", h.path, r.Method, http.StatusNotFound, h.err)
http.NotFound(w, r)
}
// HandleConflict handles Conflict scenarios
func (h *HTTPFSHandler) HandleConflict(w http.ResponseWriter, r *http.Request) {
log.Printf("path=%s,method=%s,response_code=%d,err=%s", h.path, r.Method, http.StatusConflict, h.err)
w.WriteHeader(http.StatusConflict)
io.WriteString(w, "conflict\n")
}
// HandleBadRequest handles BadRequest scenarios
func (h *HTTPFSHandler) HandleBadRequest(w http.ResponseWriter, r *http.Request) {
log.Printf("path=%s,method=%s,response_code=%d,err=%s", h.path, r.Method, http.StatusBadRequest, h.err)
w.WriteHeader(http.StatusConflict)
io.WriteString(w, "bad request\n")
}
// UNIX calls
// CatFile reproduces `cat $file_path` unix operation
func (h *HTTPFSHandler) CatFile(w http.ResponseWriter, r *http.Request) {
b, err := ioutil.ReadFile(h.path)
if err != nil {
h.err = err
h.HandleNotFound(w, r)
return
}
log.Printf("path=%s,method=%s,unix_op=cat %s,response_code=%d", h.path, r.Method, h.path, http.StatusOK)
_, _ = w.Write(b)
}
// LsDir reproduces `ls $dir_path` unix operation
func (h *HTTPFSHandler) LsDir(w http.ResponseWriter, r *http.Request) {
files, err := ioutil.ReadDir(h.path)
if err != nil {
h.err = err
h.HandleNotFound(w, r)
return
}
for _, file := range files {
line := fmt.Sprintf("%s\n", file.Name())
w.Write([]byte(line))
}
}
// TouchFile reproduces `touch $file_path` unix operation
func (h *HTTPFSHandler) TouchFile(w http.ResponseWriter, r *http.Request) {
_, err := os.OpenFile(h.path, os.O_RDONLY|os.O_CREATE, 0644)
if err != nil {
h.err = err
h.HandleConflict(w, r)
return
}
log.Printf("path=%s,method=%s,unix_op=touch %s,response_code=%d", h.path, r.Method, h.path, http.StatusCreated)
w.WriteHeader(http.StatusCreated)
}