Coder Social home page Coder Social logo

sessionula's Introduction

Sessionula

This library intends to provide server-side session functionality for your web applications.

๐Ÿšง Not ready for production use yet. ๐Ÿšง

API is still highly subject to changes
Feedback welcome

{-# LANGUAGE DataKinds         #-}
{-# LANGUAGE LambdaCase        #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeApplications  #-}
{-# LANGUAGE TypeOperators     #-}

import           Control.Monad (void)
import           Data.ByteString.Lazy.Char8 (pack)
import qualified Database.Redis as Hedis
import qualified Sessionula as Session
import           Sessionula.Backend.File
import           Sessionula.Backend.Hasql
import           Sessionula.Backend.Hedis
import           Sessionula.Backend.Map
import           Network.HTTP.Types
import           Network.Wai
import           Network.Wai.Handler.Warp (run)
import           Servant
import           Sessionula.Frontend.Servant.Server

Define a user type to be used with the session authentication logic

data User = User String

Backends

Choose your backend among the provided ones:

initManager :: Session.Config -> IO Session.Manager
initManager config = 
  Session.setup config =<< mapStorage
  -- Session.setup config =<< fileStorage "/var/sessions"
  -- Session.setup config =<< hasqlStorage "postgres://postgres@localhost:5432/sessionula"
  -- Session.setup config =<< hedisStorage Hedis.defaultConnectInfo "sessions:" (Session.cfgTokenTTL config)

Frontends

You can then use this with one of the provided frontends

WAI

sessionula-wai

waiApp :: Application
waiApp request respond = do
  Session.lookup @Bool handle >>= \case
    Nothing  -> Session.set True handle
    Just _ -> void $ Session.modify not handle
  boolSession <- Session.lookup @Bool handle
  respond $ responseLBS ok200 [(hContentType, "text/plain")] $ pack $ show boolSession
  where
    handle = extractSession request

mainWithWai :: IO ()
mainWithWai = do
  manager <- initManager Session.defaultConfig
  run 8080 $ middleware manager defaultSessionCookie { setCookieSecure = False } defaultCsrfSettings waiApp

Servant

sessionula-servant
sessionula-servant-server

type API = Session User :> Get '[PlainText] String

servantApp :: Application
servantApp = serve (Proxy @API) server

server :: Server API
server = handler
  where
    handler :: Session.Handle User -> Handler String
    handler handle = do
      Session.lookup @Bool handle >>= \case
        Nothing  -> Session.set True handle
        Just _ -> void $ Session.modify not handle
      boolSession <- Session.lookup @Bool handle
      pure $ show boolSession

mainWithServant :: IO ()
mainWithServant = do
  manager <- initManager Session.defaultConfig
  run 8080 $ middleware manager defaultSessionCookie { setCookieSecure = False } defaultCsrfSettings servantApp

Todos

  • More comprehensive test suite
  • Benchmarking

See also

sessionula's People

Contributors

aveltras avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.