Add hot reload for development
This commit is contained in:
		
							parent
							
								
									655ccb4a9b
								
							
						
					
					
						commit
						bf8717e2b1
					
				
							
								
								
									
										5
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										5
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							@ -1,4 +1,9 @@
 | 
				
			|||||||
# go executable
 | 
					# go executable
 | 
				
			||||||
delphi.market
 | 
					delphi.market
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# environment
 | 
				
			||||||
.env
 | 
					.env
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# required for hot reload
 | 
				
			||||||
 | 
					public/hotreload
 | 
				
			||||||
 | 
					*.log
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										29
									
								
								hotreload.sh
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								hotreload.sh
									
									
									
									
									
										Normal 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
									
								
							
							
						
						
									
										16
									
								
								public/hotreload.js
									
									
									
									
									
										Normal 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)
 | 
				
			||||||
@ -57,6 +57,7 @@ func index(c echo.Context) error {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
	data := map[string]any{
 | 
						data := map[string]any{
 | 
				
			||||||
		"session":         c.Get("session"),
 | 
							"session":         c.Get("session"),
 | 
				
			||||||
 | 
							"ENV":             ENV,
 | 
				
			||||||
		"markets":         markets,
 | 
							"markets":         markets,
 | 
				
			||||||
		"VERSION":         VERSION,
 | 
							"VERSION":         VERSION,
 | 
				
			||||||
		"COMMIT_LONG_SHA": COMMIT_LONG_SHA}
 | 
							"COMMIT_LONG_SHA": COMMIT_LONG_SHA}
 | 
				
			||||||
 | 
				
			|||||||
@ -22,6 +22,7 @@ var (
 | 
				
			|||||||
	VERSION          string
 | 
						VERSION          string
 | 
				
			||||||
	PORT             int
 | 
						PORT             int
 | 
				
			||||||
	PUBLIC_URL       string
 | 
						PUBLIC_URL       string
 | 
				
			||||||
 | 
						ENV              string
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func execCmd(name string, args ...string) 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.StringVar(&PUBLIC_URL, "PUBLIC_URL", "delphi.market", "Public URL of website")
 | 
				
			||||||
	flag.IntVar(&PORT, "PORT", 4321, "Server port")
 | 
						flag.IntVar(&PORT, "PORT", 4321, "Server port")
 | 
				
			||||||
 | 
						flag.StringVar(&ENV, "ENV", "development", "Specify for which environment files should be built")
 | 
				
			||||||
	flag.Parse()
 | 
						flag.Parse()
 | 
				
			||||||
	e = echo.New()
 | 
						e = echo.New()
 | 
				
			||||||
	t = &Template{
 | 
						t = &Template{
 | 
				
			||||||
@ -50,6 +52,7 @@ func init() {
 | 
				
			|||||||
	VERSION = fmt.Sprintf("v0.0.0+%s", COMMIT_SHORT_SHA)
 | 
						VERSION = fmt.Sprintf("v0.0.0+%s", COMMIT_SHORT_SHA)
 | 
				
			||||||
	log.Printf("Running commit %s", COMMIT_SHORT_SHA)
 | 
						log.Printf("Running commit %s", COMMIT_SHORT_SHA)
 | 
				
			||||||
	log.Printf("Public URL: %s", PUBLIC_URL)
 | 
						log.Printf("Public URL: %s", PUBLIC_URL)
 | 
				
			||||||
 | 
						log.Printf("Environment: %s", ENV)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func main() {
 | 
					func main() {
 | 
				
			||||||
 | 
				
			|||||||
@ -11,6 +11,9 @@
 | 
				
			|||||||
    <link rel="stylesheet" href="/market.css" />
 | 
					    <link rel="stylesheet" href="/market.css" />
 | 
				
			||||||
    <meta name="viewport" content="width=device-width, initial-scale=1" />
 | 
					    <meta name="viewport" content="width=device-width, initial-scale=1" />
 | 
				
			||||||
    <meta name="theme-color" content="#091833" />
 | 
					    <meta name="theme-color" content="#091833" />
 | 
				
			||||||
 | 
					    {{ if eq .ENV "development" }}
 | 
				
			||||||
 | 
					    <script defer src="/hotreload.js"></script>
 | 
				
			||||||
 | 
					    {{ end }}
 | 
				
			||||||
  </head>
 | 
					  </head>
 | 
				
			||||||
  <body>
 | 
					  <body>
 | 
				
			||||||
    <header class="flex flex-row text-center justify-center pt-1">
 | 
					    <header class="flex flex-row text-center justify-center pt-1">
 | 
				
			||||||
 | 
				
			|||||||
@ -10,6 +10,9 @@
 | 
				
			|||||||
    <link rel="stylesheet" href="/index.css" />
 | 
					    <link rel="stylesheet" href="/index.css" />
 | 
				
			||||||
    <meta name="viewport" content="width=device-width, initial-scale=1" />
 | 
					    <meta name="viewport" content="width=device-width, initial-scale=1" />
 | 
				
			||||||
    <meta name="theme-color" content="#091833" />
 | 
					    <meta name="theme-color" content="#091833" />
 | 
				
			||||||
 | 
					    {{ if eq .ENV "development" }}
 | 
				
			||||||
 | 
					    <script defer src="/hotreload.js"></script>
 | 
				
			||||||
 | 
					    {{ end }}
 | 
				
			||||||
  </head>
 | 
					  </head>
 | 
				
			||||||
  <body>
 | 
					  <body>
 | 
				
			||||||
    <header class="flex flex-row text-center justify-center pt-1">
 | 
					    <header class="flex flex-row text-center justify-center pt-1">
 | 
				
			||||||
 | 
				
			|||||||
@ -10,6 +10,9 @@
 | 
				
			|||||||
    <link rel="stylesheet" href="/index.css" />
 | 
					    <link rel="stylesheet" href="/index.css" />
 | 
				
			||||||
    <meta name="viewport" content="width=device-width, initial-scale=1" />
 | 
					    <meta name="viewport" content="width=device-width, initial-scale=1" />
 | 
				
			||||||
    <meta name="theme-color" content="#091833" />
 | 
					    <meta name="theme-color" content="#091833" />
 | 
				
			||||||
 | 
					    {{ if eq .ENV "development" }}
 | 
				
			||||||
 | 
					    <script defer src="/hotreload.js"></script>
 | 
				
			||||||
 | 
					    {{ end }}
 | 
				
			||||||
  </head>
 | 
					  </head>
 | 
				
			||||||
  <body>
 | 
					  <body>
 | 
				
			||||||
    <header class="flex flex-row text-center justify-center pt-1">
 | 
					    <header class="flex flex-row text-center justify-center pt-1">
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user