bung87 / scorper

scorper is a micro and elegant web framework written in Nim
Apache License 2.0
81 stars 5 forks source link

[docs] serving static files #22

Closed ITwrx closed 2 years ago

ITwrx commented 3 years ago

I'm probably missing something fundamental since i haven't used nim in a few months, but i was not able to serve static files until importing os and adding putEnv("StaticDir","./static") to the main block in main file. I tried different locations for static.nims and paths as the StaticDir in static.nims, but couldn't get that to help anything.

The below is what worked for me.

My static files are in a /var/www/appname/static directory.

The main app file is /var/www/appname/appname.nim

import scorper
import os

when isMainModule:
  type AsyncCallback = proc (request: Request): Future[void] {.closure, gcsafe.}

  let r = newRouter[AsyncCallback]()
  r.addRoute(serveStatic, "get", "/static/*$")
  let address = "127.0.0.1:8888"
  putEnv("StaticDir","./static")
  let flags = {ReuseAddr}
  var server = newScorper(address, r, flags)
  server.start()
  waitFor server.join()

What am i missing? or maybe the docs for static files can be improved?

Thanks

bung87 commented 3 years ago

I tested it works as expected , use . is expecting your cwd that run your nim file as static dir, if you run appname.nim in /var/www/appname/ I think that's all fine. both putEnv in .nim file and in .nims file in your case appname.nims

bung87 commented 3 years ago

I've add proc doc comment ## Relys onStaticDirenvironment variable , the chronos have some problem with nimdoc so I can't just generate an api document on GitHub page.

ITwrx commented 3 years ago

i hadn't tried putting static files loose in the root dir. For me, scorper will serve any static files in the project root dir regardless of what i have for StaticDir in static.nims. Also, it won't serve any static files if i put them in a /static dir even if i put ./static as StaticDir in static.nims. Serving static files from a subdirectory only works if i add putEnv("StaticDir","./static") to my main app file, as detailed above. Having static files loose in project root dir is not desirable to me, and it doesn't seem like the documented config method works for serving anywhere except the project root dir.

bung87 commented 3 years ago

In your case is appname.nims ,use . Depends on your cwd. rest of url static/ will match files under static dir , that’s how it works

ITwrx commented 3 years ago

ok, (i'm still not sure what you're trying to tell me) but when i change the StaticDir to ./static in static.nims it doesn't respect that change. It only serves in cwd of appname.nim (project root). How are you supposed to use a dedicated directory for static files, using the static.nims file in your example and/or besides how i did it above?

My point is not that your code doesn't work, but that i don't understand your docs.

Maybe you could just show how to use a dedicated static dir (not loose files in project root) in the example? I can just use the method i found, but i just posted this issue so you would know it caused/causes me problems. Maybe other users will have trouble too. Maybe it's just me.

bung87 commented 3 years ago

Okay , I get it . I am starting worry about writing document myself...