okapi/README.md

43 lines
1.6 KiB
Markdown
Raw Normal View History

2022-03-31 05:35:32 +03:00
# Okapi
2022-09-20 14:47:55 +03:00
A micro web framework based on monadic parsing. Official documentation [here](https://www.okapi.wiki/).
2022-03-31 05:35:32 +03:00
## Introduction
2022-09-20 14:47:40 +03:00
**Okapi** is a micro web framework for Haskell.
2022-03-31 05:35:32 +03:00
In contrast to other web frameworks in the Haskell ecosystem, Okapi is primarily concerned with being easy to understand and use, instead of extreme type safety.
2022-07-27 06:42:57 +03:00
2022-03-31 05:35:32 +03:00
Here's an example of a simple web server:
```haskell
{-# LANGUAGE OverloadedStrings #-}
2022-09-20 14:43:05 +03:00
{-# LANGUAGE TypeApplications #-}
2022-03-31 05:35:32 +03:00
import Data.Text
import Okapi
main :: IO ()
2022-09-20 14:43:05 +03:00
main = run greet
2022-03-31 05:35:32 +03:00
greet = do
2022-09-20 14:44:16 +03:00
methodGET
2022-09-20 14:43:05 +03:00
pathParam @Text `is` "greet"
2022-09-20 14:44:59 +03:00
name <- pathParam
2022-09-20 14:43:48 +03:00
pathEnd
2022-09-20 14:43:05 +03:00
return $ setPlaintext ("Hello " <> name <> "! I'm Okapi.") $ ok
2022-03-31 05:35:32 +03:00
```
Running this code will start a server on [localhost:3000](http://localhost:3000.org).
2022-07-27 06:43:53 +03:00
If you go to [http://localhost:3000/greeting/Bob]() the server will respond with
2022-07-27 06:44:45 +03:00
2022-07-27 06:43:53 +03:00
```Hello Bob! I'm Okapi.```
2022-07-27 06:44:45 +03:00
2022-07-27 06:43:53 +03:00
in plain text format.
2022-03-31 05:35:32 +03:00
2022-07-27 06:42:57 +03:00
Okapi provides [monadic parsers](https://www.cs.nott.ac.uk/~pszgmh/monparsing.pdf) for extracting data from HTTP requests.
Since they are monads, parsers can be used with all `Applicative`, `Alternative`, and `Monad` typeclass methods, plus other Haskell idioms like [parser combinators](https://hackage.haskell.org/package/parser-combinators).
2022-09-20 14:48:42 +03:00
Because of this, parsers are very modular and can be easily composed with one another to fit your specific needs.
2022-03-31 05:35:32 +03:00
2022-07-27 06:42:57 +03:00
With Okapi, and the rest of the Haskell ecosystem, you can create anything from simple website servers to complex APIs for web apps.
2022-03-31 05:35:32 +03:00
All you need to get started is basic knowledge about the structure of HTTP requests and an idea of how monadic parsing works.