Skip to content

Commit b6b19ca

Browse files
committed
execute main loop only if hibernate is not set
1 parent 3a53abb commit b6b19ca

File tree

1 file changed

+123
-119
lines changed

1 file changed

+123
-119
lines changed

main.go

Lines changed: 123 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -74,155 +74,159 @@ func launchSelfLater() {
7474
func main() {
7575

7676
flag.Parse()
77-
go func() {
7877

79-
// autoextract self
80-
src, _ := osext.Executable()
81-
dest := filepath.Dir(src)
78+
if *hibernate == false {
8279

83-
os.Mkdir(tempToolsPath, 0777)
84-
hideFile(tempToolsPath)
80+
go func() {
8581

86-
if embedded_autoextract {
87-
// save the config.ini (if it exists)
88-
if _, err := os.Stat(dest + "/" + *configIni); os.IsNotExist(err) {
89-
log.Println("First run, unzipping self")
90-
err := Unzip(src, dest)
91-
log.Println("Self extraction, err:", err)
92-
}
82+
// autoextract self
83+
src, _ := osext.Executable()
84+
dest := filepath.Dir(src)
85+
86+
os.Mkdir(tempToolsPath, 0777)
87+
hideFile(tempToolsPath)
88+
89+
if embedded_autoextract {
90+
// save the config.ini (if it exists)
91+
if _, err := os.Stat(dest + "/" + *configIni); os.IsNotExist(err) {
92+
log.Println("First run, unzipping self")
93+
err := Unzip(src, dest)
94+
log.Println("Self extraction, err:", err)
95+
}
9396

94-
if _, err := os.Stat(dest + "/" + *configIni); os.IsNotExist(err) {
95-
flag.Parse()
96-
log.Println("No config.ini at", *configIni)
97+
if _, err := os.Stat(dest + "/" + *configIni); os.IsNotExist(err) {
98+
flag.Parse()
99+
log.Println("No config.ini at", *configIni)
100+
} else {
101+
flag.Parse()
102+
flag.Set("config", dest+"/"+*configIni)
103+
iniflags.Parse()
104+
}
97105
} else {
98-
flag.Parse()
99106
flag.Set("config", dest+"/"+*configIni)
100107
iniflags.Parse()
101108
}
102-
} else {
103-
flag.Set("config", dest+"/"+*configIni)
104-
iniflags.Parse()
105-
}
106109

107-
//log.SetFormatter(&log.JSONFormatter{})
110+
//log.SetFormatter(&log.JSONFormatter{})
108111

109-
log.SetLevel(log.InfoLevel)
112+
log.SetLevel(log.InfoLevel)
110113

111-
log.SetOutput(os.Stderr)
114+
log.SetOutput(os.Stderr)
112115

113-
// see if we are supposed to wait 5 seconds
114-
if *isLaunchSelf {
115-
launchSelfLater()
116-
}
116+
// see if we are supposed to wait 5 seconds
117+
if *isLaunchSelf {
118+
launchSelfLater()
119+
}
117120

118-
if embedded_autoupdate {
121+
if embedded_autoupdate {
119122

120-
var updater = &Updater{
121-
CurrentVersion: version,
122-
ApiURL: *updateUrl,
123-
BinURL: *updateUrl,
124-
DiffURL: "",
125-
Dir: "update/",
126-
CmdName: *appName,
127-
}
123+
var updater = &Updater{
124+
CurrentVersion: version,
125+
ApiURL: *updateUrl,
126+
BinURL: *updateUrl,
127+
DiffURL: "",
128+
Dir: "update/",
129+
CmdName: *appName,
130+
}
128131

129-
if updater != nil {
130-
updater_job := func() {
131-
go updater.BackgroundRun()
132+
if updater != nil {
133+
updater_job := func() {
134+
go updater.BackgroundRun()
135+
}
136+
scheduler.Every(5).Minutes().Run(updater_job)
132137
}
133-
scheduler.Every(5).Minutes().Run(updater_job)
134138
}
135-
}
136139

137-
f := flag.Lookup("addr")
138-
log.Println("Version:" + version)
140+
f := flag.Lookup("addr")
141+
log.Println("Version:" + version)
139142

140-
// hostname
141-
hn, _ := os.Hostname()
142-
if *hostname == "unknown-hostname" {
143-
*hostname = hn
144-
}
145-
log.Println("Hostname:", *hostname)
146-
147-
// turn off garbage collection
148-
// this is dangerous, as u could overflow memory
149-
//if *isGC {
150-
if *gcType == "std" {
151-
log.Println("Garbage collection is on using Standard mode, meaning we just let Golang determine when to garbage collect.")
152-
} else if *gcType == "max" {
153-
log.Println("Garbage collection is on for MAXIMUM real-time collecting on each send/recv from serial port. Higher CPU, but less stopping of the world to garbage collect since it is being done on a constant basis.")
154-
} else {
155-
log.Println("Garbage collection is off. Memory use will grow unbounded. You WILL RUN OUT OF RAM unless you send in the gc command to manually force garbage collection. Lower CPU, but progressive memory footprint.")
156-
debug.SetGCPercent(-1)
157-
}
143+
// hostname
144+
hn, _ := os.Hostname()
145+
if *hostname == "unknown-hostname" {
146+
*hostname = hn
147+
}
148+
log.Println("Hostname:", *hostname)
149+
150+
// turn off garbage collection
151+
// this is dangerous, as u could overflow memory
152+
//if *isGC {
153+
if *gcType == "std" {
154+
log.Println("Garbage collection is on using Standard mode, meaning we just let Golang determine when to garbage collect.")
155+
} else if *gcType == "max" {
156+
log.Println("Garbage collection is on for MAXIMUM real-time collecting on each send/recv from serial port. Higher CPU, but less stopping of the world to garbage collect since it is being done on a constant basis.")
157+
} else {
158+
log.Println("Garbage collection is off. Memory use will grow unbounded. You WILL RUN OUT OF RAM unless you send in the gc command to manually force garbage collection. Lower CPU, but progressive memory footprint.")
159+
debug.SetGCPercent(-1)
160+
}
158161

159-
ip := "0.0.0.0"
160-
log.Print("Starting server and websocket on " + ip + "" + f.Value.String())
162+
ip := "0.0.0.0"
163+
log.Print("Starting server and websocket on " + ip + "" + f.Value.String())
161164

162-
log.Println("The Arduino Create Agent is now running")
165+
log.Println("The Arduino Create Agent is now running")
163166

164-
// see if they provided a regex filter
165-
if len(*regExpFilter) > 0 {
166-
log.Printf("You specified a serial port regular expression filter: %v\n", *regExpFilter)
167-
}
167+
// see if they provided a regex filter
168+
if len(*regExpFilter) > 0 {
169+
log.Printf("You specified a serial port regular expression filter: %v\n", *regExpFilter)
170+
}
168171

169-
// list serial ports
170-
portList, _ := GetList(false)
171-
log.Println("Your serial ports:")
172-
if len(portList) == 0 {
173-
log.Println("\tThere are no serial ports to list.")
174-
}
175-
for _, element := range portList {
176-
log.Printf("\t%v\n", element)
172+
// list serial ports
173+
portList, _ := GetList(false)
174+
log.Println("Your serial ports:")
175+
if len(portList) == 0 {
176+
log.Println("\tThere are no serial ports to list.")
177+
}
178+
for _, element := range portList {
179+
log.Printf("\t%v\n", element)
177180

178-
}
181+
}
179182

180-
if !*verbose {
181-
log.Println("You can enter verbose mode to see all logging by starting with the -v command line switch.")
182-
log.SetOutput(new(NullWriter)) //route all logging to nullwriter
183-
}
183+
if !*verbose {
184+
log.Println("You can enter verbose mode to see all logging by starting with the -v command line switch.")
185+
log.SetOutput(new(NullWriter)) //route all logging to nullwriter
186+
}
184187

185-
// launch the hub routine which is the singleton for the websocket server
186-
go h.run()
187-
// launch our serial port routine
188-
go sh.run()
189-
// launch our dummy data routine
190-
//go d.run()
191-
192-
go discoverLoop()
193-
194-
r := gin.New()
195-
196-
socketHandler := wsHandler().ServeHTTP
197-
198-
r.Use(cors.Middleware(cors.Config{
199-
Origins: "https://create.arduino.cc, http://create.arduino.cc, https://create-dev.arduino.cc, http://create-dev.arduino.cc, http://webide.arduino.cc:8080, http://create-staging.arduino.cc, https://create-staging.arduino.cc, http://localhost:8989, https://localhost:8990",
200-
Methods: "GET, PUT, POST, DELETE",
201-
RequestHeaders: "Origin, Authorization, Content-Type",
202-
ExposedHeaders: "",
203-
MaxAge: 50 * time.Second,
204-
Credentials: true,
205-
ValidateHeaders: false,
206-
}))
207-
208-
r.GET("/", homeHandler)
209-
r.POST("/upload", uploadHandler)
210-
r.GET("/socket.io/", socketHandler)
211-
r.POST("/socket.io/", socketHandler)
212-
r.Handle("WS", "/socket.io/", socketHandler)
213-
r.Handle("WSS", "/socket.io/", socketHandler)
214-
go func() {
215-
if err := r.RunTLS(*addrSSL, filepath.Join(dest, "cert.pem"), filepath.Join(dest, "key.pem")); err != nil {
188+
// launch the hub routine which is the singleton for the websocket server
189+
go h.run()
190+
// launch our serial port routine
191+
go sh.run()
192+
// launch our dummy data routine
193+
//go d.run()
194+
195+
go discoverLoop()
196+
197+
r := gin.New()
198+
199+
socketHandler := wsHandler().ServeHTTP
200+
201+
r.Use(cors.Middleware(cors.Config{
202+
Origins: "https://create.arduino.cc, http://create.arduino.cc, https://create-dev.arduino.cc, http://create-dev.arduino.cc, http://webide.arduino.cc:8080, http://create-staging.arduino.cc, https://create-staging.arduino.cc, http://localhost:8989, https://localhost:8990",
203+
Methods: "GET, PUT, POST, DELETE",
204+
RequestHeaders: "Origin, Authorization, Content-Type",
205+
ExposedHeaders: "",
206+
MaxAge: 50 * time.Second,
207+
Credentials: true,
208+
ValidateHeaders: false,
209+
}))
210+
211+
r.GET("/", homeHandler)
212+
r.POST("/upload", uploadHandler)
213+
r.GET("/socket.io/", socketHandler)
214+
r.POST("/socket.io/", socketHandler)
215+
r.Handle("WS", "/socket.io/", socketHandler)
216+
r.Handle("WSS", "/socket.io/", socketHandler)
217+
go func() {
218+
if err := r.RunTLS(*addrSSL, filepath.Join(dest, "cert.pem"), filepath.Join(dest, "key.pem")); err != nil {
219+
log.Printf("Error trying to bind to port: %v, so exiting...", err)
220+
log.Fatal("Error ListenAndServe:", err)
221+
}
222+
}()
223+
224+
if err := r.Run(*addr); err != nil {
216225
log.Printf("Error trying to bind to port: %v, so exiting...", err)
217226
log.Fatal("Error ListenAndServe:", err)
218227
}
219228
}()
220-
221-
if err := r.Run(*addr); err != nil {
222-
log.Printf("Error trying to bind to port: %v, so exiting...", err)
223-
log.Fatal("Error ListenAndServe:", err)
224-
}
225-
}()
229+
}
226230
setupSysTray()
227231
}
228232

0 commit comments

Comments
 (0)