diff --git a/.gitignore b/.gitignore index edb0c2d..3d191de 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,9 @@ # go executable delphi.market +# environment .env + +# required for hot reload +public/hotreload +*.log diff --git a/hotreload.sh b/hotreload.sh new file mode 100644 index 0000000..194d23a --- /dev/null +++ b/hotreload.sh @@ -0,0 +1,29 @@ +#!/usr/bin/env bash + +PID=$(pidof delphi.market) + +set -e + +function restart_server() { + [[ -z "$PID" ]] || kill -15 $PID + ENV=development make build -B + ./delphi.market >> server.log 2>&1 & + PID=$(pidof delphi.market) +} + +function sync() { + restart_server + date +%s.%N > public/hotreload + rsync -avh public/ dev1.delphi.market:/var/www/dev1.delphi --delete +} + +function cleanup() { + rm -f public/hotreload + [[ -z "$PID" ]] || kill -15 $PID +} +trap cleanup EXIT + +sync +while inotifywait -r -e modify src/ template/; do + sync +done diff --git a/public/hotreload.js b/public/hotreload.js new file mode 100644 index 0000000..9fe8b8b --- /dev/null +++ b/public/hotreload.js @@ -0,0 +1,16 @@ +const scroll = (y) => window.scrollTo(0, 925*y) +async function hotReload() { + console.log("running in development mode") + const r = await fetch("/hotreload") + let x = await r.text() + setInterval(async () => { + const r = await fetch("/hotreload", { + cache: "no-cache" + }) + if (x !== await r.text()) { + x = r.body + window.location.reload() + } + }, 1000) +} +hotReload().catch(console.error) \ No newline at end of file diff --git a/src/router.go b/src/router.go index f9d007b..cdb63ea 100644 --- a/src/router.go +++ b/src/router.go @@ -57,6 +57,7 @@ func index(c echo.Context) error { } data := map[string]any{ "session": c.Get("session"), + "ENV": ENV, "markets": markets, "VERSION": VERSION, "COMMIT_LONG_SHA": COMMIT_LONG_SHA} diff --git a/src/server.go b/src/server.go index 5c77a6c..b2d6f86 100644 --- a/src/server.go +++ b/src/server.go @@ -22,6 +22,7 @@ var ( VERSION string PORT int PUBLIC_URL string + ENV string ) func execCmd(name string, args ...string) string { @@ -40,6 +41,7 @@ func init() { } flag.StringVar(&PUBLIC_URL, "PUBLIC_URL", "delphi.market", "Public URL of website") flag.IntVar(&PORT, "PORT", 4321, "Server port") + flag.StringVar(&ENV, "ENV", "development", "Specify for which environment files should be built") flag.Parse() e = echo.New() t = &Template{ @@ -50,6 +52,7 @@ func init() { VERSION = fmt.Sprintf("v0.0.0+%s", COMMIT_SHORT_SHA) log.Printf("Running commit %s", COMMIT_SHORT_SHA) log.Printf("Public URL: %s", PUBLIC_URL) + log.Printf("Environment: %s", ENV) } func main() { diff --git a/template/binary_market.html b/template/binary_market.html index b2da54a..4d16b9d 100644 --- a/template/binary_market.html +++ b/template/binary_market.html @@ -11,6 +11,9 @@ + {{ if eq .ENV "development" }} + + {{ end }}