Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for HTTPs in dev and production via nginx #262

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
**npm-debug.log

**.env*
**.pem*

/client/node_modules/

Expand Down
18 changes: 18 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,24 @@ 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
# 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)/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 \
--network host \
nginx:alpine

# Build and run Bumper in daemon mode
.PHONY: bumper
DATABASE_URL=https://bumperdevdb.firebaseio.com
Expand Down
58 changes: 58 additions & 0 deletions config/nginx.conf
Original file line number Diff line number Diff line change
@@ -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 --network host will not work as expected
# Change localhost below to your machine's IP
location / {
proxy_pass http://localhost:9090/;
}
}
}
14 changes: 7 additions & 7 deletions server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,19 @@ func getLobby(w http.ResponseWriter, r *http.Request) {
func main() {
rand.Seed(time.Now().UTC().UnixNano())

PORT := os.Getenv("PORT")
brian-nguyen marked this conversation as resolved.
Show resolved Hide resolved
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))
}