-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathoctoprint.go
229 lines (199 loc) · 5.68 KB
/
octoprint.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package main
import (
"bytes"
"fmt"
"log"
"net"
"net/http"
"runtime"
"strings"
"time"
)
const (
maxMemory = 64 << 20 // 64MB
)
var (
noTrim = false
noShutoff = false
// noPreheat = false
// noReinforceTower = false
noReplaceTool = false
)
type stats struct {
start time.Time
memory uint64
success uint
failure uint
lastSuccess *last
lastFailure *last
}
type last struct {
filaname string
size int64
time time.Time
}
func (s *stats) addSuccess(filaname string, size int64) {
s.success++
s.lastSuccess = &last{
filaname: normalizedFilename(filaname),
size: size,
time: time.Now(),
}
}
func (s *stats) addFailure(filaname string, size int64) {
s.failure++
s.lastFailure = &last{
filaname: normalizedFilename(filaname),
size: size,
time: time.Now(),
}
}
func (s *stats) String() string {
var mem runtime.MemStats
runtime.ReadMemStats(&mem)
s.memory = mem.Alloc
buf := bytes.Buffer{}
buf.WriteString("memory alloc: " + humanReadableSize(int64(s.memory)) + "\n")
buf.WriteString("uptime: " + time.Since(s.start).String() + "\n")
buf.WriteString(fmt.Sprintf("success: %d, failure: %d\n", s.success, s.failure))
buf.WriteString(fmt.Sprintf("last success: %s\n - %s (%s)\n", s.lastSuccess.time.Format(time.RFC3339), s.lastSuccess.filaname, humanReadableSize(s.lastSuccess.size)))
buf.WriteString(fmt.Sprintf("last failure: %s\n - %s (%s)\n", s.lastFailure.time.Format(time.RFC3339), s.lastFailure.filaname, humanReadableSize(s.lastFailure.size)))
return buf.String()
}
func LoggingMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
defer func() {
log.Printf("Request %s %s completed in %v", r.Method, r.URL.Path, time.Since(start))
}()
next.ServeHTTP(w, r)
})
}
func startOctoPrintServer(listenAddr string, printer *Printer) error {
var (
_stats *stats
mux = http.NewServeMux()
)
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
protocol := "HTTP"
if printer.Sacp {
protocol = "SACP"
}
resp := `sm2uploader ` + Version + ` - https://github.com/macdylan/sm2uploader` + "\n\n" +
` printer id: ` + printer.ID + "\n" +
` printer ip: ` + printer.IP + "\n" +
` protocol: ` + protocol + "\n\n" +
_stats.String()
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
writeResponse(w, http.StatusOK, resp)
})
mux.HandleFunc("/api/version", func(w http.ResponseWriter, r *http.Request) {
respVersion := `{"api": "0.1", "server": "1.2.3", "text": "OctoPrint 1.2.3/Dummy"}`
writeResponse(w, http.StatusOK, respVersion)
})
mux.HandleFunc("/api/files/local", func(w http.ResponseWriter, r *http.Request) {
// Check if request is a POST request
if r.Method != http.MethodPost {
methodNotAllowedResponse(w, r.Method)
return
}
err := r.ParseMultipartForm(maxMemory)
if err != nil {
internalServerErrorResponse(w, err.Error())
return
}
// Retrieve the uploaded file
file, fd, err := r.FormFile("file")
if err != nil {
bedRequestResponse(w, err.Error())
return
}
defer file.Close()
// read X-Api-Key header
apiKey := r.Header.Get("X-Api-Key")
if len(apiKey) > 5 {
argumentsFromApi(apiKey)
}
// Send the stream to the printer
payload := NewPayload(file, fd.Filename, fd.Size)
if err := Connector.Upload(printer, payload); err != nil {
_stats.addFailure(payload.Name, payload.Size)
internalServerErrorResponse(w, err.Error())
return
}
_stats.addSuccess(payload.Name, payload.Size)
log.Printf("Upload finished: %s [%s]", fd.Filename, payload.ReadableSize())
// Return success response
writeResponse(w, http.StatusOK, `{"done": true}`)
})
handler := LoggingMiddleware(mux)
log.Printf("Starting OctoPrint server on %s ...", listenAddr)
// Create a listener
listener, err := net.Listen("tcp", listenAddr)
if err != nil {
return err
}
_stats = &stats{
start: time.Now(),
success: 0,
failure: 0,
lastSuccess: &last{
filaname: "",
size: 0,
time: time.Now(),
},
lastFailure: &last{
filaname: "",
size: 0,
time: time.Now(),
},
}
log.Printf("Server started, now you can upload files to http://%s", listener.Addr().String())
// Start the server
return http.Serve(listener, handler)
}
func writeResponse(w http.ResponseWriter, status int, body string) {
if has := w.Header().Get("Content-Type"); has == "" {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
}
w.WriteHeader(status)
w.Write([]byte(body))
}
func methodNotAllowedResponse(w http.ResponseWriter, method string) {
log.Print("Method not allowed: ", method)
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
}
func internalServerErrorResponse(w http.ResponseWriter, err string) {
log.Print("Internal server error: ", err)
http.Error(w, err, http.StatusInternalServerError)
}
func bedRequestResponse(w http.ResponseWriter, err string) {
log.Print("Bad request: ", err)
http.Error(w, err, http.StatusBadRequest)
}
func argumentsFromApi(str string) {
noTrim = strings.Contains(str, "notrim")
// noPreheat = strings.Contains(str, "nopreheat")
noShutoff = strings.Contains(str, "noshutoff")
// noReinforceTower = strings.Contains(str, "noreinforcetower")
noReplaceTool = strings.Contains(str, "noreplacetool")
msg := []string{}
if noTrim {
msg = append(msg, "-notrim")
}
// if noPreheat {
// msg = append(msg, "-nopreheat")
// }
if noShutoff {
msg = append(msg, "-noshutoff")
}
// if noReinforceTower {
// msg = append(msg, "-noreinforcetower")
// }
if noReplaceTool {
msg = append(msg, "-noreplacetool")
}
if len(msg) > 0 {
log.Printf("SMFix with args: %s", strings.Join(msg, " "))
}
}