Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

standalone binary & read only mode #33

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ On some devices WOL can be difficult to correctly configure and have work reliab

![Screenshot](wolweb_ui.png)

The UI features CRUD operation implemented using [js-grid.com](https://github.com/tabalinas/jsgrid) plugin.
The UI features CRUD operation implemented using [js-grid.com](https://github.com/tabalinas/jsgrid) plugin.

### Wake-up directly using HTTP Request

Expand All @@ -50,6 +50,7 @@ The application will use the following default values if they are not explicitly
| Port | Define the port on which the webserver will listen | **8089**
| Virtual Directory | A virtual directory to mount this application under | **/wolweb**
| Broadcast IP and Port | This is broadcast IP address and port for the local network. *Please include the port :9* | **192.168.1.255:9**
| Read Only | If set to true, the UI will be read only | **false**

You can override the default application configuration by using a config file or by setting environment variables. The application will first load values from config file and look for environment variables and overwrites values from the file with the values which were found in the environment.

Expand All @@ -60,7 +61,8 @@ You can override the default application configuration by using a config file or
"host": "0.0.0.0",
"port": 8089,
"vdir":"/wolweb",
"bcastip":"192.168.1.255:9"
"bcastip":"192.168.1.255:9",
"read_only":false
}
```
**Using Environment Variables:**
Expand All @@ -73,6 +75,7 @@ You can override the default application configuration by using a config file or
| WOLWEBPORT | Override for default HTTP port
| WOLWEBVDIR | Override for default virtual directory
| WOLWEBBCASTIP | Override for broadcast IP address and port
| WOLWEBREADONLY | Override for read only mode of UI

## Devices (targets) - devices.json format
```json
Expand Down Expand Up @@ -121,7 +124,7 @@ docker cp wolweb:/wolweb - > wolweb.gz
```


## Build
## Build
You need Go 1.20 to build binary for any OS.

```powershell
Expand Down Expand Up @@ -167,4 +170,4 @@ Thank you to the developers behind both projects, David Baumann and Shaba Abhira


## License
Distributed with GNU General Public License (c) 2023
Distributed with GNU General Public License (c) 2023
3 changes: 2 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"host": "0.0.0.0",
"port": 8089,
"vdir": "/wolweb",
"bcastip": "192.168.1.255:9"
"bcastip": "192.168.1.255:9",
"read_only": false
}
4 changes: 2 additions & 2 deletions devices.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"devices": [
{
"name": "Server",
"name": "Server1",
"mac": "34:E6:D7:33:12:71",
"ip": "192.168.1.255:9"
},
Expand All @@ -11,7 +11,7 @@
"ip": "192.168.1.255:9"
},
{
"name": "Laptop",
"name": "Laptop2",
"mac": "18:1D:EA:70:A0:21",
"ip": "192.168.1.255:9"
}
Expand Down
8 changes: 5 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<link rel="preload" href="static/css/[email protected]" as="style" onload="this.onload=null;this.rel='stylesheet'" />
</head>

<body>
<body data-read-only="{{ .ReadOnly }}">
<nav class="navbar navbar-dark navbar-expand-md py-3">
<div class="container">
<a class="navbar-brand d-flex align-items-center">
Expand All @@ -35,9 +35,11 @@
<h3>Devices</h3>
<div class="table-controls">
<div class="btn-group">
{{ if not .ReadOnly }}
<button id="device-insert-btn" class="btn btn-sm btn-outline-success" type="button" title="Add new device">
<i class="bi bi-plus-lg" style="margin-right: 6px;"></i>NEW
</button>
{{ end }}
<button id="device-filter-btn" class="btn btn-sm btn-outline-secondary" type="button" title="Filter devices">
<i class="bi bi-filter" style="margin-right: 6px;"></i>FILTER
</button>
Expand All @@ -52,7 +54,7 @@ <h3>Devices</h3>
<h5>Title here</h5>
<p style="margin: 0; font-size: calc(var(--bs-body-font-size) - 1pt);">Message here</p>
</div>

</div>
<div id="GridDevices"></div>
<hr>
Expand Down Expand Up @@ -118,7 +120,7 @@ <h6 style="margin-top: -4px;">/wolweb/wake/<span class="text-secondary">&lt;Devi
<script>
window.vDir = "{{$.VDir}}"
window.bCastIP = "{{$.BCastIP}}"

$( "#code-element" ).load( "static/api-sample.txt" , function() {
hljs.highlightAll();
});
Expand Down
18 changes: 15 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"embed"
"io/fs"
"log"
"net"
"net/http"
Expand All @@ -19,6 +21,9 @@ import (
var appConfig AppConfig
var appData AppData

//go:embed static
var staticFiles embed.FS

func main() {

setWorkingDir()
Expand Down Expand Up @@ -64,7 +69,12 @@ func setupWebServer() {
}

// map directory to server static files
router.PathPrefix(basePath + "/static/").Handler(http.StripPrefix(basePath+"/static/", CacheControlWrapper(http.FileServer(http.Dir("./static")))))
var staticFS = fs.FS(staticFiles)
staticFS, err := fs.Sub(staticFS, "static")
if err != nil {
panic(err)
}
router.PathPrefix(basePath + "/static/").Handler(http.StripPrefix(basePath+"/static/", CacheControlWrapper(http.FileServer(http.FS(staticFS)))))

// Define Home Route
router.HandleFunc(basePath+"/", renderHomePage).Methods("GET")
Expand All @@ -73,8 +83,10 @@ func setupWebServer() {
router.HandleFunc(basePath+"/wake/{deviceName}", wakeUpWithDeviceName).Methods("GET")
router.HandleFunc(basePath+"/wake/{deviceName}/", wakeUpWithDeviceName).Methods("GET")

// Define Data save Api function
router.HandleFunc(basePath+"/data/save", saveData).Methods("POST")
if appConfig.ReadOnly == false {
// Define Data save Api function
router.HandleFunc(basePath+"/data/save", saveData).Methods("POST")
}

// Define Data get Api function
router.HandleFunc(basePath+"/data/get", getData).Methods("GET")
Expand Down
20 changes: 13 additions & 7 deletions pages.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
package main

import (
_ "embed"
"html/template"
"io"
"log"
"net/http"
)

//go:embed index.html
var indexHtml string

func renderHomePage(w http.ResponseWriter, r *http.Request) {

pageData := struct {
Devices []Device
VDir string
BCastIP string
Devices []Device
VDir string
BCastIP string
ReadOnly bool
}{
Devices: appData.Devices,
VDir: appConfig.VDir,
BCastIP: appConfig.BCastIP,
Devices: appData.Devices,
VDir: appConfig.VDir,
BCastIP: appConfig.BCastIP,
ReadOnly: appConfig.ReadOnly,
}
if appConfig.VDir == "/" {
pageData.VDir = ""
}
tmpl, _ := template.ParseFiles("index.html")
tmpl, _ := template.New("index.html").Parse(indexHtml)
tmpl.Execute(w, pageData)
log.Println("Renedered the home page.")

Expand Down
Loading