laneya2

cave exploration game
git clone https://git.ce9e.org/laneya2.git

commit
752095b992d18b0e77e9d3ed9fa4aa1978c26676
parent
4a8917ab9de3c2e4a1780629802b07a355ebca4c
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2024-09-21 07:49
do not inline static files

Diffstat

M Makefile 6 +++---
M index.html 4 ++--
M laneya.go 41 +++++++++--------------------------------
R main.js -> static/main.js 0
R style.css -> static/style.css 0

5 files changed, 14 insertions, 37 deletions


diff --git a/Makefile b/Makefile

@@ -1,12 +1,12 @@
    1     1 .PHONY: live
    2     2 live:
    3    -1 	printf 'laneya.go\nindex.html\nstyle.css\nmain.js\n' | entr -r make lint run
   -1     3 	echo laneya.go | entr -r make lint run
    4     4 
    5     5 .PHONY: run
    6     6 run: laneya
    7    -1 	./laneya -v
   -1     7 	./laneya -v -s
    8     8 
    9    -1 laneya: laneya.go index.html style.css main.js
   -1     9 laneya: laneya.go
   10    10 	go build $<
   11    11 
   12    12 .PHONY: lint

diff --git a/index.html b/index.html

@@ -5,10 +5,10 @@
    5     5 	<meta name="viewport" content="width=device-width, initial-scale=1">
    6     6 	<meta http-equiv="Content-Security-Policy" content="default-src 'self'">
    7     7 	<title>Laneya</title>
    8    -1 	<link rel="stylesheet" type="text/css" href="style.css">
   -1     8 	<link rel="stylesheet" type="text/css" href="/static/style.css">
    9     9 </head>
   10    10 <body>
   11    11 	<pre role="application"></pre>
   12    -1 	<script src="main.js" type="module"></script>
   -1    12 	<script src="/static/main.js" type="module"></script>
   13    13 </body>
   14    14 </html>

diff --git a/laneya.go b/laneya.go

@@ -2,7 +2,6 @@ package main
    2     2 
    3     3 import (
    4     4 	"context"
    5    -1 	_ "embed"
    6     5 	"flag"
    7     6 	"fmt"
    8     7 	"log"
@@ -18,17 +17,9 @@ import (
   18    17 	"github.com/gorilla/websocket"
   19    18 )
   20    19 
   21    -1 //go:embed index.html
   22    -1 var html []byte
   23    -1 
   24    -1 //go:embed style.css
   25    -1 var css []byte
   26    -1 
   27    -1 //go:embed main.js
   28    -1 var js []byte
   29    -1 
   30    20 var upgrader = websocket.Upgrader{}
   31    21 var verbose = false
   -1    22 var static = false
   32    23 
   33    24 type Point struct {
   34    25 	X int `json:"x"`
@@ -330,24 +321,6 @@ func (player *Player) writePump() {
  330   321 	}
  331   322 }
  332   323 
  333    -1 func serveHome(w http.ResponseWriter, r *http.Request) {
  334    -1 	w.Header().Set("Content-Type", "text/html")
  335    -1 	w.WriteHeader(http.StatusOK)
  336    -1 	w.Write(html)
  337    -1 }
  338    -1 
  339    -1 func serveCSS(w http.ResponseWriter, r *http.Request) {
  340    -1 	w.Header().Set("Content-Type", "text/css")
  341    -1 	w.WriteHeader(http.StatusOK)
  342    -1 	w.Write(css)
  343    -1 }
  344    -1 
  345    -1 func serveJS(w http.ResponseWriter, r *http.Request) {
  346    -1 	w.Header().Set("Content-Type", "text/javascript")
  347    -1 	w.WriteHeader(http.StatusOK)
  348    -1 	w.Write(js)
  349    -1 }
  350    -1 
  351   324 func serveWs(w http.ResponseWriter, r *http.Request) {
  352   325 	conn, err := upgrader.Upgrade(w, r, nil)
  353   326 	if err != nil {
@@ -377,11 +350,12 @@ func serveWs(w http.ResponseWriter, r *http.Request) {
  377   350 
  378   351 func main() {
  379   352 	flag.Usage = func() {
  380    -1 		fmt.Fprintf(os.Stderr, "laneya [-v] [port]\n")
   -1   353 		fmt.Fprintf(os.Stderr, "laneya [-v] [-s] [port]\n")
  381   354 		flag.PrintDefaults()
  382   355 	}
  383   356 
  384   357 	flag.BoolVar(&verbose, "v", false, "enable verbose logs")
   -1   358 	flag.BoolVar(&static, "s", false, "serve static files (for development)")
  385   359 	flag.Parse()
  386   360 
  387   361 	addr := "localhost:8000"
@@ -389,9 +363,12 @@ func main() {
  389   363 		addr = fmt.Sprintf("localhost:%s", flag.Args()[0])
  390   364 	}
  391   365 
  392    -1 	http.HandleFunc("GET /{$}", serveHome)
  393    -1 	http.HandleFunc("GET /style.css", serveCSS)
  394    -1 	http.HandleFunc("GET /main.js", serveJS)
   -1   366 	if static {
   -1   367 		http.HandleFunc("GET /{$}", func(w http.ResponseWriter, r *http.Request) {
   -1   368 			http.ServeFile(w, r, "index.html")
   -1   369 		})
   -1   370 		http.Handle("GET /static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
   -1   371 	}
  395   372 	http.HandleFunc("GET /ws/{id}", serveWs)
  396   373 
  397   374 	ctx, unregisterSignals := signal.NotifyContext(

diff --git a/main.js b/static/main.js

diff --git a/style.css b/static/style.css