Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    jodersky
    @jodersky
    No blockers, someone just needs to find the time to do it :)
    jodersky
    @jodersky
    0.7.11 is out, featuring support for the latest Scala version, 3.0.0
    zetashift
    @zetashift
    Thanks for all the hard work, cask is great!
    megri
    @megri

    I just tried the new version on 3.0 and I can't replicate com-lihaoyi/cask#30 any longer. I might as well close it.

    However, one thing that struck me is that cask returns a 405 — method not allowed for undefined routes, if the method has not been used for at least one other route. It seems like this should be 404 instead?

    jodersky
    @jodersky
    might be related to this recent PR com-lihaoyi/cask#46
    cask groups all route tries per method, and the lookup errors out early, so a 405 is returned instead of a 404
    jodersky
    @jodersky
    It looks like a proper fix would be to change cask's internal routing system to include methods in the dispatch trie, instead of having one trie per method
    megri
    @megri
    I think a good structure would be something like Map[Path, Map[Method, Endpoint]]
    But paths would have to be iterated on every request for more complex paths. Perhaps that's why the dispatch trie groups by methods.
    jodersky
    @jodersky
    I'm thinking that we could fold all the per-method tries into a single one, but append the method as the final path component
    what would need to be added to the trie, is the ability to distinguish between method lookup failures and regular path failures
    we could of course also have a nested method trie for every path try, but that IMO seems more complex than augmenting the existing dispatch trie
    Anatolii Kmetiuk
    @anatoliykmetyuk
    Hi everyone, is there any chance anyone can look into: com-lihaoyi/cask#50 ?
    megri
    @megri
    @jodersky I've made a PR for #51 with the shape DispatchTrie[Map[String, …]]. It's the old structure inverted basically.
    Martin Ceronio
    @mydoghasworms

    Hi! I am new to both Scala and Cask. I am creating a small solution using Cask to run on a local machine and need a way to start and stop the server on an ad-hoc basis.

    Based on the examples in the Cask documentation, it seems that Cask is designed to always start automatically (either by extending cask.MainRoutes with the route definitions or extending cask.Main to include multiple route definitions).

    Is there a way I can embed Cask into an application, from which I can control starting and stopping it as required?

    Li Haoyi
    @lihaoyi
    @mydoghasworms look at what the Cask test suite does, and do that
    Martin Ceronio
    @mydoghasworms
    Thanks @lihaoyi
    gaminricks87
    @gaminricks87:matrix.org
    [m]

    :point_up: Edit: @lihaoyi: Hi! I am new to Scala and have been looking everywhere for something like Flask in Scala and Cask seems like the best minimalistic option.
    Is something like what the below code does possible in Cask and if so how can I implement it? Please help!

    app = Flask(name)
    @app.route("/get_emp_info", methods = ['POST'])
    def get_employee_record():
    input_data = json.loads(request.get_json())
    out_data = input_data .to_dict(orient='records')
    return jsonify(out_data)

    if name == "main":
    app.run(host='0.0.0.0', port=6123)

    Even if you can provide some link that does something very close to this, that'd be helpful. I'd basically want to be able to run it as a Scala RESTful application.

    2 replies
    gaminricks87
    @gaminricks87:matrix.org
    [m]
    Hi is it possible to set port number and host in the minimal application example type of program?
    Also why does reading a json into a spark DF and returning a spark DF back as a json string take so long with the read option json... Is there any alternatives?
    jodersky
    @jodersky
    @gaminricks87:matrix.org check out some of the examples on how cask can read JSON. You can override port and host in Main (https://github.com/com-lihaoyi/cask/blob/f3fd29a9206cee465812a80fe6bd04c6c63500c2/cask/src/cask/main/Main.scala#L36-L37). Unfortunately I can't help about the issue with Spark dataframes
    gaminricks87
    @gaminricks87:matrix.org
    [m]
    Thank you @jodersky and @Quafadas

    request: cask.Request

    How can we read this if it is a json? like in python requests.get_json()

    jodersky
    @jodersky

    You can access the request's body and give it to your JSON parser, for example

    @cask.get("/")
      def foo(req: cask.Request) = {
        val json = upickle.default.read[ujson.Value](req.bytes)
        ...
      }

    However, there are more idiomatic ways to read JSON in cask. The two that come to mind are:

    1. if you're using ujson, you can follow the example here https://com-lihaoyi.github.io/cask/#receiving-form-encoded-or-json-data
    2. for an alternate json library, you could implement your own endpoints https://com-lihaoyi.github.io/cask/#custom-endpoints
    gaminricks87
    @gaminricks87:matrix.org
    [m]
    @jodersky: Appreciate the quick response.
    I'm unable to access the request's body as you have mentioned above. Getting only readAllBytes() option
    gaminricks87
    @gaminricks87:matrix.org
    [m]
    I can access req.data which returns an object like this : io.undertow.io.UndertowInputStream@6d81f169
    jodersky
    @jodersky
    strange, the bytes method is implemented right in the request class https://github.com/com-lihaoyi/cask/blob/master/cask/src/cask/model/Params.scala#L20
    gaminricks87
    @gaminricks87:matrix.org
    [m]
    for spark i require scala 2.12 and cask latest version i require scala 2.13
    I think bcoz of older version this is not working for me
    Simon Parten
    @Quafadas

    The cask documentatino suggests that a decorator could be used to automagically serialise responses as JSON.
    "
    but you could make it e.g. automatically serialize returned objects to JSON responses via your favorite library, or serialize
    "
    Now, I've gotten this far..

      class writeToDoList(val path: String, val methods: Seq[String])
        extends cask.HttpEndpoint[Seq[Todos], Seq[String]]{    
    
        def wrapFunction(ctx: cask.Request, delegate: Delegate) = {
          delegate(Map()).map{num =>
            cask.Response(upickle.default.write(num))
          }
        }
    
        def wrapPathSegment(s: String) = Seq(s)
    
        type InputParser[T] = cask.endpoints.QueryParamReader[T]
      }

    But obviously, the end goal is extends cask.HttpEndpoint[T, Seq[String]], unfortunately, I can't seem to get the implicit in scope.

    The compiler says "implementation limitation, argument lists more than length 1 are ignored", if I try to add an implicit argument to the decorator class.
    Any suggestions?
    Otherwise, I'd have to write a decorator per return type :-/...

    pingbat
    @pingbat
    Are there any examples of using Cask with a ScalaJS frontend?
    pingbat
    @pingbat
    And with autowire and boopickle if possible! :-)
    Li Haoyi
    @lihaoyi
    @Quafadas IIRC how it works for uPickle is that it takes some type and then an implicit conversion to that type, and the implicit conversion takes the typeclass
    presumably you can do the same thing?
    Simon Parten
    @Quafadas
    @lihaoyi thankyou. I had to squint at this on and off for quite some time, but I think I have what I want now based on your hint.
    k0ala
    @k0ala
    hi all, I have a noob question about cask. I modified the MinimalApplication (0.7.8) to run on port 8484, and can only connect to it on localhost (from within the Ubuntu 20.04 VM where cask is running), but not from outside. However, if I run another http server (such as a with python -m http.server 8484) I can connect fine. Any hints?
    k0ala
    @k0ala
    Ah, I notice that if I override host to "0.0.0.0" I can access from the outside. Not sure if that's recommended, but at least it allows me to try out cask which was my objective :)
    k0ala
    @k0ala
    I am trying to create a route to upload an image file, but so far it's not working: @cask.postForm("/upload") def uploadFile(image: cask.FormFile) = { image.fileName }
    This yields an error when trying to post: scala> requests.post("http://localhost:8080/upload", data=java.nio.file.Paths.get("cat1.jpg")) requests.RequestFailedException: Request to http://localhost:8080/upload failed with status code 400 Unable to parse form data: java.lang.NullPointerException: Cannot invoke "io.undertow.server.handlers.form.FormDataParser.parseBlocking()" because the return value of "io.undertow.server.handlers.form.FormParserFactory.createParser(io.undertow.server.HttpServerExchange)" is null
    Any hints?
    1 reply
    Li Haoyi
    @lihaoyi
    @k0ala looking at the test suite https://github.com/com-lihaoyi/cask/blob/f3fd29a9206cee465812a80fe6bd04c6c63500c2/example/formJsonPost/app/test/src/ExampleTests.scala#L42-L48, cask.FormFile requires you to use the requests.MultiPart data structure to upload, rather than a raw data =
    1 reply
    k0ala
    @k0ala
    @lihaoyi thank you! I guess it doesn't help that I am an http and web noob (in addition to being a cask noob), I only know scala and decided it was time to learn how to make a website :) Btw, do you plan to refresh the "hands-on-scalajs" website sometime? It is very nicely written but outdated
    Li Haoyi
    @lihaoyi
    (the test suite has a lot of good stuff in general; basically every route in every example has a test case that calls it, and AFAIK all of them should pass)
    haha yeah I can tell, localhost vs 0.0.0.0 is something I'd expect non-scala-web-developers to know :)
    I don't have any concrete plans for updating hands-on-scala-js; I'm still a bit burnt out writing hands-on-scala-programming in 2019-2020 era, so not sure I'd be able to pick up a big project like that
    I wrote hands-on-scala-js when my curiosity was stronger, my standards were lower, and my non-work responsibilities were much fewer haha
    k0ala
    @k0ala
    yeah that's understandable, it blows my mind how many things you've produced
    Li Haoyi
    @lihaoyi
    it could definitely do with a refresh, e.g. using Mill instead of SBT, using Cask instead of Akka-HTTP, using DefinitelyScala facades, using some other OSS projects like Slinky,
    k0ala
    @k0ala
    that sounds awesome ;)