GithubHelp home page GithubHelp logo

rhc's Introduction

Remote Haskell Call

RHC is a Haskell implementation of JSON-RPC 2.0 Protocol.

Goal

At the moment, the library is in the active development stage. The package was designed and developed to follow the specific goals of the author and help him to learn about Haskell's features. So, this package isn't ready for production use yet, and maybe will be ready soon

Features

  • Convenient server interface for defining remote procedures in namespaces
  • Automatic generation of remote procedures from the server on the client side
  • Minimal boilerplate that reached by using Template Haskell

Usage guide

examples folder contains client and server usage examples.

For better convenience, here are those examples:

Client-Side code:

{-# LANGUAGE TemplateHaskell #-}

module Main where

import Rhc


load (CliConf 3000 "localhost" Http)
     ["example", "coffee"]

config :: CliConf
config = CliConf
    { cPort = 3000
    , cHost = "localhost"
    , cProtocol = Http
    }

coffeeNotification :: IO ()
coffeeNotification = remoteNotifier
    config (makeCoffee "Hello!")

main :: IO ()
main = remoteRunner
    config
    rand
    (makeCoffee "hello from client!") >>= print

Server-Side code:

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}

module Main where

import Control.Monad.IO.Class
import Control.Monad.Catch (MonadThrow(throwM))
import Data.Aeson
import Data.Aeson.Types
import Network.Wai.Handler.Warp (Port)
import Language.Haskell.TH(runQ)

import Rhc

doSome :: RemoteAction [Int] Int
doSome (x:x2:xs) = liftIO $ print x >> pure (x + x2)
doSome xs = liftIO $ print xs >> pure 0

doErr :: RemoteAction [Int] [Int]
doErr (x:x2:xs) = if x > x2
  then throwM (ErrorObject
                (ErrorServCause 
                  (ServerError (-32000)))
                "test message")
  else pure [x + x2]

makeCoffee :: RemoteAction String [String]
makeCoffee x = pure ["some", "pre-defined", "words"]

countCoffee :: RemoteAction String Int
countCoffee _ = pure 55

generate $ domain "example"
                ( method "doSome" 'doSome
               <> method "doErr" 'doErr )
             <>
           domain "coffee"
                ( method "makeCoffee" 'makeCoffee
               <> method "countCoffee" 'countCoffee )

main :: IO ()
main = serv 3000

Development

To build the project and run the tests locally, you can use either cabal or stack.

Cabal

Build the project:

cabal build rhc

To build examples, run first server command:

cabal build simple-server

And then run client command:

cabal build simple-client

The cause for this is that client code queries methods from the server in compile-time to assure in type correctness of methods.

Run all tests:

cabal test --enable-tests --test-show-details=direct

Stack

Recommended to build simple-server first with:

stack ghci

And run the main function to start the server. It will provide the working server for client example compilation and the whole project:

stack build --test --no-run-tests

Run all tests:

stack test

rhc's People

Contributors

dponya avatar

Stargazers

randomtron avatar

Watchers

 avatar

rhc's Issues

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.