From ac08b063fc4f8b0ad5854b549f1d20213bb9afb1 Mon Sep 17 00:00:00 2001 From: Neale Pickett Date: Sun, 3 May 2020 09:37:50 -0600 Subject: [PATCH] Overture to move to gcloud --- .gcloudignore | 25 +++++++++++++++++++++++++ app.yaml | 20 ++++++++++++++++++++ go.mod | 5 +++++ main.go | 18 +++++++++++++++++- static/vail.js | 2 +- 5 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 .gcloudignore create mode 100644 app.yaml create mode 100644 go.mod diff --git a/.gcloudignore b/.gcloudignore new file mode 100644 index 0000000..199e6d9 --- /dev/null +++ b/.gcloudignore @@ -0,0 +1,25 @@ +# This file specifies files that are *not* uploaded to Google Cloud Platform +# using gcloud. It follows the same syntax as .gitignore, with the addition of +# "#!include" directives (which insert the entries of the given .gitignore-style +# file at that point). +# +# For more information, run: +# $ gcloud topic gcloudignore +# +.gcloudignore +# If you would like to upload your .git directory, .gitignore file or files +# from your .gitignore file, remove the corresponding line +# below: +.git +.gitignore + +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib +# Test binary, build with `go test -c` +*.test +# Output of the go coverage tool, specifically when used with LiteIDE +*.out \ No newline at end of file diff --git a/app.yaml b/app.yaml new file mode 100644 index 0000000..e1f1953 --- /dev/null +++ b/app.yaml @@ -0,0 +1,20 @@ +runtime: go112 + +instance_class: B1 + +automatic_scaling: + max_instances: 1 + +handlers: + - url: /chat + script: auto + - url: /hello + script: auto + - url: / + static_files: static/index.html + upload: static/index.html + - url: /.* + static_dir: static + +network: + session_affinity: true diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..0400ad7 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module github.com/nealey/vail + +go 1.12 + +require golang.org/x/net v0.0.0-20200501053045-e0ff5e5a1de5 diff --git a/main.go b/main.go index d6048bd..d5b0586 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "os" "log" "net/http" "golang.org/x/net/websocket" @@ -13,10 +14,12 @@ type Client struct { } func (c Client) Handle(ws *websocket.Conn) { + log.Println("c.Handle hooray") ws.MaxPayloadBytes = 500 book.Join(c.repeaterName, ws) defer book.Part(c.repeaterName, ws) + log.Println("for loop") for { buf := make([]byte, ws.MaxPayloadBytes) @@ -31,21 +34,34 @@ func (c Client) Handle(ws *websocket.Conn) { } func ChatHandler(w http.ResponseWriter, r *http.Request) { + log.Print("Handling chat") c := Client { repeaterName: r.FormValue("repeater"), } // This API is confusing as hell. // I suspect there's a better way to do this. + log.Print("Web Socketing") websocket.Handler(c.Handle).ServeHTTP(w, r) } +func hello(w http.ResponseWriter, r *http.Request) { + w.Write([]byte("Hello world")) +} + func main() { book = NewBook() http.Handle("/chat", http.HandlerFunc(ChatHandler)) + http.Handle("/hello", http.HandlerFunc(hello)) http.Handle("/", http.FileServer(http.Dir("static"))) go book.Run() - err := http.ListenAndServe(":8080", nil) + + port := os.Getenv("PORT") + if port == "" { + port = "8080" + } + log.Println("Listening on port", port) + err := http.ListenAndServe(":" + port, nil) if err != nil { log.Fatal(err.Error()) } diff --git a/static/vail.js b/static/vail.js index 549534a..af66724 100644 --- a/static/vail.js +++ b/static/vail.js @@ -221,7 +221,7 @@ class Vail { // Set up WebSocket let wsUrl = new URL(window.location) - wsUrl.protocol = "ws:" + wsUrl.protocol = wsUrl.protocol.replace("http", "ws") wsUrl.pathname += "chat" this.socket = new WebSocket(wsUrl) this.socket.addEventListener("message", e => this.wsMessage(e))