Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
ozzzzz committed Feb 8, 2018
1 parent c5ad75c commit 11234e9
Show file tree
Hide file tree
Showing 14 changed files with 337 additions and 0 deletions.
20 changes: 20 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
dist
dist-*
cabal-dev
*.o
*.hi
*.chi
*.chs.h
*.dyn_o
*.dyn_hi
.hpc
.hsenv
.cabal-sandbox/
cabal.sandbox.config
*.prof
*.aux
*.hp
*.eventlog
.stack-work/
cabal.project.local
.HTF/
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Changelog
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]

## [0.1.0.0] - 2018-01-22
### Added
- `GetConfig` class with method `getConfig`
- Instances for FileSystem, Mongo, Mysql, Neo4j, Postgres, Redis, Schrodinger
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# bcd-config

Tiny library with configs for:
* FileSystem;
* Mongo;
* Mysql;
* Neo4j;
* Postgres;
* Redis;
* Schrodinger.
2 changes: 2 additions & 0 deletions Setup.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import Distribution.Simple
main = defaultMain
38 changes: 38 additions & 0 deletions bcd-config.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: bcd-config
version: 0.1.0.0
description: Library to get config to different systems
homepage: https://github.com/biocad/bcd-config#readme
bug-reports: https://github.com/biocad/bcd-config/issues
author: Bogdan Neterebskii
maintainer: [email protected]
copyright: (c) 2018, Bogdan Neterebskii
stability: experimental
category: System
license: BSD3
license-file: LICENSE
build-type: Simple
cabal-version: >= 1.10

extra-source-files:
CHANGELOG.md
README.md

source-repository head
type: git
location: https://github.com/biocad/bcd-config

library
hs-source-dirs: src
exposed-modules: System.BCD.Config
, System.BCD.Config.Mongo
, System.BCD.Config.Neo4j
, System.BCD.Config.Redis
, System.BCD.Config.Mysql
, System.BCD.Config.Postgres
, System.BCD.Config.Schrodinger
, System.BCD.Config.FileSystem
build-depends: base >=4.7 && <5
, text
, unordered-containers
, aeson-picker
default-language: Haskell2010
43 changes: 43 additions & 0 deletions src/System/BCD/Config.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
module System.BCD.Config
( GetConfig (..)
, getConfigText
) where

import Control.Applicative ((<|>))
import Data.List (find, isPrefixOf)
import Data.Maybe (fromJust)
import Data.Text (Text)
import Data.Text.IO (readFile)
import Prelude hiding (readFile)
import System.Environment (getArgs)

class GetConfig a where
getConfig :: IO a

{-|
The 'getConfig' function returns 'Text' in 'IO' monad with content of JSON file with config.
More information on JSON config file convention is availeble here:
<https://api.biocad.ru/Infrastructure/%D0%9A%D0%BE%D0%BD%D0%B2%D0%B5%D0%BD%D1%86%D0%B8%D0%B8/config.json>
This function will looks for config file with such params:
@
./some-application --config-file=/path/to/config.json
./some-application -f /path/to/config.json
@
By default it is looking for @config.json@ in current directory.
-}
getConfigText :: IO Text
getConfigText = do
args <- getArgs
let path = fromJust $ findLong args <|> findShort args <|> Just "config.json"
readFile path
where
longArg = "--config-file="

findLong :: [String] -> Maybe FilePath
findLong args = drop (length longArg) <$> find (longArg `isPrefixOf`) args

findShort :: [String] -> Maybe FilePath
findShort ("-f":xs) = pure . head $ xs
findShort (_:xs) = findShort xs
findShort _ = Nothing
19 changes: 19 additions & 0 deletions src/System/BCD/Config/FileSystem.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{-# LANGUAGE OverloadedStrings #-}

module System.BCD.Config.FileSystem
( FileSystemConfig (..)
, GetConfig (..)
) where

import Data.Aeson.Picker ((|--))
import Data.HashMap.Strict (HashMap)
import System.BCD.Config (GetConfig (..), getConfigText)

newtype FileSystemConfig = FileSystemConfig (HashMap String FilePath)
deriving (Show, Read, Eq)

instance GetConfig FileSystemConfig where
getConfig = do
jsonText <- getConfigText
let fs = jsonText |-- ["deploy", "fs"]
pure $ FileSystemConfig fs
27 changes: 27 additions & 0 deletions src/System/BCD/Config/Mongo.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{-# LANGUAGE OverloadedStrings #-}

module System.BCD.Config.Mongo
( MongoConfig (..)
, GetConfig (..)
) where

import Data.Aeson.Picker ((|--))
import System.BCD.Config (GetConfig (..), getConfigText)

data MongoConfig = MongoConfig { _host :: String
, _port :: Int
, _user :: String
, _password :: String
, _descr :: String
}
deriving (Show, Read, Eq)

instance GetConfig MongoConfig where
getConfig = do
jsonText <- getConfigText
let get field = jsonText |-- ["deploy", "mongo", field]
pure $ MongoConfig (get "host")
(get "port")
(get "user")
(get "password")
(get "descr")
27 changes: 27 additions & 0 deletions src/System/BCD/Config/Mysql.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{-# LANGUAGE OverloadedStrings #-}

module System.BCD.Config.Mysql
( MysqlConfig (..)
, GetConfig (..)
) where

import Data.Aeson.Picker ((|--))
import System.BCD.Config (GetConfig (..), getConfigText)

data MysqlConfig = MysqlConfig { _host :: String
, _port :: Int
, _user :: String
, _password :: String
, _descr :: String
}
deriving (Show, Read, Eq)

instance GetConfig MysqlConfig where
getConfig = do
jsonText <- getConfigText
let get field = jsonText |-- ["deploy", "mysql", field]
pure $ MysqlConfig (get "host")
(get "port")
(get "user")
(get "password")
(get "descr")
33 changes: 33 additions & 0 deletions src/System/BCD/Config/Neo4j.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{-# LANGUAGE OverloadedStrings #-}

module System.BCD.Config.Neo4j
( Neo4jConfig (..)
, GetConfig (..)
) where

import Data.Aeson.Picker ((|--))
import System.BCD.Config (GetConfig (..), getConfigText)

data Neo4jConfig = Neo4jConfig { _host :: String
, _port :: Int
, _user :: String
, _password :: String
, _stripes :: Int
, _timeout :: Int
, _rps :: Int
, _descr :: String
}
deriving (Show, Read, Eq)

instance GetConfig Neo4jConfig where
getConfig = do
jsonText <- getConfigText
let get field = jsonText |-- ["deploy", "neo4j", field]
pure $ Neo4jConfig (get "host")
(get "port")
(get "user")
(get "password")
(get "stripes")
(get "timeout")
(get "rps")
(get "descr")
27 changes: 27 additions & 0 deletions src/System/BCD/Config/Postgres.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{-# LANGUAGE OverloadedStrings #-}

module System.BCD.Config.Postgres
( PostgresConfig (..)
, GetConfig (..)
) where

import Data.Aeson.Picker ((|--))
import System.BCD.Config (GetConfig (..), getConfigText)

data PostgresConfig = PostgresConfig { _host :: String
, _port :: Int
, _user :: String
, _password :: String
, _descr :: String
}
deriving (Show, Read, Eq)

instance GetConfig PostgresConfig where
getConfig = do
jsonText <- getConfigText
let get field = jsonText |-- ["deploy", "postgres", field]
pure $ PostgresConfig (get "host")
(get "port")
(get "user")
(get "password")
(get "descr")
28 changes: 28 additions & 0 deletions src/System/BCD/Config/Redis.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{-# LANGUAGE OverloadedStrings #-}

module System.BCD.Config.Redis
( RedisConfig (..)
, GetConfig (..)
) where

import Data.Aeson.Picker ((|--))
import System.BCD.Config (GetConfig (..), getConfigText)

data RedisConfig = RedisConfig { _host :: String
, _port :: Int
, _user :: String
, _password :: String
, _descr :: String
}
deriving (Show, Read, Eq)


instance GetConfig RedisConfig where
getConfig = do
jsonText <- getConfigText
let get field = jsonText |-- ["deploy", "redis", field]
pure $ RedisConfig (get "host")
(get "port")
(get "user")
(get "password")
(get "descr")
25 changes: 25 additions & 0 deletions src/System/BCD/Config/Schrodinger.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{-# LANGUAGE OverloadedStrings #-}

module System.BCD.Config.Schrodinger
( SchrodingerConfig (..)
, GetConfig (..)
) where

import Data.Aeson.Picker ((|--))
import System.BCD.Config (GetConfig (..), getConfigText)

data SchrodingerConfig = SchrodingerConfig { _host :: String
, _port :: Int
, _user :: String
, _password :: String
}
deriving (Show, Read, Eq)

instance GetConfig SchrodingerConfig where
getConfig = do
jsonText <- getConfigText
let get field = jsonText |-- ["deploy", "schrodinger", field]
pure $ SchrodingerConfig (get "host")
(get "port")
(get "user")
(get "password")
26 changes: 26 additions & 0 deletions stack.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# before building, don`t forget to delete the cache file ~/.stack/custom-plan/yaml/

resolver: https://lts.math.bio/bcd-lts-dev.yaml

package-indices:
- name: LTS-index
download-prefix: https://hackage.math.bio/hackage.fpcomplete.com/package/
http: https://hackage.math.bio/hackage.fpcomplete.com/00-index.tar.gz
- name: Hackage Biocad
download-prefix: http://hackage.biocad.ru/package/
http: http://hackage.biocad.ru/00-index.tar.gz

# package and necessary repos that are not included in the BCD-LTS
packages:
- '.'

# necessary extra-deps that are not included in the BCD-LTS
extra-deps:
- aeson-picker-0.1.0.1

# https://github.com/commercialhaskell/stack/issues/3520
ignore-revision-mismatch: true

flags: {}

extra-package-dbs: []

0 comments on commit 11234e9

Please sign in to comment.