-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
100 lines (84 loc) · 2.66 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
package main
/*
SlicerA
The basic 3D model slicer for 3D printer based on GoSlice open souce project
Author: tobychui
Notes:
In theory all tmp files created by this program will be removed by itself after
program terminate.
*/
import (
"flag"
"log"
"net/http"
"os"
"os/signal"
"path/filepath"
"syscall"
"imuslab.com/SlicerA/mod/aroz"
)
var (
handler *aroz.ArozHandler
tmpFolderPath = flag.String("tmp", "./", "The location to save buffered files. A tmp folder will be created in this path.")
)
//Kill signal handler. Do something before the system the core terminate.
func SetupCloseHandler() {
c := make(chan os.Signal, 2)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
log.Println("\r- Shutting down SlicerA module")
//Clean up the tmp folder if it exists
if fileExists(*tmpFolderPath) {
os.RemoveAll(*tmpFolderPath)
}
os.Exit(0)
}()
}
//Special router to handle js files on Windows 10 invalid mime sissue
func mrouter(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if filepath.Ext(r.RequestURI) == ".js" && fileExists(filepath.Join("./web/", r.RequestURI)) {
//Requesting a js file
w.Header().Add("Content-Type", "text/javascript")
h.ServeHTTP(w, r)
} else {
h.ServeHTTP(w, r)
}
})
}
func main() {
//Start the aoModule pipeline (which will parse the flags as well). Pass in the module launch information
handler = aroz.HandleFlagParse(aroz.ServiceInfo{
Name: "SlicerA",
Desc: "A basic STL 3D model slicer for the ArozOS Cloud Platform",
Group: "Utilities",
IconPath: "SlicerA/img/small_icon.png",
Version: "0.3.6", //Try to match the GoSlice version we are using
StartDir: "SlicerA/index.html",
SupportFW: true,
LaunchFWDir: "SlicerA/index.html",
SupportEmb: true,
LaunchEmb: "SlicerA/index.html",
InitFWSize: []int{1060, 670},
InitEmbSize: []int{1060, 670},
SupportedExt: []string{".stl"},
})
finalTempFolderPath := filepath.Join(*tmpFolderPath, "tmp")
tmpFolderPath = &finalTempFolderPath
//Register the standard web services urls
fs := http.FileServer(http.Dir("./web"))
http.Handle("/", mrouter(fs))
//Handle the slicing process
http.HandleFunc("/slice", handleSlicing)
http.HandleFunc("/sliced", handleSliceAndDispose)
http.HandleFunc("/saveGcode", handleSaveGcode)
//Setup the close handler to handle Ctrl+C on terminal
SetupCloseHandler()
//Any log println will be shown in the core system via STDOUT redirection. But not STDIN.
log.Println("SlicerA started. Listening on " + handler.Port)
err := http.ListenAndServe(handler.Port, nil)
if err != nil {
log.Fatal(err)
}
}