GithubHelp home page GithubHelp logo

o3 / n2o Goto Github PK

View Code? Open in Web Editor NEW
47.0 8.0 5.0 295 KB

⭕️ N2O: Haskell Application Server

Home Page: https://n2o.o3.click

License: BSD 3-Clause "New" or "Revised" License

Haskell 86.85% HTML 13.15%
haskell n2o

n2o's Introduction

N2O for Haskell

Build Status

Features

  • Endpoints: poor man's WebSocket and static HTTP server
  • Formatters: BERT
  • Protocols: NITRO
  • High Performance Protocol Relay
  • Smallest possible codebase — 500 LOC

N2O defines a way we scale protocols, database schema, applications and services across companies, formatters, views and presentation layers. At the core N2O folds a list of protocols and their handlers providing a minimal type-level specification for general purpose application protocol tract.

As example this Haskell version of N2O is shipped with Nitro protocol implementation, that listens the tract and push prerendered JavaScript events back to the channel. This smart and simple reactive way of client-server interaction first was used by Rusty Klophaus in his Nitrogen web framework, that was pushed forward since then in N2O by Andy Melnikov and Marat Khafizov.

Setup

stack build
stack exec n2o-sample
open http://localhost:3000/samples/static/index.html

Nitro Protocol Demo

{-# LANGUAGE FlexibleContexts, OverloadedStrings, DeriveGeneric, DeriveAnyClass #-}
module Main (main) where

import Network.N2O
import Network.N2O.Web hiding (Event)
import GHC.Generics (Generic)
import Data.Serialize (Serialize)

data Example = Greet deriving (Show, Eq, Generic, Serialize)

main = runServer "localhost" 3000 cx

cx :: Cx Example
cx = mkCx{ cxMiddleware=[router]
         , cxProtos = [nitroProto]
         }

router cx@Context{cxReq=Req{reqPath=path}} =
  let handle = case path of
                  "/ws/samples/static/index.html" -> index
                  "/ws/samples/static/about.html" -> about
                  _ -> index
  in cx{cxHandler=mkHandler handle}

index Init = do
  updateText "system" "What is your name?"
  wire button{id_="send", postback=Just Greet, source=["name"]}
index (Message Greet) = do
  Just name <- get "name" -- wf:q/1
  updateText "system" ("Hello, " <> jsEscape name <> "!")
index ev = liftIO $ putStrLn ("Unknown event" <> show ev)
about Init = updateText "app" "This is the N2O Hello World App"
about ev = liftIO $ putStrLn ("Unknown event " <> show ev)

Credits

  • Andy Melnikov
  • Marat Khafizov
  • Maxim Sokhatsky

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.