Skip to content

Commit 3a54049

Browse files
author
Edward Muller
committed
Support different ports on the other servers
NOTE: I don't know how to do this with php
1 parent 1e30382 commit 3a54049

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

server.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,12 @@ func handleComments(w http.ResponseWriter, r *http.Request) {
8585
}
8686

8787
func main() {
88+
port := os.Getenv("PORT")
89+
if port == "" {
90+
port = "3000"
91+
}
8892
http.HandleFunc("/comments.json", handleComments)
8993
http.Handle("/", http.FileServer(http.Dir("./public")))
9094
log.Println("Server started: http://localhost:3000")
91-
log.Fatal(http.ListenAndServe(":3000", nil))
95+
log.Fatal(http.ListenAndServe(":"+port, nil))
9296
}

server.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1010

1111
import json
12+
import os
1213
from flask import Flask, Response, request
1314

1415
app = Flask(__name__, static_url_path='', static_folder='public')
@@ -29,4 +30,4 @@ def comments_handler():
2930
return Response(json.dumps(comments), mimetype='application/json', headers={'Cache-Control': 'no-cache'})
3031

3132
if __name__ == '__main__':
32-
app.run(port=3000)
33+
app.run(port=os.environ.get("PORT",3000))

server.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
require 'webrick'
1212
require 'json'
1313

14-
puts 'Server started: http://localhost:3000/'
14+
port = ENV['PORT'].nil? ? 3000 : ENV['PORT'].to_i
15+
16+
puts "Server started: http://localhost:#{port}/"
1517

1618
root = File.expand_path './public'
17-
server = WEBrick::HTTPServer.new :Port => 3000, :DocumentRoot => root
19+
server = WEBrick::HTTPServer.new :Port => port, :DocumentRoot => root
1820

1921
server.mount_proc '/comments.json' do |req, res|
2022
comments = JSON.parse(File.read('./comments.json'))

0 commit comments

Comments
 (0)