https://github.com/onionpancakes/serval
Servlet oriented web framework for Clojure.
https://github.com/onionpancakes/serval
clojure http jetty servlet webserver
Last synced: 10 months ago
JSON representation
Servlet oriented web framework for Clojure.
- Host: GitHub
- URL: https://github.com/onionpancakes/serval
- Owner: onionpancakes
- License: mit
- Created: 2021-08-19T08:34:40.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-03-01T19:23:45.000Z (about 2 years ago)
- Last Synced: 2024-03-01T20:33:18.688Z (about 2 years ago)
- Topics: clojure, http, jetty, servlet, webserver
- Language: Clojure
- Homepage:
- Size: 526 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Serval
Servlet oriented web framework for Clojure.
# Status
[](https://github.com/onionpancakes/serval/actions/workflows/run_tests.yml)
Currently for my personal use. Future breaking changes possible.
# Deps
For **all** modules, add the following.
```clojure
{:deps
{dev.onionpancakes/serval
{:git/url "https://github.com/onionpancakes/serval"
:git/sha ""}}}
```
For specific modules, use `:deps/root`.
```clojure
{:deps
{dev.onionpancakes/serval-core
{:git/url "https://github.com/onionpancakes/serval"
:git/sha ""
:deps/root "modules/serval-core"}
dev.onionpancakes/serval-jetty
{:git/url "https://github.com/onionpancakes/serval"
:git/sha ""
:deps/root "modules/serval-jetty"}}}
```
# Example
Require the namespaces.
```clojure
(require '[dev.onionpancakes.serval.core :as srv])
(require '[dev.onionpancakes.serval.jetty :as srv.jetty])
```
Write servlet service functions.
```clojure
(defn hello-world
[_ _ response]
(doto response
(srv/set-http :status 200
:content-type "text/plain"
:character-encoding "utf-8")
(srv/write-body "Hello world!")))
(defn not-found
[_ _ response]
(doto response
(srv/set-http :status 404
:content-type "text/plain"
:character-encoding "utf-8")
(srv/write-body "Not found.")))
(defn error
[_ _ response]
(doto response
(srv/set-http :status 500
:content-type "text/plain"
:character-encoding "utf-8")
(srv/write-body "Error happened.")))
```
In an app map, add the service fns to :routes and :error-pages.
```clojure
;; Note: :routes uses servlet url-pattern "routing"
;; e.g. "" - match root
;; "/*" - match wildcard
;; "/foo" - match prefix
;; "/foo/*" - match prefix with wildcard
;; "*.css" - match extension
;; "/" - match default
(def app
{:routes [["" hello-world]
["/not-found" not-found]
["/error" error]]
:error-pages {404 "/not-found"
Throwable "/error"}})
```
Add the app to a server.
```clojure
(defonce server
(srv.jetty/server))
(defn -main []
(srv.jetty/configure server {:connectors [{:port 3000}]
:handler app})
(srv.jetty/start server))
```
See example directory for complete solution.
# License
Released under the MIT license.