Overture to move to gcloud

This commit is contained in:
Neale Pickett 2020-05-03 09:37:50 -06:00
parent 8ed3cd7b11
commit ac08b063fc
5 changed files with 68 additions and 2 deletions

25
.gcloudignore Normal file
View File

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

20
app.yaml Normal file
View File

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

5
go.mod Normal file
View File

@ -0,0 +1,5 @@
module github.com/nealey/vail
go 1.12
require golang.org/x/net v0.0.0-20200501053045-e0ff5e5a1de5

18
main.go
View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"os"
"log" "log"
"net/http" "net/http"
"golang.org/x/net/websocket" "golang.org/x/net/websocket"
@ -13,10 +14,12 @@ type Client struct {
} }
func (c Client) Handle(ws *websocket.Conn) { func (c Client) Handle(ws *websocket.Conn) {
log.Println("c.Handle hooray")
ws.MaxPayloadBytes = 500 ws.MaxPayloadBytes = 500
book.Join(c.repeaterName, ws) book.Join(c.repeaterName, ws)
defer book.Part(c.repeaterName, ws) defer book.Part(c.repeaterName, ws)
log.Println("for loop")
for { for {
buf := make([]byte, ws.MaxPayloadBytes) buf := make([]byte, ws.MaxPayloadBytes)
@ -31,21 +34,34 @@ func (c Client) Handle(ws *websocket.Conn) {
} }
func ChatHandler(w http.ResponseWriter, r *http.Request) { func ChatHandler(w http.ResponseWriter, r *http.Request) {
log.Print("Handling chat")
c := Client { c := Client {
repeaterName: r.FormValue("repeater"), repeaterName: r.FormValue("repeater"),
} }
// This API is confusing as hell. // This API is confusing as hell.
// I suspect there's a better way to do this. // I suspect there's a better way to do this.
log.Print("Web Socketing")
websocket.Handler(c.Handle).ServeHTTP(w, r) websocket.Handler(c.Handle).ServeHTTP(w, r)
} }
func hello(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello world"))
}
func main() { func main() {
book = NewBook() book = NewBook()
http.Handle("/chat", http.HandlerFunc(ChatHandler)) http.Handle("/chat", http.HandlerFunc(ChatHandler))
http.Handle("/hello", http.HandlerFunc(hello))
http.Handle("/", http.FileServer(http.Dir("static"))) http.Handle("/", http.FileServer(http.Dir("static")))
go book.Run() 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 { if err != nil {
log.Fatal(err.Error()) log.Fatal(err.Error())
} }

View File

@ -221,7 +221,7 @@ class Vail {
// Set up WebSocket // Set up WebSocket
let wsUrl = new URL(window.location) let wsUrl = new URL(window.location)
wsUrl.protocol = "ws:" wsUrl.protocol = wsUrl.protocol.replace("http", "ws")
wsUrl.pathname += "chat" wsUrl.pathname += "chat"
this.socket = new WebSocket(wsUrl) this.socket = new WebSocket(wsUrl)
this.socket.addEventListener("message", e => this.wsMessage(e)) this.socket.addEventListener("message", e => this.wsMessage(e))