Skip to content

Instantly share code, notes, and snippets.

@g4s8
Last active March 31, 2020 08:49
Show Gist options
  • Save g4s8/713666cff4626c1c689aeb18800c88d8 to your computer and use it in GitHub Desktop.
Save g4s8/713666cff4626c1c689aeb18800c88d8 to your computer and use it in GitHub Desktop.
// some kind of web app
class WebApp extends HttpWebApp {
WebApp() {
// creating new file object for composing with read and write handlers
this(new File("/tmp/1"));
}
// composition?
WebApp(File file) {
super(
// routes only PUT requests
new HttpPut(
new FileSave(file)
),
// routes only GET requests
new HttpGet(
new FileRead(file)
)
)
}
}
// file definition
interface File {
void write(byte[] data);
byte[] read();
}
// http endpoint to save content to file
class FileSave {
File file;
Response response(Request req) {
file.write(req.data());
}
}
// http endpoint to read content from file to response
class FileRead {
File file;
Response response(Request req) {
return file.read();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment