Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tejaskumar.kasundra committed Jun 3, 2021
1 parent 9496e47 commit b4a4bcd
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 21 deletions.
10 changes: 6 additions & 4 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
31 changes: 28 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
10 changes: 5 additions & 5 deletions parse-data.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package main

import (
"encoding/json"
"errors"
"io/ioutil"
"log"
"sync"
"errors"

"gopkg.in/yaml.v2"
)
Expand All @@ -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
Expand All @@ -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"`
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -152,4 +152,4 @@ func (c *Config) portStatusUpdate(portname string, st uint8) error {
}
}
return errors.New("Port not found.")
}
}
16 changes: 8 additions & 8 deletions ui/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
11 changes: 10 additions & 1 deletion ui/port.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,16 @@
<script>
var term;
var querystring = window.location.search.split("=").pop()
var websocket = new WebSocket("ws://" + window.location.hostname + ":" + window.location.port + "/serialconsole?portname=" + querystring);
console.log(window.location.protocol);
if (window.location.protocol == "http:") {
sockettype = "ws://"
} else if (window.location.protocol == "https:") {
sockettype = "wss://"
} else {
console.log("Unknown protocol setting socket type to wss.")
sockettype = "wss://"
}
var websocket = new WebSocket(sockettype + window.location.hostname + ":" + window.location.port + "/serialconsole?portname=" + querystring);
websocket.binaryType = "arraybuffer";
document.title = "Port:" + querystring;

Expand Down

0 comments on commit b4a4bcd

Please # to comment.