From 67c5efe8179488172a4c0e50c3b84ea03baa268a Mon Sep 17 00:00:00 2001 From: brian-nguyen Date: Mon, 24 Dec 2018 14:27:51 -0800 Subject: [PATCH 1/5] Add support for HTTPs in dev and production via nginx --- .gitignore | 1 + Makefile | 19 ++++++++++++++++ config/nginx.conf | 58 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 config/nginx.conf diff --git a/.gitignore b/.gitignore index 43c6bbc..b9e0919 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ **npm-debug.log **.env* +**.pem* /client/node_modules/ diff --git a/Makefile b/Makefile index 9e9b1ae..8264da9 100644 --- a/Makefile +++ b/Makefile @@ -16,6 +16,25 @@ deploy: make bundle git subtree push --prefix client/public origin gh-pages +# Generate self-signed key and certificate +.PHONY: https +https: + openssl req -x509 -newkey rsa:4096 -keyout config/key.pem -out config/cert.pem -days 365 -nodes + +# Run NGINX container in daemon mode +# KNOWN ISSUE: Docker for Mac/Windows runs in VM --network host will not work as expected +.PHONY: nginx +nginx: + docker run -d --rm \ + --name bumper_nginx \ + -v $(PWD)/web:/usr/share/nginx/html \ + -v $(PWD)/config/nginx.conf:/etc/nginx/nginx.conf \ + -v $(PWD)/config/cert.pem:/etc/nginx/ssl/nginx.crt \ + -v $(PWD)/config/key.pem:/etc/nginx/ssl/nginx.key \ + # -p 80:80 \ # Development setting + --network host \ # Production setting + nginx:alpine + # Build and run Bumper in daemon mode .PHONY: bumper DATABASE_URL=https://bumperdevdb.firebaseio.com diff --git a/config/nginx.conf b/config/nginx.conf new file mode 100644 index 0000000..1b13a0b --- /dev/null +++ b/config/nginx.conf @@ -0,0 +1,58 @@ +user nginx; + +worker_processes auto; +worker_rlimit_nofile 8192; + +events { + worker_connections 8000; + multi_accept on; + use epoll; +} + +error_log /var/log/nginx/error.log warn; + +pid /var/run/nginx.pid; + +http { + gzip on; + gzip_http_version 1.0; + gzip_comp_level 5; + gzip_min_length 256; + gzip_proxied any; + gzip_vary on; + gzip_types + application/atom+xml + application/javascript + application/json + application/rss+xml + application/vnd.ms-fontobject + application/x-font-ttf + application/x-web-app-manifest+json + application/xhtml+xml + application/xml + font/opentype + image/svg+xml + image/x-icon + text/css + text/plain + text/x-component; + + server { + root /usr/share/nginx/html; + + listen 80; + listen [::]:80; + + listen 443 ssl; + + server_name localhost; + ssl_certificate /etc/nginx/ssl/nginx.crt; + ssl_certificate_key /etc/nginx/ssl/nginx.key; + + # KNOWN ISSUE - Docker for Mac/Windows runs in VM + # Change localhost below to your machine's IP + location / { + proxy_pass http://localhost:9090/; + } + } +} \ No newline at end of file From b70fed91a93ece824c888aba97a0095738729d94 Mon Sep 17 00:00:00 2001 From: brian-nguyen Date: Mon, 24 Dec 2018 14:34:33 -0800 Subject: [PATCH 2/5] Add more detail to comment in nginx.conf --- Makefile | 6 +++--- config/nginx.conf | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 8264da9..2b246ac 100644 --- a/Makefile +++ b/Makefile @@ -23,16 +23,16 @@ https: # Run NGINX container in daemon mode # KNOWN ISSUE: Docker for Mac/Windows runs in VM --network host will not work as expected +# Replace --network host with -p 80:80 and -p 443:443 when in development .PHONY: nginx nginx: docker run -d --rm \ --name bumper_nginx \ - -v $(PWD)/web:/usr/share/nginx/html \ -v $(PWD)/config/nginx.conf:/etc/nginx/nginx.conf \ -v $(PWD)/config/cert.pem:/etc/nginx/ssl/nginx.crt \ -v $(PWD)/config/key.pem:/etc/nginx/ssl/nginx.key \ - # -p 80:80 \ # Development setting - --network host \ # Production setting + -p 80:80 \ + -p 443:443 \ nginx:alpine # Build and run Bumper in daemon mode diff --git a/config/nginx.conf b/config/nginx.conf index 1b13a0b..a7b60c1 100644 --- a/config/nginx.conf +++ b/config/nginx.conf @@ -49,7 +49,7 @@ http { ssl_certificate /etc/nginx/ssl/nginx.crt; ssl_certificate_key /etc/nginx/ssl/nginx.key; - # KNOWN ISSUE - Docker for Mac/Windows runs in VM + # KNOWN ISSUE - Docker for Mac/Windows runs in VM --network host will not work as expected # Change localhost below to your machine's IP location / { proxy_pass http://localhost:9090/; From 750def36474746a9632a72415af93b248b68beaf Mon Sep 17 00:00:00 2001 From: brian-nguyen Date: Mon, 24 Dec 2018 14:37:19 -0800 Subject: [PATCH 3/5] Default Makefile to production config --- Makefile | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 2b246ac..d74fa2d 100644 --- a/Makefile +++ b/Makefile @@ -31,8 +31,7 @@ nginx: -v $(PWD)/config/nginx.conf:/etc/nginx/nginx.conf \ -v $(PWD)/config/cert.pem:/etc/nginx/ssl/nginx.crt \ -v $(PWD)/config/key.pem:/etc/nginx/ssl/nginx.key \ - -p 80:80 \ - -p 443:443 \ + --network host \ nginx:alpine # Build and run Bumper in daemon mode From c4e70111788e4d05e948d03b3c9e6805bb1fcc90 Mon Sep 17 00:00:00 2001 From: brian-nguyen Date: Mon, 24 Dec 2018 14:37:28 -0800 Subject: [PATCH 4/5] Set default PORT if not specified --- server/main.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/server/main.go b/server/main.go index 11e100e..52d049f 100644 --- a/server/main.go +++ b/server/main.go @@ -29,19 +29,19 @@ func getLobby(w http.ResponseWriter, r *http.Request) { func main() { rand.Seed(time.Now().UTC().UnixNano()) + PORT := os.Getenv("PORT") + if PORT == "" { + PORT = "9090" + } + arena.MessageChannel = make(chan models.Message) game := game.CreateGame() - // database.ConnectDB("service-account.json") - // if database.DBC == nil { - // log.Println("DBClient not initialized correctly") - // } - http.Handle("/", http.FileServer(http.Dir("./build"))) http.HandleFunc("/start", getLobby) http.Handle("/connect", game) game.StartGame() - log.Println("Starting server on localhost:" + os.Getenv("PORT")) - log.Println(http.ListenAndServe(":"+os.Getenv("PORT"), nil)) + log.Println("Starting server on localhost:" + PORT) + log.Println(http.ListenAndServe(":"+PORT, nil)) } From 3dd68c76727d6c7b0a5daa4445d62c3bdb2e230b Mon Sep 17 00:00:00 2001 From: brian-nguyen Date: Mon, 24 Dec 2018 14:46:03 -0800 Subject: [PATCH 5/5] Update server port for Bumper container --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index d74fa2d..5cd6f87 100644 --- a/Makefile +++ b/Makefile @@ -46,7 +46,7 @@ bumper: -e DATABASE_URL=$(DATABASE_URL) \ -e PORT=$(SERVER_PORT) \ -v $(PWD)/client/public:/app/build \ - -p 80:$(SERVER_PORT) \ + -p $(SERVER_PORT):$(SERVER_PORT) \ bumper # Starts a file server in the web/ directory