Fork me on GitHub
#yada
<
2016-02-11
>
jethroksy03:02:56

Is there any way to serve static files on the jvm classpath?

jethroksy03:02:29

(yada (io/file "public")) references the file system

dominicm07:02:16

I'm going to say yes, I'll see if I can find the snippet

dominicm07:02:53

(yada (Class/getResource "resources/"))

dominicm07:02:08

(io/resource "resources/") may also work

jethroksy07:02:11

I've tried the latter, doesn't work

jethroksy07:02:59

this is the result of (yada (io/resource "resources/"))

jethroksy07:02:19

i've only gotten (io/file ...) to work

dominicm07:02:19

No luck with the Class/getResource?

jethroksy07:02:30

nope 😕

jethroksy07:02:15

is it under new-classpath-resource?

dominicm07:02:27

That looks like it should work, yep

jethroksy07:02:36

seems like private api

jethroksy07:02:49

let me try getResource again

jethroksy07:02:35

clojure.lang.ExceptionInfo: No matching method: getResource

dominicm07:02:03

That sounds like Class/getResource is failing, no?

jethroksy07:02:06

I see it on the docs though

dominicm07:02:39

(.getResources Class "path/")

dominicm07:02:47

It's not a static method

jethroksy07:02:12

ah let me try that

jethroksy08:02:04

can't really confirm that that works

dominicm08:02:32

Because you're on boot, like me 😛

jethroksy08:02:50

I want yada to serve up stuff from inside the jar after uberjaring

jethroksy08:02:05

but at the same time serve from the classpath boot set up

jethroksy08:02:13

while doing boot dev etc

dominicm08:02:37

Looking at the source, @malcolmsparks may need to extend java.net.URL class to work as a resource (for io/resource), new-classpath-resource is what you're after, no doubt in my mind. It's designed for EXACTLY your need. It's really cool.

jethroksy08:02:52

in my repl (.getResource ...) returns nil

jethroksy08:02:04

turns out only boot.jar is in the classpath

dominicm08:02:08

Yeah, I assume for no resource found.

dominicm08:02:30

I'll jump into the edge repo and check

jethroksy08:02:37

I was using new-file-resource last time

jethroksy08:02:49

but malcolm told me the canonical way was to call (io/file ...)

jethroksy08:02:06

so I'm thinking there's a better way for these classpath stuffs

dominicm08:02:25

I mean, this works:

dominicm08:02:49

(yada (io/file (io/resource "a-file-in-resources.foo")))

dominicm08:02:21

The reason io/resource doesn't work is because of java.net.URL vs java.io.File

jethroksy08:02:39

there's a corresponding coercion for URL in yada

jethroksy08:02:08

resource works fine, but only for a single file

jethroksy08:02:28

(yada (io/resource "file-in-classpath.foo"))

jethroksy08:02:34

but it doesn't work for a directory

jethroksy08:02:47

because classpath has no concept of directories

jethroksy08:02:55

(that's what micha told me)

dominicm08:02:35

Interesting.

dominicm08:02:47

Micha is clever like that 😛

jethroksy08:02:19

micha [11:35 AM] i guess what you need is a task that makes a manifest of all the files in public [11:35] so you can add them one by one [11:35] as yada endpoints

dominicm08:02:38

I'd just use new-classpath-resource 😛

jethroksy08:02:42

so he suggested a file manifest

jethroksy08:02:01

let me try that

dominicm08:02:04

Malcolm is using it for swagger & such at this point.

dominicm08:02:12

It'll possibly change in the future.

jethroksy08:02:36

new-classpath-resource only allows OPTIONS

jethroksy08:02:45

I guess it's not meant for that

jethroksy08:02:23

I can make do with io/file, just that it would be nice to be able to serve from classpath (with a single jar)

dominicm08:02:51

Yeah. Malcolm will probably tell us soon that it's done and we just haven't found it. 😛

malcolmsparks08:02:57

For serving reaources, an example is at the of this file https://github.com/juxt/yada/blob/master/src/yada/swagger.clj

malcolmsparks08:02:51

(new-classpath-resource "/foo)

malcolmsparks08:02:27

Sorry, use 'foo' not '/foo'

malcolmsparks08:02:46

Really? I'll look into that

malcolmsparks08:02:15

Ah, did you add a additional path?

malcolmsparks08:02:04

You have to add an additional path-info onto the URI, like with dire tory serving

malcolmsparks08:02:19

Sorry, hands about to freeze!

malcolmsparks08:02:30

Hopefully this bus will arrive soon

malcolmsparks08:02:46

So if I 'mounted' my classpath resource at /foo/ I would access a resource prefix+bar with /foo/bar

jethroksy08:02:58

let me try again

malcolmsparks08:02:13

Careful with those slashes!

jethroksy08:02:31

usually I just try every possibility and pray one works hahaha

malcolmsparks08:02:36

Check the Subresources chapter

jethroksy08:02:40

same 405 error

jethroksy09:02:00

the new-classpath-resource method looks correct though

jethroksy09:02:11

I'll take some time to think things through

malcolmsparks10:02:20

I'm in the office now, do send me some code and I'll help you figure it out. Are you using bidi btw?

malcolmsparks11:02:14

In the meantime I'll check that Class/getResource thing - I'm sure it will work, although I only wrote that piece of documentation yesterday and didn't expect anyone to read it yet!

dominicm11:02:08

@malcolmsparks: Not a static method, and it returns slightly differently to (io/resource) based on my tests. 😛 We're all watching your commits carefully, maybe you'll have to start branching out.

fahey15:02:40

Hi. I'm getting a validation error when providing handlers in my http responses. :error {:methods {:post {:responses {404 {:response disallowed-key}}}}} (I've been having lots of fun with yada over the past couple of weeks though, of course I only ever say anything unless I think I have a bug... simple_smile)

malcolmsparks15:02:17

Ah, responses is just a resource-level thing right now. So you put it at the resource-level, alongside :methods

malcolmsparks15:02:00

Status responses are quite new and I'm sure soon you'll be able to specify them at the method-level, for consistency if nothing else.

malcolmsparks15:02:32

@fahey glad you've been having fun with yada, I hope that's both good fun and the usual frustrating 'fun' scrambling up any learning curve

malcolmsparks15:02:29

The validation is to protect resource models that yada currently doesn't support - it can be painful getting past the validation step but it's worth having otherwise the code in yada would get horribly complex

fahey15:02:55

Oh, thanks @malcolmsparks ! I would have stared at that for days before figuring it out, if then.

fahey15:02:30

I really like the validation, and it serves as documentation to some extent.

fahey15:02:56

Yada seems fun to me because I can tell that the whole thing is coherent and well-thought out, so it is more like exploring something than just learning a tool.

malcolmsparks15:02:32

@fahey good to hear. It's fun for me too, it's been a nice way of learning http simple_smile

malcolmsparks15:02:58

The validation is data, so I think perhaps it's time to publish it in some doc form

fahey15:02:45

@malcolmsparks: That would be useful, especially if there were hints alongside about the coercions/shortcuts.

malcolmsparks16:02:42

Yes, it needs its own chapter really, there's lots of shortforms now.

stijn16:02:03

@malcolmsparks: I finally have some time to migrate from 1.0 (0.9-my-own-dev-branch) to 1.1. starting right now simple_smile

stijn16:02:15

I'll report back on the video upload

stijn16:02:35

the plan is to stream parts through to AWS S3

malcolmsparks16:02:31

@stijn Great news - that's one area I want feedback on before release 1.1 final. There is a protocol extension for allowing S3 uploads, but I think we'll need to work together one this

stijn16:02:07

okay, let me first get all the other resources working, it's quite a change simple_smile

malcolmsparks16:02:17

this is application/octetstream right? Not multipart/*

stijn16:02:53

well, that was initially the case, but I think we'll be moving to multipart/* since we are adding a web client too

malcolmsparks16:02:17

ok, just checking, both are supported (although I need to check that application/octetstream code - I'll do that tonight)

malcolmsparks16:02:37

yes, it's a big change from 1.0->1.1. But I think you'll find it's an improvement once you get used to it

stijn16:02:49

S3 uploads are multipart/* too

malcolmsparks16:02:06

yes, the S3 extension would cope with any upload

malcolmsparks16:02:16

have an S3 extension built-in would be very cool

malcolmsparks16:02:39

the best resource is still examples/phonebook

stijn16:02:50

ok, i'll look into that

malcolmsparks16:02:02

I'm still trying to document everything but it's hard work and slow going

stijn16:02:05

😄

malcolmsparks16:02:11

The features are pretty much done, but the docs need some work 😞

malcolmsparks16:02:50

@stijn just ping me in this slack channel if you need any help or confused by anything

malcolmsparks16:02:00

I tend to check-in at least twice a day

stijn16:02:58

ok, will do