squeaky-pl / japronto

Screaming-fast Python 3.5+ HTTP toolkit integrated with pipelining HTTP server based on uvloop and picohttpparser.
MIT License
8.61k stars 581 forks source link

css and js #43

Closed jersobh closed 7 years ago

jersobh commented 7 years ago

Hi there,

I'm trying to run the html example, but js and css files get error 404

squeaky-pl commented 7 years ago

Hard to comment on the thing without a code example, make a minimal example that fails for you and explains what you wanna do and I am sure somebody will help you.

jersobh commented 7 years ago

I was running your project's example. Then I've added a js tag on the header of the html file, but it gets 404 error, on any file. I know this must be something stupid.. it's just because i'm new to python. In php it could be solved by a "FILE" or "DIR" maybe.

squeaky-pl commented 7 years ago

If you are new to Python I would suggest you try some framework that provides static file serving capabilities like Sanic, Falcon or Flask. Japronto is targeted for particular use cases of speed enthusiasts. It does not provide static file serving capabilities out of the box and you need to build all the stuff from scratch yourself. It is not yet a general purpose web framework I hope it becomes one day.

jersobh commented 7 years ago

I see. I've actually started writting this -> https://github.com/jersobh/pydge Solved the problem creating a route for the static files :+1:

Thanks

pingf commented 7 years ago

I'm trying to build a framework upon this, now I just use the code like this

from japronto import Application

async def hello(request):
    path = request.match_dict['static_file'].strip('/')

    with open(path) as html_file:
        return request.Response(text=html_file.read(), mime_type='text/html')

app = Application()

app.router.add_route('/static/{static_file}', hello)

app.run(debug=True, port=8765)

and the performance is good enough for me.

wrk -t12 -c100 -d10s http://127.0.0.1:8765/static/index.html
Running 10s test @ http://127.0.0.1:8765/static/index.html
  12 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     3.71ms  329.27us  12.56ms   96.76%
    Req/Sec     2.16k   139.07     4.55k    97.60%
  259806 requests in 10.09s, 41.13MB read
Requests/sec:  25746.55
Transfer/sec:      4.08MB
Snuggert commented 7 years ago

Because my static files were in a multi-tiered file tree i hacked something together, i'm not proud....

from japronto import Application

def content(request):
    with open(request.path[1:], 'rb') as static_file:
        return request.Response(body=static_file.read())

app = Application()

app.router.add_route('/static/{1}', static)
app.router.add_route('/static/{1}/{2}', static)
app.router.add_route('/static/{1}/{2}/{3}', static)

app.run(debug=True)

Performance for a 5kB file.

wrk -t 1 -c 100 -d 1m http://localhost:8080/static/1/2/3.file
Running 1m test @ http://localhost:8080/static/1/2/3.file
  1 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     6.32ms    2.95ms  28.84ms   77.73%
    Req/Sec    16.04k     3.97k   22.81k    54.50%
  958039 requests in 1.00m, 4.53GB read
Requests/sec:  15955.85
Transfer/sec:     77.18MB