-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The OctoPrint server is simulated. See
-octoprint
- Loading branch information
Showing
7 changed files
with
181 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package main | ||
|
||
import ( | ||
"io" | ||
"log" | ||
"net" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
func LoggingMiddleware(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
start := time.Now() | ||
next.ServeHTTP(w, r) | ||
// Log the request | ||
log.Printf("Request %s %s completed in %v", r.Method, r.URL.Path, time.Since(start)) | ||
}) | ||
} | ||
|
||
func startOctoPrintServer(listenAddr string, printer *Printer) error { | ||
mux := http.NewServeMux() | ||
mux.HandleFunc("/api/version", func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
w.Write([]byte(`{"api": "0.1", "server": "1.2.3", "text": "OctoPrint 1.2.3/Dummy"}`)) | ||
}) | ||
|
||
mux.HandleFunc("/api/files/local", func(w http.ResponseWriter, r *http.Request) { | ||
// Check if request is a POST request | ||
if r.Method != http.MethodPost { | ||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) | ||
return | ||
} | ||
|
||
// Retrieve the uploaded file | ||
file, fd, err := r.FormFile("file") | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusBadRequest) | ||
return | ||
} | ||
defer file.Close() | ||
fd.Filename = normalizedFilename(fd.Filename) | ||
|
||
// Send the stream to the printer | ||
content, _ := io.ReadAll(file) | ||
if err := Connector.Upload(printer, fd.Filename, content); err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
log.Printf("Upload finished: %s [%s]", fd.Filename, humanReadableSize(int64(len(content)))) | ||
|
||
// Return success response | ||
w.WriteHeader(http.StatusOK) | ||
}) | ||
|
||
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 | ||
} | ||
|
||
log.Printf("Server started, now you can upload files to http://localhost:%s", listenAddr) | ||
|
||
// Start the server | ||
return http.Serve(listener, handler) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
) | ||
|
||
func humanReadableSize(size int64) string { | ||
const unit = 1024 | ||
if size < unit { | ||
return fmt.Sprintf("%d B", size) | ||
} | ||
div, exp := int64(unit), 0 | ||
for n := size / unit; n >= unit; n /= unit { | ||
div *= unit | ||
exp++ | ||
} | ||
return fmt.Sprintf("%.1f %cB", float64(size)/float64(div), "KMGTPE"[exp]) | ||
} | ||
|
||
var reFilename = regexp.MustCompile(`^[\.\/\\~]+`) | ||
|
||
func normalizedFilename(filename string) string { | ||
return reFilename.ReplaceAllString(filename, "") | ||
} |