mig-server (empty) → 0.1.0.0
raw patch · 11 files changed
+387/−0 lines, 11 filesdep +aesondep +basedep +blaze-htmlsetup-changed
Dependencies added: aeson, base, blaze-html, data-default, http-api-data, http-types, mig, mig-extra, mig-swagger-ui, mig-wai, openapi3, text, transformers, warp
Files
- LICENSE +30/−0
- README.md +1/−0
- Setup.hs +3/−0
- mig-server.cabal +114/−0
- src/Mig.hs +174/−0
- src/Mig/Html.hs +9/−0
- src/Mig/Html/IO.hs +9/−0
- src/Mig/IO.hs +9/−0
- src/Mig/Json.hs +9/−0
- src/Mig/Json/IO.hs +9/−0
- src/Mig/Server/Warp.hs +20/−0
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Anton Kholomiov (c) 2023++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of Author name here nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ README.md view
@@ -0,0 +1,1 @@+# mig-server
+ Setup.hs view
@@ -0,0 +1,3 @@+import Distribution.Simple++main = defaultMain
+ mig-server.cabal view
@@ -0,0 +1,114 @@+cabal-version: 1.12++-- This file has been generated from package.yaml by hpack version 0.35.2.+--+-- see: https://github.com/sol/hpack++name: mig-server+version: 0.1.0.0+synopsis: Build lightweight and composable servers+description: With library mig we can build lightweight and composable servers.+ There are only couple of combinators to assemble servers from parts.+ It supports generic handler functions as servant does. But strives to use more+ simple model for API. It does not go to describing Server API at type level which+ leads to simpler error messages.+ .+ The main features are:+ .+ * lightweight library+ .+ * expressive DSL to compose servers+ .+ * type-safe handlers+ .+ * handlers are encoded with generic haskell functions+ .+ * built on top of WAI and warp server libraries.+ .+ Example of hello world server:+ .+ > import Mig.Json.IO+ >+ > -- | We can render the server and run it on port 8085.+ > -- It uses wai and warp.+ > main :: IO ()+ > main = runServer 8085 server+ >+ > -- | Init simple hello world server whith two routes:+ > server :: Server IO+ > server =+ > "api/v1" /.+ > [ "hello" /. hello+ > , "bye" /. bye+ > ]+ >+ > -- | Handler takes no inputs and marked as Get HTTP-request that returns Text.+ > hello :: Get (Resp Text)+ > hello = pure $ ok "Hello World"+ >+ > -- | Handle with URL-param query and json body input as Post HTTP-request that returns Text.+ > bye :: Query "name" Text -> Body Text -> Post (Resp Text)+ > bye (Query name) (Body greeting) =+ > pure $ ok $ "Bye to " <> name <> " " <> greeting+ .+ Please see:+ .+ * quick start guide at <https://anton-k.github.io/mig/>+ .+ * examples directory for more fun servers: at <https://github.com/anton-k/mig/tree/main/examples/mig-example-apps#readme>+ .+ * reference for the main functions: <https://anton-k.github.io/mig/09-reference.html>+category: Web+homepage: https://github.com/anton-k/mig#readme+bug-reports: https://github.com/anton-k/mig/issues+author: Anton Kholomiov+maintainer: anton.kholomiov@gmail.com+copyright: 2023 Anton Kholomiov+license: BSD3+license-file: LICENSE+build-type: Simple+extra-source-files:+ README.md++source-repository head+ type: git+ location: https://github.com/anton-k/mig++library+ exposed-modules:+ Mig+ Mig.Html+ Mig.Html.IO+ Mig.IO+ Mig.Json+ Mig.Json.IO+ Mig.Server.Warp+ other-modules:+ Paths_mig_server+ hs-source-dirs:+ src+ default-extensions:+ DerivingStrategies+ DuplicateRecordFields+ LambdaCase+ OverloadedRecordDot+ OverloadedStrings+ StrictData+ TypeFamilies+ ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints -Wunused-packages+ build-depends:+ aeson+ , base >=4.7 && <5+ , blaze-html+ , data-default+ , http-api-data+ , http-types+ , mig >=0.2+ , mig-extra >=0.1+ , mig-swagger-ui >=0.1+ , mig-wai >=0.1+ , openapi3+ , text+ , transformers+ , warp+ default-language: GHC2021
+ src/Mig.hs view
@@ -0,0 +1,174 @@+{-| Main module to write servers++Server is a function from response to request. Request is wrapped into monad.+Library supports IO-monad and ReaderT over IO like monads.++We can build servers from parts with flexible combinators.+Let's build hello-world server:++> main :: IO ()+> main = runServer 8080 server+>+> server :: Server IO+> server =+> "api" /. "v1" /. "hello" /. handleHello+>+> handleHello :: Get IO (Resp Text Text)+> handleHello = Send $ pure $ ok "Hello World"++We can iuse monoids to combine servers and newtype-wrappers to read various inputs.+See readme of the repo for tutorial and docs.+-}+module Mig (+ -- * types+ Server (..),+ Api (..),+ Path (..),+ PathItem (..),+ Route (..),++ -- * DSL+ Json,+ AnyMedia,+ FormUrlEncoded,+ OctetStream,+ ToServer (..),+ ToRoute (..),+ MediaType,+ ToMediaType (..),+ ToRespBody (..),+ FromReqBody (..),++ -- ** response+ IsResp (..),+ badReq,+ internalServerError,+ notImplemented,+ redirect,+ setHeader,++ -- ** methods+ Send (..),+ Get,+ Post,+ Put,+ Delete,+ Patch,+ Options,+ Head,+ Trace,+ IsMethod (..),+ GET,+ POST,+ PUT,+ DELETE,+ PATCH,+ OPTIONS,+ HEAD,+ TRACE,++ -- ** path and query++ -- | Build API for routes with queries and captures.+ -- Use monoid to combine several routes together.+ (/.),+ Capture (..),+ Query (..),+ QueryFlag (..),+ Optional (..),+ Body (..),+ Header (..),+ PathInfo (..),+ FullPathInfo (..),+ RawRequest (..),++ -- ** response++ -- | How to modify response and attach specific info to it+ Resp (..),+ RespOr (..),++ -- ** specific cases+ staticFiles,++ -- ** Plugins+ Plugin (..),+ PluginFun,+ ToPlugin (..),+ applyPlugin,+ ($:),+ prependServerAction,+ appendServerAction,+ processResponse,++ -- ** Low-level types+ Request,+ Response,+ okResponse,+ badResponse,+ ServerFun,+ -- | Run server application+ runServer,+ runServer',+ ServerConfig (..),+ FindRouteType (..),+ CacheConfig (..),+ toApplication,++ -- ** Render++ -- | Render Reader-IO monad servers to IO servers.+ HasServer (..),+ fromReader,++ -- * Convertes+ ToText (..),++ -- * utils+ badRequest,++ -- ** Server+ mapRouteInfo,+ mapServerFun,+ mapResponse,+ atPath,+ filterPath,+ getServerPaths,+ addPathLink,++ -- ** OpenApi+ toOpenApi,+ setDescription,+ describeInputs,+ setSummary,+ module X,++ -- ** Swagger+ withSwagger,+ swagger,+ DefaultInfo (..),+ addDefaultInfo,+ writeOpenApi,+ printOpenApi,+) where++-- common codecs and types++import Control.Monad.IO.Class as X+import Control.Monad.Trans.Class as X+import Data.Aeson as X (FromJSON (..), ToJSON (..))+import Data.Default as X+import Data.OpenApi as X (OpenApi, ToParamSchema (..), ToSchema (..))+import Data.Text as X (Text)+import GHC.Generics as X (Generic)+import Network.HTTP.Types.Header as X (RequestHeaders, ResponseHeaders)+import Network.HTTP.Types.Status as X+import Text.Blaze.Html as X (Html, ToMarkup (..))+import Web.FormUrlEncoded as X+import Web.HttpApiData as X++import Mig.Core+import Mig.Extra.Derive as X+import Mig.Server.Wai+import Mig.Server.Warp+import Mig.Swagger
+ src/Mig/Html.hs view
@@ -0,0 +1,9 @@+-- | Html Servers+module Mig.Html (+ module X,+) where++import Mig.Extra.Server.Html as X+import Mig.Server.Wai as X+import Mig.Server.Warp as X+import Mig.Swagger as X
+ src/Mig/Html/IO.hs view
@@ -0,0 +1,9 @@+-- | Html IO-based servers+module Mig.Html.IO (+ module X,+) where++import Mig.Extra.Server.Html.IO as X+import Mig.Server.Wai as X+import Mig.Server.Warp as X+import Mig.Swagger as X
+ src/Mig/IO.hs view
@@ -0,0 +1,9 @@+-- | IO-based servers+module Mig.IO (+ module X,+) where++import Mig.Extra.Server.IO as X+import Mig.Server.Wai as X+import Mig.Server.Warp as X+import Mig.Swagger as X
+ src/Mig/Json.hs view
@@ -0,0 +1,9 @@+-- | Json servers+module Mig.Json (+ module X,+) where++import Mig.Extra.Server.Json as X+import Mig.Server.Wai as X+import Mig.Server.Warp as X+import Mig.Swagger as X
+ src/Mig/Json/IO.hs view
@@ -0,0 +1,9 @@+-- | Json IO-based servers+module Mig.Json.IO (+ module X,+) where++import Mig.Extra.Server.Json.IO as X+import Mig.Server.Wai as X+import Mig.Server.Warp as X+import Mig.Swagger as X
+ src/Mig/Server/Warp.hs view
@@ -0,0 +1,20 @@+-- | Run mig-server with warp+module Mig.Server.Warp (+ runServer,+ runServer',+ ServerConfig (..),+ FindRouteType (..),+ CacheConfig (..),+) where++import Data.Default+import Mig.Core+import Mig.Core.Server.Cache+import Mig.Server.Wai+import Network.Wai.Handler.Warp qualified as Warp++runServer :: Int -> Server IO -> IO ()+runServer port server = Warp.run port (toApplication def server)++runServer' :: ServerConfig -> Int -> Server IO -> IO ()+runServer' config port server = Warp.run port (toApplication config server)