Add hot reload for development

This commit is contained in:
ekzyis 2023-09-09 22:52:50 +02:00
parent 655ccb4a9b
commit bf8717e2b1
8 changed files with 63 additions and 0 deletions

5
.gitignore vendored
View File

@ -1,4 +1,9 @@
# go executable
delphi.market
# environment
.env
# required for hot reload
public/hotreload
*.log

29
hotreload.sh Normal file
View File

@ -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

16
public/hotreload.js Normal file
View File

@ -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)

View File

@ -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}

View File

@ -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() {

View File

@ -11,6 +11,9 @@
<link rel="stylesheet" href="/market.css" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#091833" />
{{ if eq .ENV "development" }}
<script defer src="/hotreload.js"></script>
{{ end }}
</head>
<body>
<header class="flex flex-row text-center justify-center pt-1">

View File

@ -10,6 +10,9 @@
<link rel="stylesheet" href="/index.css" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#091833" />
{{ if eq .ENV "development" }}
<script defer src="/hotreload.js"></script>
{{ end }}
</head>
<body>
<header class="flex flex-row text-center justify-center pt-1">

View File

@ -10,6 +10,9 @@
<link rel="stylesheet" href="/index.css" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#091833" />
{{ if eq .ENV "development" }}
<script defer src="/hotreload.js"></script>
{{ end }}
</head>
<body>
<header class="flex flex-row text-center justify-center pt-1">