{"id":18003066,"url":"https://github.com/zmactep/hasbolt-sample-app","last_synced_at":"2025-06-17T04:06:24.732Z","repository":{"id":143004023,"uuid":"75623309","full_name":"zmactep/hasbolt-sample-app","owner":"zmactep","description":"Sample Movie Database application with Haskell backend","archived":false,"fork":false,"pushed_at":"2017-11-20T22:37:08.000Z","size":15,"stargazers_count":7,"open_issues_count":2,"forks_count":5,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-04-07T05:34:20.742Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Haskell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zmactep.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-12-05T12:37:41.000Z","updated_at":"2021-06-15T11:41:48.000Z","dependencies_parsed_at":"2023-03-17T16:45:27.347Z","dependency_job_id":null,"html_url":"https://github.com/zmactep/hasbolt-sample-app","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/zmactep/hasbolt-sample-app","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zmactep%2Fhasbolt-sample-app","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zmactep%2Fhasbolt-sample-app/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zmactep%2Fhasbolt-sample-app/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zmactep%2Fhasbolt-sample-app/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zmactep","download_url":"https://codeload.github.com/zmactep/hasbolt-sample-app/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zmactep%2Fhasbolt-sample-app/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260288471,"owners_count":22986667,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-29T23:25:20.735Z","updated_at":"2025-06-17T04:06:24.704Z","avatar_url":"https://github.com/zmactep.png","language":"Haskell","funding_links":[],"categories":[],"sub_categories":[],"readme":"# hasbolt-sample-app\nSample Movie Database application with Haskell backend\n\nFeel free to copy this example and use [hasbolt](https://github.com/zmactep/hasbolt) as you with :)\n\nBuild\n-----\n\nTo build the project just clone it and run build command by stack:\n```\ngit clone https://github.com/zmactep/hasbolt-sample-app.git\ncd hasbolt-sample-app\nstack build\n```\n\nUsage\n-----\n\n```\nPORT=8080 stack exec hasbolt-sample-app-exe\n```\n\nCloud deployment\n----------------\n\nTo deploy on Heroku just follow these steps:\n```\nexport app=neo4j-movies-haskell-`whoami`\nheroku apps:create $app\n\n# Add neo4j addon and make it available from application\nheroku addons:add graphenedb:chalk --app $app\n\n# Set Haskell Stack buildpack\nheroku buildpacks:set https://github.com/mfine/heroku-buildpack-stack\n\n# deploy to heroku\ngit push heroku master\n\n# open application\nheroku open --app $app\n\n# open addon admin page\nheroku addons:open graphenedb\n```\n\nIn the Graphenedb-UI use “Launch Neo4j Admin UI”. In the Neo4j-Browser import the `:play movies` dataset.\n\nUnder the hood\n--------------\n\nHere I use static jQuery html from [movies-python-bolt](https://github.com/neo4j-examples/movies-python-bolt) and the same API.\n\nHttp backend uses [Scotty](https://github.com/scotty-web/scotty) web framework. To store the internal state with connection pool I use a ReaderT monad transformer.\n\n### Server state\n\nFirst, we need to create a connection pool to out Neo4j database ([source](https://github.com/zmactep/hasbolt-sample-app/blob/master/src/Data.hs#L21)):\n```haskell\n-- |A pool of connections to Neo4j server\ndata ServerState = ServerState { pool :: Pool Pipe }\n\n-- |Reader monad over IO to store connection pool\ntype WebM = ReaderT ServerState IO\n```\n\nTo create new connection pool we use `connect :: BoltCfg -\u003e IO Pipe` and `close :: Pipe -\u003e IO ()` functions from **hasbolt** to tell resource-pool how to create a new connection and how to close one ([source](https://github.com/zmactep/hasbolt-sample-app/blob/master/src/Data.hs#L68)):\n```haskell\n-- |Create pool of connections (4 stripes, 500 ms timeout, 1 resource per stripe)\nconstructState :: BoltCfg -\u003e IO ServerState\nconstructState bcfg = do pool \u003c- createPool (connect bcfg) close 4 500 1\n                         return (ServerState pool)\n```\n\n### Simple server\n\nAfter we created a representation of our server state, we can create a simple server on given port ([source](https://github.com/zmactep/hasbolt-sample-app/blob/master/src/SimpleServer.hs#L18)):\n```haskell\nrunServer :: Port -\u003e BoltCfg -\u003e IO ()\nrunServer port config = do state \u003c- constructState config\n                           scottyT port (`runReaderT` state) $ do\n                             middleware logStdoutDev\n                             get  \"/\"             mainR\n                             get  \"/graph\"        graphR\n                             get  \"/search\"       searchR\n                             get  \"/movie/:title\" movieR\n```\n\nHere we construct a new state by `constrictState :: BoltCfg -\u003e ServerState` function from hardcoded default configuration and set routes. Let's implement these routes.\n\nFirst of all we need to respond with static [index.html](https://github.com/zmactep/hasbolt-sample-app/blob/master/index.html) on \"/\" route ([source](https://github.com/zmactep/hasbolt-sample-app/blob/master/src/Routes.hs#L20)):\n```haskell\n-- |Main page route\nmainR :: ActionT Text WebM ()\nmainR = file \"index.html\"\n```\n\nOn a search request we get a text \"q\" parameter and perform a movie search, then respond it as json ([source](https://github.com/zmactep/hasbolt-sample-app/blob/master/src/Routes.hs#L30)):\n```haskell\n-- |Search response route\nsearchR :: ActionT Text WebM ()\nsearchR = do q \u003c- param \"q\" :: ActionT Text WebM Text\n             results \u003c- runQ $ querySearch (toStrict q)\n             json results\n```\n\nA movie select is quick and unsafe way to get a json with movie info by it's exact title ([source](https://github.com/zmactep/hasbolt-sample-app/blob/master/src/Routes.hs#L36)):\n```haskell\n-- |Movie response route\nmovieR :: ActionT Text WebM ()\nmovieR = do t \u003c- param \"title\" :: ActionT Text WebM Text\n            movieInfo \u003c- runQ $ queryMovie (toStrict t)\n            json movieInfo\n```\n\nAt last we have to return a graph of movies and actors ([source]()):\n```haskell\n-- |Graph response route\ngraphR :: ActionT Text WebM ()\ngraphR = do limit \u003c- param \"limit\" `rescue` const (return 100)\n            graph \u003c- runQ $ queryGraph limit\n            json graph\n```\n\n### Frontend \u003c-\u003e backend JSON protocol serialization\n\n**TODO:** Uninteresting JSON data serialization ([source](https://github.com/zmactep/hasbolt-sample-app/blob/master/src/Type.hs)).\n\n### Database queries by hasbolt\n\nTo run queries we need to get a connection pool, get one `Pipe` and do a request by this pipe. All of this in the `runQ` function ([source](https://github.com/zmactep/hasbolt-sample-app/blob/master/src/Routes.hs#L15)):\n```haskell\n-- |Run BOLT action in scotty 'ActionT' monad transformer\nrunQ :: BoltActionT IO a -\u003e ActionT Text WebM a\nrunQ act = do ss \u003c- lift ask\n              liftIO $ withResource (pool ss) (`run` act)\n```\n\nSee comments in the code to understand the query functions.\n\nSearch ([source](https://github.com/zmactep/hasbolt-sample-app/blob/master/src/Data.hs#L27)):\n```haskell\n-- |Search movie by title pattern\nquerySearch :: Text -\u003e BoltActionT IO [Movie]\nquerySearch q = do records \u003c- queryP cypher params           -- get record list by cypher query and params\n                   nodes \u003c- traverse (`at` \"movie\") records  -- from each record get only \"movie\" field\n                   traverse toMovie nodes                    -- serialize movies to jsonable data\n  where cypher = \"MATCH (movie:Movie) WHERE movie.title =~ {title} RETURN movie\"\n        params = fromList [(\"title\", T $ \"(?i).*\" \u003c\u003e q \u003c\u003e \".*\")]\n```\n\n\nMovie ([source](https://github.com/zmactep/hasbolt-sample-app/blob/master/src/Data.hs#L35)):\n```haskell\n-- |Returns movie by title\nqueryMovie :: Text -\u003e BoltActionT IO MovieInfo\nqueryMovie title = do result \u003c- head \u003c$\u003e queryP cypher params -- get first record from received record list by cypher query and params\n                      T title \u003c- result `at` \"title\"          -- get movie title as text (you also can use exact function here)\n                      L members \u003c- result `at` \"cast\"         -- get movie cast as list\n                      cast \u003c- traverse toCast members         -- serialize cast to jsonable data\n                      return $ MovieInfo title cast           -- serialize all to json object\n  where cypher = \"MATCH (movie:Movie {title:{title}}) \" \u003c\u003e\n                 \"OPTIONAL MATCH (movie)\u003c-[r]-(person:Person) \" \u003c\u003e\n                 \"RETURN movie.title as title,\" \u003c\u003e\n                 \"collect([person.name, \" \u003c\u003e\n                 \"         head(split(lower(type(r)), '_')), r.roles]) as cast \" \u003c\u003e\n                 \"LIMIT 1\"\n        params = fromList [(\"title\", T title)]\n```\n\nGraph ([source](https://github.com/zmactep/hasbolt-sample-app/blob/master/src/Data.hs#L50)):\n```haskell\n-- |Returns movies with all it's actors\nqueryGraph :: Int -\u003e BoltActionT IO MGraph\nqueryGraph limit = do records \u003c- queryP cypher params                        -- get first record from received record\n                      nodeTuples \u003c- traverse toNodes records                 -- convert records to list of tuples (movie, [actors])\n                      let movies = fst \u003c$\u003e nodeTuples                        -- get list of movies\n                      let actors = nub $ concatMap snd nodeTuples            -- get list of actors\n                      let actorIdx = fromJust . (`lookup` zip actors [0..])  -- some magic here to obtain relations\n                      let modifyTpl (m, as) = (m, actorIdx \u003c$\u003e as)\n                      let indexMap = fromList $ modifyTpl \u003c$\u003e nodeTuples\n                      let mkTuples (m, t) = (`MRel` t) \u003c$\u003e indexMap ! m\n                      let relations = concatMap mkTuples $ zip movies [length actors..]\n                      return $ MGraph (actors \u003c\u003e movies) relations           -- serialize all to json object\n  where cypher = \"MATCH (m:Movie)\u003c-[:ACTED_IN]-(a:Person) \" \u003c\u003e\n                 \"RETURN m.title as movie, collect(a.name) as cast \" \u003c\u003e\n                 \"LIMIT {limit}\"\n        params = fromList [(\"limit\", I limit)]\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzmactep%2Fhasbolt-sample-app","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzmactep%2Fhasbolt-sample-app","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzmactep%2Fhasbolt-sample-app/lists"}