From b4a4bcd6e87a932a57c58d724d6a6410c5a84e5f Mon Sep 17 00:00:00 2001 From: "tejaskumar.kasundra" Date: Thu, 3 Jun 2021 15:57:27 +0530 Subject: [PATCH] Initial commit --- config.yaml | 10 ++++++---- main.go | 31 ++++++++++++++++++++++++++++--- parse-data.go | 10 +++++----- ui/home.html | 16 ++++++++-------- ui/port.html | 11 ++++++++++- 5 files changed, 57 insertions(+), 21 deletions(-) diff --git a/config.yaml b/config.yaml index 946b1c9..9f9b45b 100644 --- a/config.yaml +++ b/config.yaml @@ -22,10 +22,12 @@ logs: maxage: 30 #Number of Days serverconfig: - name: http + enable: 1 #1-Enable 2-Disable port: 8083 sslcert: na sslkey: na - - name: https #Not implemented yet. - port: 8084 #Not implemented yet. - sslcert: /root/serial-port-websocket/server.crt #Not implemented yet. - sslkey: /etc/serial-port-websocket/server.key #Not implemented yet. + - name: https + enable: 1 #1-Enable 2-Disable + port: 8084 + sslcert: /root/serial-port-websocket/server.crt + sslkey: /etc/serial-port-websocket/server.key diff --git a/main.go b/main.go index a3325d4..2b36bd3 100644 --- a/main.go +++ b/main.go @@ -131,8 +131,33 @@ func main() { return } r := createRouterRegisterPaths() - err := http.ListenAndServe(":8083", r) - if err != nil { - log.Printf("net.http could not listen on address 8083: %s\n", err) + errs := make(chan error) + for _, value := range config.ServerConfig { + if value.Enable == 1 { + if value.Name == "http" { + go func(port int) { + log.Printf("http server starting") + err := http.ListenAndServe(":"+strconv.Itoa(port), r) + if err != nil { + log.Printf("net.http could not listen: %s\n", err) + errs <- err + } + }(value.Port) + } else if value.Name == "https" { + go func(port int, sslcert string, sslkey string) { + log.Printf("https server starting") + err := http.ListenAndServeTLS(":"+strconv.Itoa(port), sslcert, sslkey, r) + if err != nil { + log.Printf("net.https could not listen: %s", err) + errs <- err + } + }(value.Port, value.SslCert, value.SslKey) + } else { + log.Printf("Unknown protocol %s... Skipping...", value.Name) + } + } else { + log.Printf("Server disabled for protocol:%s", value.Name) + } } + <-errs } diff --git a/parse-data.go b/parse-data.go index a68784e..64fbb2f 100644 --- a/parse-data.go +++ b/parse-data.go @@ -2,10 +2,10 @@ package main import ( "encoding/json" + "errors" "io/ioutil" "log" "sync" - "errors" "gopkg.in/yaml.v2" ) @@ -16,7 +16,7 @@ type port struct { Baudrate int `yaml:"baudrate"` Parity byte `yaml:"parity"` Desc string `yaml:"desc"` - Status uint8 `yaml:"status"` + Status uint8 `yaml:"status"` } // Config type for YAML File marshall/unmarshall @@ -25,13 +25,13 @@ type Config struct { Ports []port `yaml:"ports"` Logs struct { Inlogs string `yaml:"inlogs"` - Outlogs string `yaml:"outlogs"` Maxsize int `yaml:"maxsize"` Maxbackups int `yaml:"maxbackups"` Maxage int `yaml:"maxage"` } `yaml:"logs"` ServerConfig []struct { Name string `yaml:"name"` + Enable int `yaml:"enable"` Port int `yaml:"port"` SslCert string `yaml:"sslcert"` SslKey string `yaml:"sslkey"` @@ -102,7 +102,7 @@ func (c *Config) checkElement(portname string) bool { return false } -// addElement will add new element provided port name, baudrate +// addElement will add new element provided port name, baudrate // and description func (c *Config) addElement(portname string, baudrate int, des string) error { c.mu.Lock() @@ -152,4 +152,4 @@ func (c *Config) portStatusUpdate(portname string, st uint8) error { } } return errors.New("Port not found.") -} \ No newline at end of file +} diff --git a/ui/home.html b/ui/home.html index 4daaf05..bbddf61 100644 --- a/ui/home.html +++ b/ui/home.html @@ -265,37 +265,37 @@ var eleid = JSONConvert.Ports[i].Name.split("/").pop(); var link = "window.open('" + window.location.protocol + "//" + window.location.hostname + ":" + window.location.port + "/port?portname=" + JSONConvert.Ports[i].Name + "')" - var getconsole = CreateBtn("btn btn-info mr-2", "console-" + eleid, + var getconsole = CreateBtn("btn btn-sm btn-info mr-2", "console-" + eleid, "Get Console", link) link = "window.open('" + window.location.protocol + "//" + window.location.hostname + ":" + window.location.port + "/logs/')" - var getlogs = CreateBtn("btn btn-info mr-2", "logs-" + eleid, + var getlogs = CreateBtn("btn btn-sm btn-info mr-2", "logs-" + eleid, "Get Logs", link) cell3.append(getconsole, getlogs); var eleid = JSONConvert.Ports[i].Name.split("/").pop(); if (JSONConvert.Ports[i].Status == 1) { - var startstopport = createbutton("btn btn-danger mr-2", null, "startstop-" + eleid, + var startstopport = createbutton("btn btn-sm btn-danger mr-2", null, "startstop-" + eleid, "Disable", "stopport('" + JSONConvert.Ports[i].Name + "')"); } else if (JSONConvert.Ports[i].Status == 2) { - var startstopport = createbutton("btn btn-info mr-2", null, "startstop-" + eleid, + var startstopport = createbutton("btn btn-sm btn-info mr-2", null, "startstop-" + eleid, "Enable", "startport('" + JSONConvert.Ports[i].Name + "')"); getlogs.disabled = true; getconsole.disabled = true; } else if (JSONConvert.Ports[i].enabled == 3) { - var startstopport = createbutton("btn btn-danger mr-2", null, "startstop-" + eleid, + var startstopport = createbutton("btn btn-sm btn-danger mr-2", null, "startstop-" + eleid, "Disabling", null); getlogs.disabled = true; getconsole.disabled = true; } else if (JSONConvert.Ports[i].enabled == 4) { - var startstopport = createbutton("btn btn-info mr-2", null, "submit-" + eleid, + var startstopport = createbutton("btn btn-sm btn-info mr-2", null, "submit-" + eleid, "Enabling", null); getlogs.disabled = true; getconsole.disabled = true; } - del = createbutton("btn btn-danger", null, JSONConvert.Ports[i].Name, + del = createbutton("btn btn-sm btn-danger", null, JSONConvert.Ports[i].Name, "Delete", "confirmdelete('" + JSONConvert.Ports[i].Name + "')"); tmp = { "data-toggle": "modal", "data-target": "#editportmodal" }; - edit = createbutton("btn btn-info mr-2", tmp, JSONConvert.Ports[i].Name, + edit = createbutton("btn btn-sm btn-info mr-2", tmp, JSONConvert.Ports[i].Name, "Edit", null); row.insertCell(4).append(startstopport, edit, del); } diff --git a/ui/port.html b/ui/port.html index 0c3c4c6..8108ae6 100644 --- a/ui/port.html +++ b/ui/port.html @@ -15,7 +15,16 @@