mummy icon indicating copy to clipboard operation
mummy copied to clipboard

[question] Simple server like `five-server-vscode`

Open heysokam opened this issue 2 years ago • 5 comments

Hi there I'm trying to create a simple localdev server similar to nimhttpd, but using mummy. The goal is to create something similar to https://marketplace.visualstudio.com/items?itemName=yandeu.five-server

I have no experience at all creating an http server, so looking for some guidance.

  • Which of the examples would be the closest to this idea?
  • Can you list the tools from this library that I would need to be concerned with specifically?

To narrow it further, the goal is not to make a production-ready server. Just a local dev server that serves all files from a folder and automatically refreshes when any file changes, that's all.

heysokam avatar Jan 09 '24 07:01 heysokam

Here is an idea to get started (using my own mummy-utility, you can just copy-paste the sendFile)

mummy_folder
  - mummy_server.nim
  - files
    - image.jpg 
# mummy_server.nim
import mummy, mummy/routers
import mummy_utils # https://github.com/ThomasTJdev/mummy_utils

proc serveFiles(request: Request, details: Details) =
  sendFile("files/" & @"filename")

var router: Router
router.routerSet(HttpGet, "/files/images/@filename", serveFiles)

let server = newServer(router)
echo "Serving on http://localhost:8080"
server.serve(Port(8080))
curl localhost:8080/files/images/image.jpg

ThomasTJdev avatar Jan 09 '24 21:01 ThomasTJdev

@ThomasTJdev tysm! How does this work in the context of serving html pages?

heysokam avatar Jan 10 '24 02:01 heysokam

Managed to make the basic index.html load :partying_face: Now the question is how to make it redirect / to /index.html :thinking: Any tips on how to proceed?

image

heysokam avatar Jan 10 '24 03:01 heysokam

You can make a redirect on the / to index.html route, or just check that the url == "/" and then serve the index.html.

proc redirectUno(request: Request) =
  if request.uri == "/":
    # request.respond(200, @[("Content-Type", newMimetypes().getMimetype(path.split(".")[^1]))], fileBody)
    sendFile("index.html")
  else:
    echo "something else"
    
proc redirectDuo(request: Request) =
  if request.uri == "/":
    # request.respond(303, @[("Location", "index.html")])
    redirect("/index.html")
  else:
    echo "something else"

ThomasTJdev avatar Jan 10 '24 06:01 ThomasTJdev

Published the current effort on @heysokam/mummyd. That last recommendation is still pending, but the rest has worked so far :)

heysokam avatar Jan 13 '24 23:01 heysokam