Selaa lähdekoodia

auto update script executed

Toby Chui 1 vuosi sitten
vanhempi
commit
c3f20bd586
4 muutettua tiedostoa jossa 57 lisäystä ja 11 poistoa
  1. 38 7
      mod/webserv/webserv.go
  2. 4 4
      start.go
  3. 12 0
      web/components/rproot.html
  4. 3 0
      web/components/webserv.html

+ 38 - 7
mod/webserv/webserv.go

@@ -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.

+ 4 - 4
start.go

@@ -217,14 +217,14 @@ func startupSequence() {
 	*/
 
 	staticWebServer = webserv.NewWebServer(&webserv.WebServerOptions{
-		Port:                   "8081",
+		Sysdb:                  sysdb,
+		Port:                   "8081", //Default Port
 		WebRoot:                *staticWebServerRoot,
 		EnableDirectoryListing: true,
 		EnableWebDirManager:    *allowWebFileManager,
 	})
-
-	//TODO: Connect UI element to static web server
-	staticWebServer.Start()
+	//Restore the web server to previous shutdown state
+	staticWebServer.RestorePreviousState()
 }
 
 // This sequence start after everything is initialized

+ 12 - 0
web/components/rproot.html

@@ -14,6 +14,13 @@
                     <label>Root require TLS connection <br><small>Check this if your proxy root URL starts with https://</small></label>
                 </div>
             </div>
+            <div class="ui horizontal divider">OR</div>
+            <div class="field">
+                <div class="ui checkbox">
+                    <input type="checkbox" id="useStaticWebServer" onchange="handleUseStaticWebServerAsRoot()">
+                    <label>Use Static Web Server as Root <br><small>Check this if you prefer a more Apache Web Server like experience</small></label>
+                </div>
+            </div>
             <br>
             <button class="ui basic button" onclick="setProxyRoot()"><i class="teal home icon" ></i> Update Proxy Root</button>
             <div class="ui divider"></div>
@@ -58,6 +65,11 @@
 <script>
     $("#advanceRootSettings").accordion();
 
+    function handleUseStaticWebServerAsRoot(){
+        alert("WIP");
+        //TODO: Add automatic fill in static web server info
+    }
+    
     function initRootInfo(){
         $.get("/api/proxy/list?type=root", function(data){
             if (data == null){

+ 3 - 0
web/components/webserv.html

@@ -116,6 +116,9 @@
                     $("#webserv_dirManager").remove();
                 }
 
+                $("#webserv_listenPort").val(data.ListeningPort);
+                updateWebServLinkExample(data.ListeningPort);
+
                 //Bind checkbox events
                 $("#webserv_enable").off("change").on("change", function(){
                     let enable = $(this)[0].checked;