From 4bd3127ccf3568a4509e09ee50cf02c47bfdd78b Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Thu, 4 Apr 2024 09:05:00 +0200 Subject: [PATCH] ws: go http server --- .gitignore | 1 + README.md | 17 ++++------------- ws/go.mod | 3 +++ ws/main.go | 25 +++++++++++++++++++++++++ 4 files changed, 33 insertions(+), 13 deletions(-) create mode 100644 .gitignore create mode 100644 ws/go.mod create mode 100644 ws/main.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1b556a6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/ws/ws diff --git a/README.md b/README.md index 059ca59..8c5228a 100644 --- a/README.md +++ b/README.md @@ -46,20 +46,11 @@ Lightpanda browser, but the code is not publicly available yet. Clone the [demo web page](https://github.com/lightpanda-io/demo) and expose the `public/` directory locally with a web server. -We use a simple Go program to expose the files. +We use the simple Go program to expose the files in `ws/` dir. +By default it exposes the `public` dir using the `1234` port. -```go -package main - -import ( - "log" - "net/http" -) - -func main() { - // Simple static webserver: - log.Fatal(http.ListenAndServe(":1234", http.FileServer(http.Dir("")))) -} +```console +$ go run ws/main.go ``` ## Single request diff --git a/ws/go.mod b/ws/go.mod new file mode 100644 index 0000000..6aa84f8 --- /dev/null +++ b/ws/go.mod @@ -0,0 +1,3 @@ +module github.com/lightpanda-io/demo/ws + +go 1.22.1 diff --git a/ws/main.go b/ws/main.go new file mode 100644 index 0000000..9750cc6 --- /dev/null +++ b/ws/main.go @@ -0,0 +1,25 @@ +package main + +import ( + "fmt" + "log" + "net/http" + "os" +) + +func main() { + address := os.Getenv("WS_ADDRESS") + if address == "" { + address = ":1234" + } + + dir := os.Getenv("WS_DIR") + if dir == "" { + dir = "public" + } + + fmt.Fprintf(os.Stderr, "expose dir: %q\nlisten: %q\n", dir, address) + + // Simple static webserver: + log.Fatal(http.ListenAndServe(address, http.FileServer(http.Dir(dir)))) +}