Coder Social home page Coder Social logo

ksenia-portu / hydra Goto Github PK

View Code? Open in Web Editor NEW

This project forked from graninas/hydra

0.0 1.0 0.0 508 KB

Hydra is a full-fledged framework for building web services, multithreaded and concurrent applications with SQL and KV DB support. It's currently moving to the production-ready state

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

Haskell 100.00%

hydra's Introduction

Hydra Framework

All-in-one framework for writing Haskell apps which use the following features out of the box:

  • Multithreading
  • Safe STM-powered concurrency
  • KV DB functionality (Redis & RocksDB backends supported)
  • SQL DB functionality (beam incorporated, SQLite supported, PG & MySQL possible)
  • CLI apps support
  • Logging
  • Random data generation
  • Many others

With the framework, you can create complex applications which have a good maintainability, testability, simplicity, have a good structure and are easy to read and change. The key principles of the framework:

  • Layered architecture
  • Separation of concerns (interfaces, business logic, runtime and implementation)
  • Simple and convenient eDSLs
  • Testable and maintainable code

The Hydra Project

This project demonstrates the principles of Software Design and Architecture in pure Functional Programming. Hydra has several separate frameworks based on different engines for easy comparison:

  • Final Tagless engine
  • Free monad based engine
  • Church-encoded Free monad based engine

The core idea of the two Free monadic frameworks is known as Hierarchical Free Monads.

The project is a showcase for my book Functional Design and Architecture. The approaches presented in Hydra are well-described and rationalized in the book, so you may obtain even more info about best practices and ideas of how to write a good Haskell code.

Note The Free monad based framework is the most developed by functionality. I'm working on synchronizing the functionality between all the engines.

Building dependencies

Ubuntu:

$ sudo apt-get install libpq-dev librocksdb-dev

MacOS:

brew install rocksdb postgresql

Building and running

Use stack for building all the framework and apps:

$ stack build

You can also switch the optimizations off and use several threads for building:

$ stack build --fast -j4

Running a project is also simple:

$ stack exec labyrinth

To load a subproject into GHCi, use the following command:

$ stack ghci labyrinth:exe:labyrinth

Sample applications

There are several sample applications:

  • Astro app: web server (with servant) and CLI client tool which allows to track meteors (tool for astronomers).
  • PerfTestApp: an application you can run to measure the performance of the three engines.
  • PerfTestApp2: another application you can run to measure the performance of the three engines.
  • MeteorCounter: application which demonstrates the usage of STM and multithreading using three engines.
  • Labyrinth: a game about exploring the labyrinth with a CLI interactive interface.

Code samples

Sample SQL-related code

createMeteor :: MeteorTemplate -> D.SqlConn BS.SqliteM -> L.AppL MeteorId
createMeteor mtp@(MeteorTemplate {..}) conn = do
  L.logInfo $ "Inserting meteor into SQL DB: " <> show mtp

  let time = Time.UTCTime (toEnum 1) (Time.secondsToDiffTime 0)

  doOrFail
    $ L.scenario
    $ L.runDB conn
    $ L.insertRows
    $ B.insert (SqlDB._meteors SqlDB.astroDb)
    $ B.insertExpressions
          [ SqlDB.Meteor B.default_
            (B.val_ size)
            (B.val_ mass)
            (B.val_ azimuth)
            (B.val_ altitude)
            (B.val_ time)
          ]

  let predicate meteorDB
          = (SqlDB._meteorSize meteorDB     ==. B.val_ size)
        &&. (SqlDB._meteorMass meteorDB     ==. B.val_ mass)
        &&. (SqlDB._meteorAzimuth meteorDB  ==. B.val_ azimuth)
        &&. (SqlDB._meteorAltitude meteorDB ==. B.val_ altitude)

  m <- doOrFail
    $ L.scenario
    $ L.runDB conn
    $ L.findRow
    $ B.select
    $ B.limit_ 1
    $ B.filter_ predicate
    $ B.all_ (SqlDB._meteors SqlDB.astroDb)
  pure $ SqlDB._meteorId $ fromJust m

Additional materials

Checkout the following materials to learn more about he Hierarchical Free Monads approach used in Hydra:

hydra's People

Contributors

graninas avatar yaitskov avatar laban-beta avatar qnikst avatar ksenia-portu avatar strobe avatar

Watchers

 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.