GithubHelp home page GithubHelp logo

Routing/Endpoint DSL about midas HOT 2 OPEN

midas-framework avatar midas-framework commented on May 10, 2024
Routing/Endpoint DSL

from midas.

Comments (2)

nono avatar nono commented on May 10, 2024

I don't have a lot of experiences with functional programming, but the router makes me think to pattern matching:

// users.gleam
import midas

fn list() -> midas.Response {
  // TODO return a midas.Response with the list of users
}

fn get_post(user_id: String, post_id: String) -> midas.Response {
  let published = midas.QueryParam(req, "published")
  // TODO return a midas.Response with the given post

pub fn router(req: midas.Request, path: List(String)) -> midas.Response {
  let is_user_id = midas.String([midas.MinLength(14), midas.MaxLength(14)])
  let is_post_id = midas.Integer([midas.Max(999)])
  case [req.Verb, path] {
    [midas.Get, []] -> list(req, res)
    [midas.Get, [user_id, "posts", post_id]] if is_user_id(user_id) && is_post_id(post_id) -> get_post(user_id, post_id)
    _ -> midas.error404(req)
  }
}

// router.gleam
import midas
import my_app/users

pub fn router(req: midas.Request) -> midas.Response {
  case req.SplittedPath {
    ["users" | rest] -> users.router(req, rest)
    _ -> midas.error404(req)
  }
}

A limitation is that I don't know how to make post_id an Int (and not just a String to parse).

Maybe it can give you some ideas.

Update: I've read again the README, and I feel stupid to suggest what is already there. Let's just say that I prefer what is in the README to the other suggestions.

from midas.

CrowdHailer avatar CrowdHailer commented on May 10, 2024

The pattern matching solution is nice, it's certainly the most simple.
You hit the nail on the head about the integers though. The DSL suggested here gives you more type safety but potentially not worth the overhead.

As mentioned, it can exist as an experimental API for a while because the option of a simple case statement and matching will always exist.

from midas.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.