{"id":19736128,"url":"https://github.com/arnauld/swoop","last_synced_at":"2025-04-30T04:31:58.015Z","repository":{"id":2663431,"uuid":"3654649","full_name":"Arnauld/swoop","owner":"Arnauld","description":"Simple Web oop(s) -- A sinatra like Java web framework","archived":false,"fork":false,"pushed_at":"2013-12-06T23:12:23.000Z","size":548,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-05T22:31:58.406Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Arnauld.png","metadata":{"files":{"readme":"README.markdown","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-03-07T23:03:42.000Z","updated_at":"2014-12-01T13:06:35.000Z","dependencies_parsed_at":"2022-09-02T14:01:45.010Z","dependency_job_id":null,"html_url":"https://github.com/Arnauld/swoop","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Arnauld%2Fswoop","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Arnauld%2Fswoop/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Arnauld%2Fswoop/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Arnauld%2Fswoop/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Arnauld","download_url":"https://codeload.github.com/Arnauld/swoop/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251643052,"owners_count":21620414,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-12T01:05:23.327Z","updated_at":"2025-04-30T04:31:57.622Z","avatar_url":"https://github.com/Arnauld.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cimg src=\"https://github.com/Arnauld/swoop/raw/master/doc/images/swoop-logo.png\"/\u003e\n\n*Simple Web OOp!*\n\n# Quick start\n\n```java\nimport static swoop.Swoop.get;\nimport swoop.Action;\nimport swoop.Request;\nimport swoop.Response;\n\npublic class Hello {\n\n    public static void main(String[] args) {\n        get(new Action() {\n            @Override\n            public void handle(Request request, Response response) {\n                response.body(\"\u003ch1\u003eHello!\u003c/h1\u003e\");\n            }\n        });\n    }\n}\n```\n\nLaunch the main and view it:\n\n    http://0.0.0.0:4567\n\n# Features\n\n* Simple and extensible\n* Sinatra based routing ([Sinatra Route](http://www.sinatrarb.com/intro.html#Routes))\n  * Route patterns support \n  * Condition support (**in progress**)\n* Cookie support\n* WebSocket support (**almost done** still need to figure out how to write integration tests on it)\n* EventSource support (**not even in progress yet**)\n* Static files support\n* Pluggable HTTP server\n  * Default implementation based on an *event-driven* and *non-blocking* http server ([Webbit](https://github.com/webbit/webbit))\n\n\n# *SwOOp* in...\n\n## ...two minutes!\n\nDefine a filter that mesure the time spent when handling request to any `/hello/` sub routes. And define two handlers on `Get`: \n\n* one on `/time` route that simply returns the current time\n* the other one on `/hello/:name` that extracts the `name` parameter from the called uri, simulates some random job and returns a pretty nice greeting!\n* The second route match the filter, so its content will be modified with the time spent\n\n```java\nimport static swoop.Swoop.*;\nimport java.util.Random;\nimport swoop.*;\n\npublic class TwoMinutes {\n\n    public static void main(String[] args) {\n        around(new Filter(\"/hello/*\") {\n            @Override\n            public void handle(Request request, Response response, RouteChain routeChain) {\n                long t0 = System.currentTimeMillis();\n                try  {\n                    routeChain.invokeNext();\n                }\n                finally {\n                    long t1 = System.currentTimeMillis();\n                    String body = response.body();\n                    body += \"\u003cbr/\u003e\u003csmall\u003eRequest \" + request.logInfo() \n                                + \" executed in \" + (t1-t0) + \"ms\u003c/small\u003e\";\n                    response.body(body);\n                }\n            }\n        });\n        get(new Action(\"/time\") {\n            @Override\n            public void handle(Request request, Response response) {\n                response.body(\"\u003ch1\u003eCurrent time is: \" + new java.util.Date() + \"\u003c/h1\u003e\");\n            }\n        });\n        get(new Action(\"/hello/:name\") {\n            @Override\n            public void handle(Request request, Response response) {\n                try {\n                    // simulate some random processing\n                    Thread.sleep(new Random().nextInt(1000));\n                } catch (InterruptedException e) { \n                    /* ignore */\n                }\n                response.body(\"\u003ch1\u003eHello \" + request.routeParam(\"name\") + \"!\u003c/h1\u003e\");\n            }\n        });\n    }\n}\n```\n\nLaunch the main and view it:\n\n    http://localhost:4567/time\n\nCheck now at:\n\n    http://localhost:4567/hello/world\n    http://localhost:4567/hello/Everybody\n\nand see the filter that has added the processing duration\n\n\n## ...less than five minutes! (and with WebSocket)\n\nLet's see how to use and define websocket. First of all the code:\n\n```java\nimport static swoop.Swoop.*;\n\nimport java.io.IOException;\nimport java.io.InputStream;\n\nimport org.apache.commons.io.IOUtils;\n\nimport swoop.*;\n\npublic class FiveMinutes {\n\n    public static void main(String[] args) {\n        get(new Action(\"/hello\") {\n            @Override\n            public void handle(Request request, Response response) {\n                // for code simplicity page is loaded from a resource\n                // use Swoop.staticDir(dir) for static content instead\n                response.body(resourceAsString(\"FiveMinutes.html\"));\n            }\n        });\n        webSocket(new WebSocket(\"/hellowebsocket\") {\n            @Override\n            public void onMessage(WebSocketConnection connection, WebSocketMessage msg) {\n                // echo back message in upper case if it is text\n                if(msg.isText())\n                    connection.send(msg.text().toUpperCase());\n            }\n        });\n    }\n    \n    /**\n     * utility method that simply read a resource and returns its content as string\n     */\n    private static String resourceAsString(String resourcePath) {\n        InputStream input = FiveMinutes.class.getResourceAsStream(resourcePath);\n        try {\n            return IOUtils.toString(input);\n        }\n        catch(IOException ioe) {\n            throw new SwoopException(\"Failed to load resource \u003c\" + resourcePath + \"\u003e\", ioe);\n        }\n        finally {\n            IOUtils.closeQuietly(input);\n        }\n    }\n}\n```\n\nLaunch the main and view it:\n\n    http://localhost:4567/hello\n\nThe route `/hello` simply load the html page from the resource and return it as is. The `Send` button on the html page send the content of the input text through a websocket. The corresponding route is defined on the server at `/hellowebcocket` which simply returns the content of the message in upper case.\n\nHtml file `FiveMinutes.html` (copied from [Webbit](https://github.com/webbit/webbit)) is in `src/test/resources`\n\n```html\n\u003chtml\u003e\n  \u003cbody\u003e\n    \u003c!-- Send text to websocket --\u003e\n    \u003cinput id=\"userInput\" type=\"text\"\u003e\n    \u003cbutton onclick=\"ws.send(document.getElementById('userInput').value)\"\u003eSend\u003c/button\u003e\n\n    \u003c!-- Results --\u003e\n    \u003cdiv id=\"message\"\u003e\u003c/div\u003e\n\n    \u003cscript\u003e\n      function showMessage(text) {\n        document.getElementById('message').innerHTML += \"\u003cbr/\u003e\" + text;\n      }\n\n      // Have a look to www.modernizr.com as an efficient library to figure out\n      // if your browser support webSocket, and other html5 features\n      var ws = new WebSocket('ws://' + document.location.host + '/hellowebsocket');\n      showMessage('Connecting...');\n      ws.onopen = function() { showMessage('Connected!'); };\n      ws.onclose = function() { showMessage('Lost connection'); };\n      ws.onmessage = function(msg) { showMessage(msg.data); };\n    \u003c/script\u003e\n  \u003c/body\u003e\n\u003c/html\u003e\n```\n\n## ... in more than ten minutes (but non-blocking and functional!)\n\n**In progress**\n\nPort of [NodeJS and Callbacks](http://tapestryjava.blogspot.com/2012/03/nodejs-and-callbacks.html) article using SwOOp.\n\n# Contributing\n\n```bash\n    $ mvn clean test\n```\n\nIntegration/Functional\u003csup\u003e1\u003c/sup\u003e tests:\n\n```bash\n    $ mvn clean test -Pfunc\n```\n\nPerformance tests:\n\n```bash\n    $ mvn clean test -Pperf\n```\n\n\u003csup\u003e1\u003c/sup\u003e: Whereas it is really debatable, in the case of a middleware library i guess both are strongly related, by the way \"don’t worry too much about what you call a test, as long as you are clear on what it does and it does a single thing.\" \u0026mdash; [The false dichotomy of tests](http://gojko.net/2011/01/12/the-false-dichotomy-of-tests/)\n\n# Implementation notes\n\n* SwOOp route management and http dispatch method is strongly based on the Interceptor Pattern (see [Core J2EE Patterns - Intercepting Filter](http://java.sun.com/blueprints/corej2eepatterns/Patterns/InterceptingFilter.html))\n\n## Inspirations \u0026 Credits\n\n*SwOOp* was originally a fork from [Spark](https://github.com/perwendel/spark). Idea was to replace JEE Servlet dependency (originally from [Jetty](http://jetty.codehaus.org/jetty/)) by a non-blocking and event based HTTP server. After some initial refactorings, this project has emerged as a complete rewriting in order to have a more flexible and easier to test basis. There are some remaining especially *the static bootstrap initialization*.\n\nAfter investigation, the by-default underlying HTTP server is [Webbit](https://github.com/webbit/webbit) which is based on [Netty](http://www.jboss.org/netty).\n\n* Spark: [github](https://github.com/perwendel/spark) and [Website](http://www.sparkjava.com/)\n* [Webbit](https://github.com/webbit/webbit)\n* [Sinatra](https://github.com/sinatra/sinatra)\n  * [Base code](https://github.com/sinatra/sinatra/blob/master/lib/sinatra/base.rb)\n  * [Routing tests](https://github.com/sinatra/sinatra/blob/master/test/routing_test.rb)\n  * [Rake parse_query](https://github.com/rack/rack/blob/master/lib/rack/utils.rb#L65)\n  * [Rake parse_query tests](https://github.com/rack/rack/blob/master/test/spec_utils.rb#L103)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farnauld%2Fswoop","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Farnauld%2Fswoop","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farnauld%2Fswoop/lists"}