Merge pull request #22 from RiV-chain/offline_ui

load index.html from file resource
This commit is contained in:
Vadym Vikulin 2022-11-26 16:29:44 +02:00 committed by GitHub
commit d834b2515f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 175 additions and 161 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
.history

4
contrib/ui/mesh-ui/assets/all.min.css vendored Normal file

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -6,8 +6,8 @@
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>RiV-mesh</title>
<link rel="stylesheet" href="https://maxst.icons8.com/vue-static/landings/line-awesome/font-awesome-line-awesome/css/all.min.css" type="text/css">
<link rel="stylesheet" href="https://unpkg.com/bulmaswatch/slate/bulmaswatch.min.css" type="text/css">
<link rel="stylesheet" href="assets/all.min.css" type="text/css">
<link rel="stylesheet" href="assets/bulmaswatch.min.css" type="text/css">
<script>
function setFieldValue(id, value){

Binary file not shown.

View file

@ -1,21 +1,22 @@
package main
import (
"github.com/webview/webview"
"github.com/hjson/hjson-go"
"encoding/json"
"path/filepath"
"io/ioutil"
"os/exec"
"net/url"
"runtime"
"strings"
"strconv"
"time"
"net"
"log"
"fmt"
"io/ioutil"
"log"
"net"
"net/url"
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
"time"
"github.com/hjson/hjson-go"
"github.com/webview/webview"
"github.com/RiV-chain/RiV-mesh/src/admin"
)
@ -51,11 +52,11 @@ func main() {
} else {
//read peers from mesh.conf
conf, _ := ioutil.ReadFile(mesh_settings_path)
var dat map[string]interface {}
var dat map[string]interface{}
if err := hjson.Unmarshal(conf, &dat); err != nil {
fmt.Printf("Unable to parse mesh.conf file: %v", err)
} else {
if dat["Peers"]!=nil {
if dat["Peers"] != nil {
peers := dat["Peers"].([]interface{})
remove_peers()
for _, u := range peers {
@ -69,7 +70,7 @@ func main() {
}
var path string
if len(os.Args)>1 {
if len(os.Args) > 1 {
path, err = filepath.Abs(filepath.Dir(os.Args[1]))
} else {
path, err = filepath.Abs(filepath.Dir(os.Args[0]))
@ -105,32 +106,33 @@ func main() {
w.Bind("ping", func(peer_list string) {
go ping(w, peer_list)
})
dat, err := ioutil.ReadFile(path+"/index.html")
w.Navigate("data:text/html,"+url.QueryEscape(string(dat)))
//dat, err := ioutil.ReadFile(path+"/index.html")
//w.Navigate("data:text/html,"+url.QueryEscape(string(dat)))
//w.Navigate("data:text/html,"+"<html>"+path+"</html>")
w.Navigate("file://" + path + "/index.html")
w.Run()
}
func ping(w webview.WebView, peer_list string){
func ping(w webview.WebView, peer_list string) {
var peers []string
_ = json.Unmarshal([]byte(peer_list), &peers)
log.Printf("Unmarshaled: %v", peers)
for _, u := range peers {
log.Printf("Unmarshaled: %v", u)
ping_time := check(u);
ping_time := check(u)
log.Printf("ping: %d", ping_time)
setPingValue(w, u, strconv.FormatInt(ping_time, 10));
setPingValue(w, u, strconv.FormatInt(ping_time, 10))
}
}
func check(peer string) int64 {
u, e := url.Parse(peer)
if e!=nil {
if e != nil {
return -1
}
t := time.Now()
_, err := net.DialTimeout("tcp", u.Host, 5*time.Second)
if err!=nil {
if err != nil {
return -1
}
d := time.Since(t)
@ -155,7 +157,7 @@ func get_user_home_path() string {
}
}
func get_ctl_path() string{
func get_ctl_path() string {
if runtime.GOOS == "windows" {
program_path := "programfiles"
path, exists := os.LookupEnv(program_path)
@ -173,7 +175,7 @@ func get_ctl_path() string{
}
}
func run(w webview.WebView){
func run(w webview.WebView) {
if len(riv_ctrl_path) > 0 {
get_self(w)
get_peers(w)
@ -183,7 +185,7 @@ func run(w webview.WebView){
})
}
func run_command(command string) []byte{
func run_command(command string) []byte {
args := []string{"-json", command}
cmd := exec.Command(riv_ctrl_path, args...)
out, err := cmd.CombinedOutput()
@ -194,7 +196,7 @@ func run_command(command string) []byte{
return out
}
func run_command_with_arg(command string, arg string) []byte{
func run_command_with_arg(command string, arg string) []byte {
args := []string{"-json", command, arg}
cmd := exec.Command(riv_ctrl_path, args...)
out, err := cmd.CombinedOutput()
@ -205,15 +207,15 @@ func run_command_with_arg(command string, arg string) []byte{
return out
}
func add_peers(uri string){
func add_peers(uri string) {
run_command_with_arg("addpeers", "uri="+uri)
}
func remove_peers(){
func remove_peers() {
run_command("removepeers")
}
func get_self(w webview.WebView){
func get_self(w webview.WebView) {
res := &admin.GetSelfResponse{}
out := run_command("getSelf")
@ -231,7 +233,7 @@ func get_self(w webview.WebView){
//go setFieldValue(w, "peers", string(out))
}
func get_peers(w webview.WebView){
func get_peers(w webview.WebView) {
res := &admin.GetPeersResponse{}
out := run_command("getPeers")
@ -242,7 +244,7 @@ func get_peers(w webview.WebView){
var m []string
for _, s := range res.Peers {
m=append(m, s.Remote)
m = append(m, s.Remote)
}
for k := range m {
// Loop
@ -255,12 +257,12 @@ func get_peers(w webview.WebView){
func setFieldValue(p webview.WebView, id string, value string) {
p.Dispatch(func() {
p.Eval("setFieldValue('"+id+"','"+value+"');")
p.Eval("setFieldValue('" + id + "','" + value + "');")
})
}
func setPingValue(p webview.WebView, peer string, value string) {
p.Dispatch(func() {
p.Eval("setPingValue('"+peer+"','"+value+"');")
p.Eval("setPingValue('" + peer + "','" + value + "');")
})
}