|
@@ -11,6 +11,7 @@ import (
|
|
|
"path/filepath"
|
|
|
"sync"
|
|
|
|
|
|
+ "imuslab.com/zoraxy/mod/database"
|
|
|
"imuslab.com/zoraxy/mod/utils"
|
|
|
)
|
|
|
|
|
@@ -24,10 +25,11 @@ import (
|
|
|
var templates embed.FS
|
|
|
|
|
|
type WebServerOptions struct {
|
|
|
- Port string //Port for listening
|
|
|
- EnableDirectoryListing bool //Enable listing of directory
|
|
|
- WebRoot string //Folder for stroing the static web folders
|
|
|
- EnableWebDirManager bool //Enable web file manager to handle files in web directory
|
|
|
+ Port string //Port for listening
|
|
|
+ EnableDirectoryListing bool //Enable listing of directory
|
|
|
+ WebRoot string //Folder for stroing the static web folders
|
|
|
+ EnableWebDirManager bool //Enable web file manager to handle files in web directory
|
|
|
+ Sysdb *database.Database //Database for storing configs
|
|
|
}
|
|
|
|
|
|
type WebServer struct {
|
|
@@ -38,7 +40,7 @@ type WebServer struct {
|
|
|
mu sync.Mutex
|
|
|
}
|
|
|
|
|
|
-// NewWebServer creates a new WebServer instance.
|
|
|
+// NewWebServer creates a new WebServer instance. One instance only
|
|
|
func NewWebServer(options *WebServerOptions) *WebServer {
|
|
|
if !utils.FileExists(options.WebRoot) {
|
|
|
//Web root folder not exists. Create one with default templates
|
|
@@ -52,6 +54,9 @@ func NewWebServer(options *WebServerOptions) *WebServer {
|
|
|
}
|
|
|
|
|
|
}
|
|
|
+
|
|
|
+ //Create new table to store the config
|
|
|
+ options.Sysdb.NewTable("webserv")
|
|
|
return &WebServer{
|
|
|
mux: http.NewServeMux(),
|
|
|
option: options,
|
|
@@ -60,6 +65,29 @@ func NewWebServer(options *WebServerOptions) *WebServer {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+// Restore the configuration to previous config
|
|
|
+func (ws *WebServer) RestorePreviousState() {
|
|
|
+ //Set the port
|
|
|
+ port := ws.option.Port
|
|
|
+ ws.option.Sysdb.Read("webserv", "port", &port)
|
|
|
+ ws.option.Port = port
|
|
|
+
|
|
|
+ //Set the enable directory list
|
|
|
+ enableDirList := ws.option.EnableDirectoryListing
|
|
|
+ ws.option.Sysdb.Read("webserv", "dirlist", &enableDirList)
|
|
|
+ ws.option.EnableDirectoryListing = enableDirList
|
|
|
+
|
|
|
+ //Check the running state
|
|
|
+ webservRunning := false
|
|
|
+ ws.option.Sysdb.Read("webserv", "enabled", &webservRunning)
|
|
|
+ if webservRunning {
|
|
|
+ ws.Start()
|
|
|
+ } else {
|
|
|
+ ws.Stop()
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
// ChangePort changes the server's port.
|
|
|
func (ws *WebServer) ChangePort(port string) error {
|
|
|
if ws.isRunning {
|
|
@@ -76,6 +104,8 @@ func (ws *WebServer) ChangePort(port string) error {
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
+ ws.option.Sysdb.Write("webserv", "port", port)
|
|
|
+
|
|
|
return nil
|
|
|
}
|
|
|
|
|
@@ -116,7 +146,7 @@ func (ws *WebServer) Start() error {
|
|
|
|
|
|
log.Println("Static Web Server started. Listeing on :" + ws.option.Port)
|
|
|
ws.isRunning = true
|
|
|
-
|
|
|
+ ws.option.Sysdb.Write("webserv", "enabled", true)
|
|
|
return nil
|
|
|
}
|
|
|
|
|
@@ -134,13 +164,14 @@ func (ws *WebServer) Stop() error {
|
|
|
}
|
|
|
|
|
|
ws.isRunning = false
|
|
|
-
|
|
|
+ ws.option.Sysdb.Write("webserv", "enabled", false)
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
// UpdateDirectoryListing enables or disables directory listing.
|
|
|
func (ws *WebServer) UpdateDirectoryListing(enable bool) {
|
|
|
ws.option.EnableDirectoryListing = enable
|
|
|
+ ws.option.Sysdb.Write("webserv", "dirlist", enable)
|
|
|
}
|
|
|
|
|
|
// Close stops the web server without returning an error.
|