-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebapi.go
More file actions
120 lines (105 loc) · 3.89 KB
/
webapi.go
File metadata and controls
120 lines (105 loc) · 3.89 KB
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
package webapi
import (
"encoding/json"
"io"
"net/http"
"github.com/chepo92/PrintAndGo/store"
"github.com/chepo92/PrintAndGo/task"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
)
// WebApi is the macro structure of PrintAndGo, implements and manages the server and web API, handling storage, print tasks, serial port and camera.
type WebApi struct {
storage FileStorage
// camera *camera.Camera // camera disabled for windows build
task *task.Task
serialPort func() (io.ReadWriteCloser, error)
SerialPortInfo struct {
Port string
BaudRate int
}
// motd file path
motdFile string
// function we execute if hardware should be shut down.
shutdown func()
Version string
}
type FileStorage interface {
UploadFile(path, filename string, r io.ReadCloser) error
ReadFile(path, filename string) (store.Stream, error)
}
// New creates a new WebApi instance. Starts a new task and returns the instance.
func New(store FileStorage, motdFile string, serial func() (io.ReadWriteCloser, error), shutdown func()) *WebApi {
wapi := &WebApi{
storage: store,
serialPort: serial,
motdFile: motdFile,
shutdown: shutdown,
//camera: camera.New(camdev), // camera disabled for windows build
task: task.New(),
}
return wapi
}
// SetPortBaudRate sets the current port and baud rate
// This is used to update the UI.
func (wapi *WebApi) SetPortBaudRate(port string, baud int) {
wapi.SerialPortInfo.Port = port
wapi.SerialPortInfo.BaudRate = baud
}
// SetVersion sets the current version of PrintAndGo
func (wapi *WebApi) SetVersion(v string) {
wapi.Version = v
}
// Run starts the web server and binds the routes.
// returns the http server handle
func (wapi *WebApi) Run(srv *http.Server) error {
r := chi.NewRouter()
r.Use(middleware.RequestID)
r.Use(middleware.RealIP)
r.Use(middleware.Logger)
r.Get("/", indexPage)
// PrintAndGo api
r.Get("/printandgo/job/status", wapi.pagJobStatus)
r.Post("/printandgo/job/create", wapi.localUpload)
r.Post("/printandgo/job/cancel", wapi.pagJobCancel)
r.Post("/printandgo/device/shutdown", wapi.pagShutdown)
r.Get("/printandgo/gcode/action", wapi.pagEnqueueBuiltin)
// Octoprint fake-compatibility
r.Post("/api/login", octoLoginReply)
r.Get("/api/version", octoVersionReply)
r.Get("/api/server", octoServerReply) // To be implemented
r.Get("/api/connection", wapi.octoGetConnectionReply) // To be implemented
r.Post("/api/connection", octoPostConnectionReply) // To be implemented
r.Post("/api/files/local", wapi.localUpload)
r.Get("/api/job", wapi.octoJobStatus)
r.Post("/api/job", wapi.octoModifyJob)
r.Get("/api/languages", octoLanguagesReply) // To be implemented
r.Get("/api/printer", octoPrinterReplyFake)
r.Post("/api/printer/command", wapi.octoPrinterCommand)
r.Get("/api/printerprofiles", octoPrinterProfilesReply) // To be implemented
r.Get("/api/settings", octoSettingsReply)
r.Get("/api/slicing", octoSlicingReply) // To be implemented
r.Get("/api/system/commands", octoSystemCommandsReply) // To be implemented
r.Get("/api/timelapse", octoTimelapseReply) // To be implemented
r.Get("/api/access", octoAccessReply) // To be implemented
r.Get("/api/util/test", octoUtilTestReply) // To be implemented
r.Get("/setup/wizard", octoSetupWizardReply) // To be implemented
// Camera support
//r.Get("/camera", cameraPage) // camera disabled for windows build
//r.Get("/camera/stream.mjpeg", wapi.camera.WriteStream) // camera disabled for windows build
srv.Handler = r
return srv.ListenAndServe()
}
func jsonWrite(w http.ResponseWriter, msg interface{}) {
pl, err := json.Marshal(msg)
if err != nil {
http.Error(w, "marshalling error", 503)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(pl)
}
func (wapi *WebApi) pagShutdown(w http.ResponseWriter, r *http.Request) {
wapi.shutdown()
jsonWrite(w, nil)
}