From c15fd03e3a9c5666b495685f3e63e5e0c9737ebb Mon Sep 17 00:00:00 2001 From: Volker Schukai <volker.schukai@schukai.com> Date: Sun, 4 Sep 2022 11:31:32 +0200 Subject: [PATCH] chore: commit save point --- README.md | 31 +++++++--- application/source/go.mod | 4 +- application/source/go.sum | 6 ++ application/source/logging/logger.go | 3 +- application/source/server/inject.go | 82 ++----------------------- application/source/server/inject.js | 74 ++++++++++++++++++++++ application/source/server/inject_gen.go | 4 ++ application/source/watch/resources.go | 2 + development/script/go.mod | 9 +++ development/script/go.sum | 6 ++ development/script/inject-js.go | 62 +++++++++++++++++++ release.json | 2 +- 12 files changed, 195 insertions(+), 90 deletions(-) create mode 100644 application/source/server/inject.js create mode 100644 application/source/server/inject_gen.go create mode 100644 development/script/go.mod create mode 100644 development/script/go.sum create mode 100644 development/script/inject-js.go diff --git a/README.md b/README.md index d4f90e5..7100de9 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,6 @@ This tool helps to serve files. Conan is a binary file that must be stored in a directory. The files can be found [here](http://download.schukai.com/tools/conan/). - ```bash wget -O ~/.local/bin/conan http://download.schukai.com/tools/conan/conan-$( uname -s | tr [:upper:] [:lower:])-$(echo `uname -m | sed s/aarch64/arm64/ | sed s/x86_64/amd64/`) ## For Linux, the execution bit must still be set. @@ -22,22 +21,22 @@ chmod u+x ~/.local/bin/conan ```yaml Server: - + # The hostname or IP address of the server (CONAN_SERVER_HOST) # Domain, IP or hostname of the server # Host: localhost:8080 - + # The port of the server (CONAN_SERVER_ADDRESS) # Address: localhost - + # The port of the server (CONAN_SERVER_PORT) # Port: 8080 - + # Path: # The path to the server (CONAN_SERVER_WEB_PATH) Web: web - + Watch: - Path: src Command: /bin/bash -c "npx esbuild --bundle --outfile={{ .WebPath }}/scripts/bundle.js --sourcemap {{ .Path }}" @@ -48,13 +47,26 @@ Server: Exclude: - ~$ - ^\. - + Flags: FollowSymlinks: true - + ``` +The following placeholders can be used in the Commandline. + + +| Placeholder | Description | +| ----------- |------------------------------------------------| +| `Path` | The Watched Path | +| `IsDir` | Is the Path a directory? | +| `Directory` | If the path is a file, then the directory here | +| `WebPath` | The Web Path | +| `PID` | The Process ID (Server) | +| `Bin` | The command used to start the server. | +| `Call` | The entire call | + ### Server Start server and deliver files. @@ -80,8 +92,7 @@ conan --help ## Change Log - Version 1.0 - - Initial release - + - Initial release ## License # Credits diff --git a/application/source/go.mod b/application/source/go.mod index d72d73e..1e9a264 100644 --- a/application/source/go.mod +++ b/application/source/go.mod @@ -19,11 +19,13 @@ require ( require ( bitbucket.org/creachadair/shell v0.0.7 // indirect github.com/bitfield/script v0.20.2 // indirect + github.com/dave/jennifer v1.5.1 // indirect + github.com/evanw/esbuild v0.15.6 // indirect github.com/itchyny/gojq v0.12.7 // indirect github.com/itchyny/timefmt-go v0.1.3 // indirect github.com/unknwon/com v1.0.1 // indirect github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect go.uber.org/atomic v1.7.0 // indirect go.uber.org/multierr v1.6.0 // indirect - golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect + golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect ) diff --git a/application/source/go.sum b/application/source/go.sum index 4ec1200..2730002 100644 --- a/application/source/go.sum +++ b/application/source/go.sum @@ -11,11 +11,15 @@ github.com/couchbase/go-couchbase v0.0.0-20201026062457-7b3be89bbd89/go.mod h1:+ github.com/couchbase/gomemcached v0.1.1/go.mod h1:mxliKQxOv84gQ0bJWbI+w9Wxdpt9HjDvgW9MjCym5Vo= github.com/couchbase/goutils v0.0.0-20201030094643-5e82bb967e67/go.mod h1:BQwMFlJzDjFDG3DJUdU0KORxn88UlsOULuxLExMh3Hs= github.com/cupcake/rdb v0.0.0-20161107195141-43ba34106c76/go.mod h1:vYwsqCOLxGiisLwp9rITslkFNpZD5rz43tf41QFkTWY= +github.com/dave/jennifer v1.5.1 h1:AI8gaM02nCYRw6/WTH0W+S6UNck9YqPZ05xoIxQtuoE= +github.com/dave/jennifer v1.5.1/go.mod h1:AxTG893FiZKqxy3FP1kL80VMshSMuz2G+EgvszgGRnk= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= github.com/edsrzf/mmap-go v1.0.0/go.mod h1:YO35OhQPt3KJa3ryjFM5Bs14WD66h8eGKpfaBNrHW5M= +github.com/evanw/esbuild v0.15.6 h1:zrPkd5NNIxreDrLvFYZR0MIvytz4t7V/cyDsViSIgaY= +github.com/evanw/esbuild v0.15.6/go.mod h1:iINY06rn799hi48UqEnaQvVfZWe6W9bET78LbvN8VWk= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= github.com/fsnotify/fsnotify v1.5.4 h1:jRbGcIw6P2Meqdwuo0H1p6JVLbL5DHKAKlYndzMwVZI= @@ -131,6 +135,8 @@ golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220412211240-33da011f77ad h1:ntjMns5wyP/fN65tdBD4g8J5w8n015+iIIs9rtjXkY0= golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= diff --git a/application/source/logging/logger.go b/application/source/logging/logger.go index b45bfe2..959204b 100644 --- a/application/source/logging/logger.go +++ b/application/source/logging/logger.go @@ -62,7 +62,8 @@ func InitLogger() { } - logger, err := cfg.Build() + var err error + logger, err = cfg.Build() if err != nil { panic(err) } diff --git a/application/source/server/inject.go b/application/source/server/inject.go index 728e697..cd2c035 100644 --- a/application/source/server/inject.go +++ b/application/source/server/inject.go @@ -46,93 +46,21 @@ func injectHandler(next http.Handler) http.Handler { } +//go:generate go run ../../../development/script/inject-js.go var script string func init() { - script = `<head> - <script type="module"> - - try { - let died = false; - let counter = 0; - let socket - let url = "" - if (document.location.protocol == 'https:') { - url = 'wss://' - } else { - url = 'ws://' - } - url += document.location.host + "{{ constants.WebsocketPath }}"; - - function connectWebsocket() { - - if (socket) { - socket.close() - } - - if (counter++ > 20) { - console.error("Failed 20 times to connect to websocket. Giving up.") - return - } - - - socket = new WebSocket(url) - - socket.onopen = function (e) { - - if (died===true ){ - window.location.reload(); - } - - console.log("[conan] Connection established"); - counter = 0 - }; - - socket.onmessage = function (event) { - console.log("[conan] Data received from server: " + event?.data); - - if (event?.data == "reload") { - console.log("[conan] Reloading page") - window.location.reload(); - } - }; - - socket.onclose = function (event) { - if (event.wasClean) { - console.log("[conan] Connection closed cleanly, code=" + event?.code + " reason=" + event?.reason + ""); - } else { - console.error("[conan] Connection died"); - died = true - setTimeout(connectWebsocket, 3000*counter) - } - }; - - socket.onerror = function (error) { - console.error("[conan] error", error); - }; - - } - - document.addEventListener("DOMContentLoaded", () => { - connectWebsocket() - }) - - } catch (e) { - console.error(e) - } - - - </script>` - - script = strings.Replace(script, "{{ constants.WebsocketPath }}", constants.WebsocketPath, -1) + script = "<script type=\"module\">" + + strings.Replace(jsTemplate, "{{ constants.WebsocketPath }}", constants.WebsocketPath, -1) + + "</script><body" } func injectScript(p []byte) []byte { s := string(p) - reg := regexp.MustCompile(`<head>`) + reg := regexp.MustCompile(`<body`) s = reg.ReplaceAllString(s, script) return []byte(s) diff --git a/application/source/server/inject.js b/application/source/server/inject.js new file mode 100644 index 0000000..df6700c --- /dev/null +++ b/application/source/server/inject.js @@ -0,0 +1,74 @@ +// This script is integrated into the inject_gen.go file using a generator. +// It is not intended to be run directly. +// run go generate to update the inject_gen.go file. + +try { + let died = false; + let counter = 0; + let socket + let url = "" + if (document.location.protocol == 'https:') { + url = 'wss://' + } else { + url = 'ws://' + } + url += document.location.host + "{{ constants.WebsocketPath }}"; + + function connectWebsocket() { + + if (socket) { + socket.close() + } + + if (counter++ > 20) { + console.error("Failed 20 times to connect to websocket. Giving up.") + return + } + + socket = new WebSocket(url) + + socket.onopen = function (e) { + + if (died === true) { + window.location.reload(); + } + + console.log("[conan] Connection established"); + counter = 0 + }; + + socket.onmessage = function (event) { + console.log("[conan] Data received from server: " + event?.data); + + if (event?.data == "reload") { + console.log("[conan] Reloading page") + window.location.reload(); + } + }; + + socket.onclose = function (event) { + if (event.wasClean) { + console.log("[conan] Connection closed cleanly, code=" + event?.code + " reason=" + event?.reason + ""); + } else { + console.error("[conan] Connection died"); + died = true + setTimeout(connectWebsocket, 3000 * counter) + } + }; + + socket.onerror = function (error) { + console.error("[conan] error", error); + }; + + } + + document.addEventListener("DOMContentLoaded", () => { + connectWebsocket() + }) + +} catch (e) { + console.error(e) +} + + +// Language: javascript \ No newline at end of file diff --git a/application/source/server/inject_gen.go b/application/source/server/inject_gen.go new file mode 100644 index 0000000..10d4c56 --- /dev/null +++ b/application/source/server/inject_gen.go @@ -0,0 +1,4 @@ +// Code generated by generator, DO NOT EDIT. +package server + +var jsTemplate string = "(()=>{try{let r=function(){if(n&&n.close(),l++>20){console.error(\"Failed 20 times to connect to websocket. Giving up.\");return}n=new WebSocket(e),n.onopen=function(o){c===!0&&window.location.reload(),console.log(\"[conan] Connection established\"),l=0},n.onmessage=function(o){console.log(\"[conan] Data received from server: \"+(o==null?void 0:o.data)),(o==null?void 0:o.data)==\"reload\"&&(console.log(\"[conan] Reloading page\"),window.location.reload())},n.onclose=function(o){o.wasClean?console.log(\"[conan] Connection closed cleanly, code=\"+(o==null?void 0:o.code)+\" reason=\"+(o==null?void 0:o.reason)):(console.error(\"[conan] Connection died\"),c=!0,setTimeout(r,3e3*l))},n.onerror=function(o){console.error(\"[conan] error\",o)}};a=r;let c=!1,l=0,n,e=\"\";document.location.protocol==\"https:\"?e=\"wss://\":e=\"ws://\",e+=document.location.host+\"{{ constants.WebsocketPath }}\",document.addEventListener(\"DOMContentLoaded\",()=>{r()})}catch(c){console.error(c)}var a;})();\n" diff --git a/application/source/watch/resources.go b/application/source/watch/resources.go index 12cc6b1..6953982 100644 --- a/application/source/watch/resources.go +++ b/application/source/watch/resources.go @@ -108,6 +108,8 @@ func executeWatchAction(watchPath string) { continue } + logging.LogInfo("watching: execute watch action: %s", cmd.String()) + result := script.Exec(cmd.String()) if result.Error() != nil { logging.LogError("watching: execute watch action error: %v", result.Error()) diff --git a/development/script/go.mod b/development/script/go.mod new file mode 100644 index 0000000..c006c9d --- /dev/null +++ b/development/script/go.mod @@ -0,0 +1,9 @@ +module gitlab.schukai.com/oss/utilities/conan/script + +go 1.19 + +require ( + github.com/dave/jennifer v1.5.1 // indirect + github.com/evanw/esbuild v0.15.6 // indirect + golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect +) diff --git a/development/script/go.sum b/development/script/go.sum new file mode 100644 index 0000000..b48c8b3 --- /dev/null +++ b/development/script/go.sum @@ -0,0 +1,6 @@ +github.com/dave/jennifer v1.5.1 h1:AI8gaM02nCYRw6/WTH0W+S6UNck9YqPZ05xoIxQtuoE= +github.com/dave/jennifer v1.5.1/go.mod h1:AxTG893FiZKqxy3FP1kL80VMshSMuz2G+EgvszgGRnk= +github.com/evanw/esbuild v0.15.6 h1:zrPkd5NNIxreDrLvFYZR0MIvytz4t7V/cyDsViSIgaY= +github.com/evanw/esbuild v0.15.6/go.mod h1:iINY06rn799hi48UqEnaQvVfZWe6W9bET78LbvN8VWk= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/development/script/inject-js.go b/development/script/inject-js.go new file mode 100644 index 0000000..1b36e21 --- /dev/null +++ b/development/script/inject-js.go @@ -0,0 +1,62 @@ +package main + +import ( + . "github.com/dave/jennifer/jen" + "github.com/evanw/esbuild/pkg/api" + "path" + "path/filepath" +) +import "os" + +func main() { + + wd, _ := os.Getwd() + //file := os.Getenv("GOFILE") + + //p := path.Join(wd, file) + j := path.Join(wd, "inject.js") + + result := api.Build(api.BuildOptions{ + EntryPoints: []string{j}, + Bundle: true, + MinifyWhitespace: true, + MinifyIdentifiers: true, + MinifySyntax: true, + Engines: []api.Engine{ + {api.EngineChrome, "58"}, + {api.EngineFirefox, "57"}, + {api.EngineSafari, "11"}, + {api.EngineEdge, "16"}, + }, + Write: false, + }) + + if len(result.Errors) > 0 { + os.Exit(1) + } + + code := "" + for _, r := range result.OutputFiles { + code += string(r.Contents) + } + // + //content, err := os.ReadFile(p) + //if err != nil { + // os.Exit(1) + //} + + //content = []byte(code) + + goPackage := os.Getenv("GOPACKAGE") + f := NewFile(goPackage) + f.PackageComment("Code generated by generator, DO NOT EDIT.") + + f.Var().Id("jsTemplate").String().Op("=").Lit(code) + + goFile := os.Getenv("GOFILE") + ext := filepath.Ext(goFile) + baseFilename := goFile[0 : len(goFile)-len(ext)] + targetFilename := baseFilename + "_gen.go" + + f.Save(targetFilename) +} diff --git a/release.json b/release.json index 61d897f..67a06bd 100644 --- a/release.json +++ b/release.json @@ -1 +1 @@ -{"version":"0.1.39"} +{"version":"0.1.41"} -- GitLab