Gro GoGo
Gro GoGo

Reputation: 23

heroku webassembly. Run python server

I've developed an webassembly application.

I have an index.html:

<section id="app"></section>

<script src='/pkg/package.js'></script>

<script>
    const { render } = wasm_bindgen;
    function run() {
        render();
    }
    wasm_bindgen('/pkg/package_bg.wasm')
        .then(run)
        .catch(console.error);
</script>

I run the server by python script(serve.py):

import http.server
import os
import socketserver
import urllib

PORT = 8000


class Handler(http.server.SimpleHTTPRequestHandler):
    # Allow SPA routing by redirecting subpaths.
    def do_GET(self):
        urlparts = urllib.parse.urlparse(self.path)
        request_file_path = urlparts.path.strip('/')
        if not os.path.exists(request_file_path):
            self.path = '/'

        return http.server.SimpleHTTPRequestHandler.do_GET(self)


handler = Handler
# Add support for the WASM mime type.
handler.extensions_map.update({
    '.wasm': 'application/wasm',
})

socketserver.TCPServer.allow_reuse_address = True
with socketserver.TCPServer(("", PORT), handler) as httpd:
    httpd.allow_reuse_address = True
    print("Serving at port", PORT)
    httpd.serve_forever()

It works successfully locally

My Procfile:

web: python serve.py

requirements.txt is empty.

Now, I want to deploy it to Heroku.

My steps:

  1. heroku create

  2. heroku config:set PORT=8000

  3. heroku buildpacks:set heroku/python

  4. git push heroku master

the application builds successfully:

2019-02-25T21:47:08.000000+00:00 app[api]: Build succeeded
2019-02-25T21:47:10.931203+00:00 heroku[web.1]: Starting process with command `python serve.py`
2019-02-25T21:47:12.753933+00:00 app[web.1]: Serving at port 8000

But when I try to open the website, I got the error:

2019-02-25T21:47:42.820665+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2019-02-25T21:47:42.820665+00:00 heroku[web.1]: Stopping process with SIGKILL
2019-02-25T21:47:42.916377+00:00 heroku[web.1]: Process exited with status 137
2019-02-25T21:48:11.109110+00:00 heroku[web.1]: State changed from starting to crashed
2019-02-25T21:48:11.017426+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2019-02-25T21:48:11.017426+00:00 heroku[web.1]: Stopping process with SIGKILL
2019-02-25T21:48:11.096343+00:00 heroku[web.1]: Process exited with status 137
2019-02-25T21:48:13.055253+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=whispering-savannah-32017.herokuapp.com request_id=6f484e71-69fc-462e-bd80-6ad9015c8570 fwd="109.110.75.176" dyno= connect= service= status=503 bytes= protocol=https

What I'm doing wrong?

Upvotes: 2

Views: 556

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599590

The point of binding to $PORT is that Heroku dynamically decides which port to use. You shouldn't be trying to set it as a config var, but more importantly you should be getting the value from the end l environment in your script add using that. So replace

PORT = 8000

with

PORT = os.environ["PORT"]

and remove the config:set PORT call.

Upvotes: 2

Related Questions