Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[question] Simple server like five-server-vscode #103

Open
heysokam opened this issue Jan 9, 2024 · 5 comments
Open

[question] Simple server like five-server-vscode #103

heysokam opened this issue Jan 9, 2024 · 5 comments

Comments

@heysokam
Copy link

heysokam commented Jan 9, 2024

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.

@ThomasTJdev
Copy link
Contributor

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

@heysokam
Copy link
Author

heysokam commented Jan 10, 2024

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

@heysokam
Copy link
Author

heysokam commented Jan 10, 2024

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

image

@ThomasTJdev
Copy link
Contributor

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"

@heysokam
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants