ifs.Enabled()&&!s.IsListening(){// check if not listening because on restart this block will re-executing,but we don't want to start ssh again, ssh will never stops.
ifstation.Config.IsDevelopment&&s.Logger==nil{
s.Logger=station.Logger
}
// cache the messages to be sent to the channel, no need to produce memory allocations here
statusRunningMsg:=_output("The HTTP Server is running.")
statusNotRunningMsg:=_output("The HTTP Server is NOT running. ")
serverStoppedMsg:=_output("The HTTP Server has been stopped.")
errServerNotReadyMsg:=_output("Error: HTTP Server is not even builded yet!")
serverStartedMsg:=_output("The HTTP Server has been started.")
serverRestartedMsg:=_output("The HTTP Server has been restarted.")
loggerStartedMsg:=_output("Logger has been registered to the HTTP Server.\nNew Requests will be printed here.\nYou can still type 'exit' to close this SSH Session.\n\n")
//
sshCommands:=Commands{
Command{Name:"status",Description:"Prompts the status of the HTTP Server, is listening(started) or not(stopped).",Action:func(connssh.Channel){
execPath,err:=osext.Executable()// this works fine, if the developer builded the go app, if just go run main.go then prints the temporary path which the go tool creates
iferr==nil{
conn.Write([]byte("[EXEC] "+execPath+"\n"))
}
}},
// Note for stop If you have opened a tab with Q route:
// in order to see that the http listener has closed you have to close your browser and re-navigate(browsers caches the tcp connection)
Command{Name:"stop",Description:"Stops the HTTP Server.",Action:func(connssh.Channel){
srv:=station.Servers.Main()
ifsrv!=nil&&srv.IsListening(){
srv.Close()
srv.listener=nil
serverStoppedMsg(conn)
}else{
errServerNotReadyMsg(conn)
}
}},
Command{Name:"start",Description:"Starts the HTTP Server.",Action:func(connssh.Channel){
srv:=station.Servers.Main()
if!srv.IsListening(){
gosrv.Open(srv.Handler)
}
serverStartedMsg(conn)
}},
Command{Name:"restart",Description:"Restarts the HTTP Server.",Action:func(connssh.Channel){
srv:=station.Servers.Main()
ifsrv!=nil&&srv.IsListening(){
srv.Close()
srv.listener=nil
}
gosrv.Open(srv.Handler)
serverRestartedMsg(conn)
}},
/*notreadyyet
Command{Name:"service",Description:"[REQUIRES HTTP SERVER's ADMIN PRIVILEGE] Adds the web server to the system services, use it when you want to make your server to autorun on reboot",Action:func(connssh.Channel){
///TODO:
// 1. Unistall service and change the 'service' to 'install service'
// 2. Fix, this current implementation doesn't works on windows 10 it says that the service is not responding to request and start...
// 2.1 the fix is maybe add these and change the s.Install to s.Run to the $DESKTOP/some/q/main.go I will try this
// as the example shows.
// remember: run command line as administrator > sc delete "Iris Web Server - $DATETIME" to delete the service, do it on each test.
svcConfig:=&service.Config{
Name:"Iris Web Server - "+time.Now().Format(q.TimeFormat),
DisplayName:"Iris Web Server - "+time.Now().Format(q.TimeFormat),
Description:"The web server which has been registered by SSH interface.",
}
prg:=&systemServiceWrapper{}
s,err:=service.New(prg,svcConfig)
iferr!=nil{
conn.Write([]byte(err.Error()+"\n"))
return
}
err=s.Install()
iferr!=nil{
conn.Write([]byte(err.Error()+"\n"))
return
}
conn.Write([]byte("Service has been registered.\n"))
}},*/
Command{Name:"log",Description:"Adds a logger to the HTTP Server, waits for requests and prints them here.",Action:func(connssh.Channel){
// the ssh user can still write commands, this is not blocking anything.
loggerMiddleware:=NewLoggerHandler(conn,true)
station.UseGlobalFunc(loggerMiddleware)
// register to the errors also
errorLoggerHandler:=NewLoggerHandler(conn,false)
fork,v:=rangestation.mux.errorHandlers{
errorH:=v
// wrap the error handler with the ssh logger middleware
errorLoggerHandler(ctx)// after the error handler because that is setting the status code.
})
}
station.mux.build()// rebuild the mux in order the UseGlobalFunc to work at runtime
loggerStartedMsg(conn)
// the middleware will still to run, we could remove it on exit but exit is general command I dont want to touch that
// we could make a command like 'log stop' or on 'stop' to remove the middleware...I will think about it.
}},
}
for_,cmd:=rangesshCommands{
if_,found:=s.Commands.ByName(cmd.Name);!found{// yes, the user can add custom commands too, I will cover this on docs some day, it's not too hard if you see the code.
CommandsCommands// Commands{Command{Name: "restart", Description:"restarts & rebuild the server", Action: func(ssh.Channel){}}}
// note for Commands field:
// the default Iris's commands are defined at the end of this file, I tried to make this file as standalone as I can, because it will be used for Iris web framework also.
Shellbool// Set it to true to enable execute terminal's commands(system commands) via ssh if no other command is found from the Commands field. Defaults to false for security reasons
Logger*logger.Logger// log.New(...)/ $qinstance.Logger, fill it when you want to receive debug and info/warnings messages
}
// Enabled returns true if SSH can be started, if Host != ""
func(s*SSHServer)Enabled()bool{
ifs==nil{
returnfalse
}
returns.Host!=""
}
// IsListening returns true if ssh server has been started