tdammers / sprinkles

Web Development without programming
BSD 3-Clause "New" or "Revised" License
44 stars 3 forks source link

-bake does not work #5

Open ad-si opened 6 years ago

ad-si commented 6 years ago
$ sprinkles -bake
BakeProject "./baked" (ServerConfig {scBackendCache = [], scDriver = DefaultDriver, scLogger = Nothing, scSessions = SessionConfig {sessCookieName = "ssid", sessCookieSecure = True, sessExpiration = NeverExpire, sessDriver = NoSessionDriver}, scRootDir = ""})
Baking project into ./baked
GET /123087408972309872109873012984709218371209847123 404 Not Found
text/html;charset=utf8
GET / 200 OK
application/json
./baked: openBinaryFile: inappropriate type (Is a directory)

$ tree
.
├── baked
│   └── _errors
│       └── 404.html
├── data
├── project.yml
└── templates
    ├── 404.html
    └── page.html
tdammers commented 6 years ago

Care to share your project.yml and templates?

ad-si commented 6 years ago

https://github.com/ad-si/sprinkles-test

tdammers commented 6 years ago

This is a peculiar situation: the / route produces a JSON response, and the -bake command handles these by simply dumping the JSON output into the file that corresponds to the route.

The problem here, however, is that since we're processing the root URL, that file amounts to ./baked, which is a directory, so we can't write our JSON data into that.

This is actually a more general problem though; if, for example, you were to have, say, the following routes:

  1. { pattern: '/foo', data: { ... } }
  2. { pattern: '/foo/bar', data: { ... } }

Note that both routes default to producing JSON output, so the -bake command would have to create the following two files:

  1. ./baked/foo
  2. ./baked/foo/bar

But that would require ./baked/foo to be a directory in the second case, and a file in the first case.

It works for HTML responses, because these are handled differently: if we add templates to those routes so that they render HTML, we get the following files:

  1. ./baked/foo/index.html
  2. ./baked/foo/bar/index.html

Now ./baked/foo is a directory in both cases, and everything works.

We could do the same for JSON responses, probably, but then we'd have to add a suitable additional default document to the .htaccess configuration (and thus we would be hardcoding the Apache dependency even harder than we already do).

For now, I think the best workaround is to just not have JSON routes that collide with directories; an approach that works well for anything other than / is to append .json to the route (which has the additional advantage that idiotic browsers like certain versions of IE, that have broken MIME type detection in some edge cases, will handle them correctly).