diff --git a/.gitignore b/.gitignore index 274184d..80da994 100644 --- a/.gitignore +++ b/.gitignore @@ -1,10 +1,17 @@ # system .DS_Store +.vscode # needed for stack .stack-work/ *~ -haskell-starter-kit.cabal + +# cabal specific +dist +dist-* + +# nix specific +result* # Auth key keys/* diff --git a/default.nix b/default.nix new file mode 100644 index 0000000..3ed5ee9 --- /dev/null +++ b/default.nix @@ -0,0 +1,13 @@ +let + sources = import ./nix/sources.nix {}; + haskellNix = import sources.haskellNix {}; + pkgs = import + haskellNix.sources.nixpkgs-2105 + haskellNix.nixpkgsArgs; +in pkgs.haskell-nix.project { + src = pkgs.haskell-nix.haskellLib.cleanGit { + name = "haskell-starter-kit"; + src = ./.; + }; + compiler-nix-name = "ghc8107"; +} \ No newline at end of file diff --git a/haskell-starter-kit.cabal b/haskell-starter-kit.cabal new file mode 100644 index 0000000..c379fc2 --- /dev/null +++ b/haskell-starter-kit.cabal @@ -0,0 +1,244 @@ +cabal-version: 2.2 +name: haskell-starter-kit +version: 0.1.0.0 +description: Please see the README on GitHub at +homepage: https://github.com/fullstack-development/haskell-starter-kit#readme +bug-reports: https://github.com/fullstack-development/haskell-starter-kit/issues +author: Author name here +maintainer: example@example.com +copyright: 2021 Author name here +license: BSD-3-Clause +license-file: LICENSE +build-type: Simple +extra-source-files: + README.md + ChangeLog.md + +source-repository head + type: git + location: https://github.com/fullstack-development/haskell-starter-kit + +common lang + default-language: Haskell2010 + default-extensions: + RecordWildCards + NamedFieldPuns + OverloadedStrings + FlexibleContexts + FlexibleInstances + StandaloneDeriving + ScopedTypeVariables + ExplicitForAll + TypeOperators + DeriveGeneric + MonoLocalBinds + LambdaCase + DeriveFoldable + DeriveFunctor + DeriveGeneric + DeriveLift + DeriveTraversable + ExplicitForAll + GeneralizedNewtypeDeriving + + ghc-options: + -Wall -Wnoncanonical-monad-instances -Wincomplete-uni-patterns + -Wincomplete-record-updates -Wredundant-constraints -Widentities + -fobject-code -fno-ignore-interface-pragmas + -fno-omit-interface-pragmas + +library + import: lang + exposed-modules: + AppName.API + AppName.API.PhoneVerification + AppName.API.User + AppName.AppHandle + AppName.Auth + AppName.Auth.Combinators + AppName.Auth.Commands + AppName.Auth.ServantInstances + AppName.Auth.User + AppName.Config + AppName.Domain.PhoneVerification + AppName.Gateways.CryptoRandomGen + AppName.Gateways.Database + AppName.Gateways.Database.Connection + AppName.Gateways.Database.Setup + AppName.Gateways.Database.Tables.User + AppName.Gateways.Endpoints.FakeLogin + AppName.Gateways.Endpoints.GetUsers + AppName.Gateways.Endpoints.PhoneVerification + AppName.Gateways.Endpoints.SaveUsers + AppName.Gateways.PhoneVerificationStorage + AppName.Server + Ext.Data.Aeson + Ext.Data.Text + Ext.Data.Time + Ext.HTTP.Error + Ext.HTTP.Response + Ext.Logger + Ext.Logger.Colog + Ext.Logger.Config + Lib + other-modules: + Paths_haskell_starter_kit + hs-source-dirs: + src + build-depends: + aeson + , base >=4.7 && <5 + , basement + , bytestring + , co-log + , containers + , configurator + , cryptonite + , dhall + , directory + , esqueleto + , http-client + , http-client-tls + , jose + , memory + , monad-logger + , mtl + , persistent + , persistent-postgresql + , persistent-template + , postgresql-simple + , random + , resource-pool + , resourcet + , safe-exceptions + , servant-auth + , servant-auth-server + , servant-client + , servant-foreign + , servant-options + , servant-server + , stm + , text + , time + , typerep-map + , unliftio + , unliftio-core + , wai + , wai-cors + , wai-logger + , warp + default-language: Haskell2010 + +executable haskell-starter-kit-exe + main-is: Main.hs + import: lang + hs-source-dirs: + app + build-depends: + aeson + , base >=4.7 && <5 + , basement + , bytestring + , co-log + , containers + , cryptonite + , dhall + , directory + , esqueleto + , haskell-starter-kit + , http-client + , http-client-tls + , jose + , memory + , monad-logger + , mtl + , persistent + , persistent-postgresql + , persistent-template + , postgresql-simple + , random + , resource-pool + , resourcet + , safe-exceptions + , servant-auth + , servant-auth-server + , servant-client + , servant-foreign + , servant-options + , servant-server + , stm + , text + , time + , typerep-map + , unliftio + , unliftio-core + , wai + , wai-cors + , wai-logger + , warp + if false + other-modules: + Paths_haskell_starter_kit + default-language: Haskell2010 + +test-suite haskell-starter-kit-test + type: exitcode-stdio-1.0 + import: lang + main-is: Spec.hs + other-modules: + PhoneVerification + hs-source-dirs: + test + build-depends: + aeson + , base >=4.7 && <5 + , basement + , bytestring + , co-log + , containers + , cryptonite + , dhall + , directory + , esqueleto + , haskell-starter-kit + , hspec + , hspec-core + , hspec-wai + , http-client + , http-client-tls + , http-types + , jose + , memory + , monad-logger + , mtl + , persistent + , persistent-postgresql + , persistent-template + , postgresql-simple + , random + , resource-pool + , resourcet + , safe-exceptions + , servant-auth + , servant-auth-server + , servant-client + , servant-foreign + , servant-options + , servant-server + , stm + , tasty + , tasty-hspec + , text + , time + , typerep-map + , unliftio + , unliftio-core + , wai + , wai-cors + , wai-extra + , wai-logger + , warp + if false + other-modules: + Paths_haskell_starter_kit + default-language: Haskell2010 diff --git a/nix/sources.json b/nix/sources.json new file mode 100644 index 0000000..132349a --- /dev/null +++ b/nix/sources.json @@ -0,0 +1,38 @@ +{ + "haskellNix": { + "branch": "master", + "description": "Alternative Haskell Infrastructure for Nixpkgs", + "homepage": "https://input-output-hk.github.io/haskell.nix", + "owner": "input-output-hk", + "repo": "haskell.nix", + "rev": "8cac12db77edfde52377a6a29813e007f11d38e3", + "sha256": "18zmis0n2q578gylyl8383fzackwppng5xwdnrsmr49kf6kby0vx", + "type": "tarball", + "url": "https://github.com/input-output-hk/haskell.nix/archive/8cac12db77edfde52377a6a29813e007f11d38e3.tar.gz", + "url_template": "https://github.com///archive/.tar.gz" + }, + "niv": { + "branch": "master", + "description": "Easy dependency management for Nix projects", + "homepage": "https://github.com/nmattia/niv", + "owner": "nmattia", + "repo": "niv", + "rev": "65a61b147f307d24bfd0a5cd56ce7d7b7cc61d2e", + "sha256": "17mirpsx5wyw262fpsd6n6m47jcgw8k2bwcp1iwdnrlzy4dhcgqh", + "type": "tarball", + "url": "https://github.com/nmattia/niv/archive/65a61b147f307d24bfd0a5cd56ce7d7b7cc61d2e.tar.gz", + "url_template": "https://github.com///archive/.tar.gz" + }, + "nixpkgs": { + "branch": "release-20.03", + "description": "Nix Packages collection", + "homepage": "", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "eb73405ecceb1dc505b7cbbd234f8f94165e2696", + "sha256": "06k21wbyhhvq2f1xczszh3c2934p0m02by3l2ixvd6nkwrqklax7", + "type": "tarball", + "url": "https://github.com/NixOS/nixpkgs/archive/eb73405ecceb1dc505b7cbbd234f8f94165e2696.tar.gz", + "url_template": "https://github.com///archive/.tar.gz" + } +} diff --git a/nix/sources.nix b/nix/sources.nix new file mode 100644 index 0000000..1938409 --- /dev/null +++ b/nix/sources.nix @@ -0,0 +1,174 @@ +# This file has been generated by Niv. + +let + + # + # The fetchers. fetch_ fetches specs of type . + # + + fetch_file = pkgs: name: spec: + let + name' = sanitizeName name + "-src"; + in + if spec.builtin or true then + builtins_fetchurl { inherit (spec) url sha256; name = name'; } + else + pkgs.fetchurl { inherit (spec) url sha256; name = name'; }; + + fetch_tarball = pkgs: name: spec: + let + name' = sanitizeName name + "-src"; + in + if spec.builtin or true then + builtins_fetchTarball { name = name'; inherit (spec) url sha256; } + else + pkgs.fetchzip { name = name'; inherit (spec) url sha256; }; + + fetch_git = name: spec: + let + ref = + if spec ? ref then spec.ref else + if spec ? branch then "refs/heads/${spec.branch}" else + if spec ? tag then "refs/tags/${spec.tag}" else + abort "In git source '${name}': Please specify `ref`, `tag` or `branch`!"; + in + builtins.fetchGit { url = spec.repo; inherit (spec) rev; inherit ref; }; + + fetch_local = spec: spec.path; + + fetch_builtin-tarball = name: throw + ''[${name}] The niv type "builtin-tarball" is deprecated. You should instead use `builtin = true`. + $ niv modify ${name} -a type=tarball -a builtin=true''; + + fetch_builtin-url = name: throw + ''[${name}] The niv type "builtin-url" will soon be deprecated. You should instead use `builtin = true`. + $ niv modify ${name} -a type=file -a builtin=true''; + + # + # Various helpers + # + + # https://github.com/NixOS/nixpkgs/pull/83241/files#diff-c6f540a4f3bfa4b0e8b6bafd4cd54e8bR695 + sanitizeName = name: + ( + concatMapStrings (s: if builtins.isList s then "-" else s) + ( + builtins.split "[^[:alnum:]+._?=-]+" + ((x: builtins.elemAt (builtins.match "\\.*(.*)" x) 0) name) + ) + ); + + # The set of packages used when specs are fetched using non-builtins. + mkPkgs = sources: system: + let + sourcesNixpkgs = + import (builtins_fetchTarball { inherit (sources.nixpkgs) url sha256; }) { inherit system; }; + hasNixpkgsPath = builtins.any (x: x.prefix == "nixpkgs") builtins.nixPath; + hasThisAsNixpkgsPath = == ./.; + in + if builtins.hasAttr "nixpkgs" sources + then sourcesNixpkgs + else if hasNixpkgsPath && ! hasThisAsNixpkgsPath then + import {} + else + abort + '' + Please specify either (through -I or NIX_PATH=nixpkgs=...) or + add a package called "nixpkgs" to your sources.json. + ''; + + # The actual fetching function. + fetch = pkgs: name: spec: + + if ! builtins.hasAttr "type" spec then + abort "ERROR: niv spec ${name} does not have a 'type' attribute" + else if spec.type == "file" then fetch_file pkgs name spec + else if spec.type == "tarball" then fetch_tarball pkgs name spec + else if spec.type == "git" then fetch_git name spec + else if spec.type == "local" then fetch_local spec + else if spec.type == "builtin-tarball" then fetch_builtin-tarball name + else if spec.type == "builtin-url" then fetch_builtin-url name + else + abort "ERROR: niv spec ${name} has unknown type ${builtins.toJSON spec.type}"; + + # If the environment variable NIV_OVERRIDE_${name} is set, then use + # the path directly as opposed to the fetched source. + replace = name: drv: + let + saneName = stringAsChars (c: if isNull (builtins.match "[a-zA-Z0-9]" c) then "_" else c) name; + ersatz = builtins.getEnv "NIV_OVERRIDE_${saneName}"; + in + if ersatz == "" then drv else + # this turns the string into an actual Nix path (for both absolute and + # relative paths) + if builtins.substring 0 1 ersatz == "/" then /. + ersatz else /. + builtins.getEnv "PWD" + "/${ersatz}"; + + # Ports of functions for older nix versions + + # a Nix version of mapAttrs if the built-in doesn't exist + mapAttrs = builtins.mapAttrs or ( + f: set: with builtins; + listToAttrs (map (attr: { name = attr; value = f attr set.${attr}; }) (attrNames set)) + ); + + # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/lists.nix#L295 + range = first: last: if first > last then [] else builtins.genList (n: first + n) (last - first + 1); + + # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L257 + stringToCharacters = s: map (p: builtins.substring p 1 s) (range 0 (builtins.stringLength s - 1)); + + # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L269 + stringAsChars = f: s: concatStrings (map f (stringToCharacters s)); + concatMapStrings = f: list: concatStrings (map f list); + concatStrings = builtins.concatStringsSep ""; + + # https://github.com/NixOS/nixpkgs/blob/8a9f58a375c401b96da862d969f66429def1d118/lib/attrsets.nix#L331 + optionalAttrs = cond: as: if cond then as else {}; + + # fetchTarball version that is compatible between all the versions of Nix + builtins_fetchTarball = { url, name ? null, sha256 }@attrs: + let + inherit (builtins) lessThan nixVersion fetchTarball; + in + if lessThan nixVersion "1.12" then + fetchTarball ({ inherit url; } // (optionalAttrs (!isNull name) { inherit name; })) + else + fetchTarball attrs; + + # fetchurl version that is compatible between all the versions of Nix + builtins_fetchurl = { url, name ? null, sha256 }@attrs: + let + inherit (builtins) lessThan nixVersion fetchurl; + in + if lessThan nixVersion "1.12" then + fetchurl ({ inherit url; } // (optionalAttrs (!isNull name) { inherit name; })) + else + fetchurl attrs; + + # Create the final "sources" from the config + mkSources = config: + mapAttrs ( + name: spec: + if builtins.hasAttr "outPath" spec + then abort + "The values in sources.json should not have an 'outPath' attribute" + else + spec // { outPath = replace name (fetch config.pkgs name spec); } + ) config.sources; + + # The "config" used by the fetchers + mkConfig = + { sourcesFile ? if builtins.pathExists ./sources.json then ./sources.json else null + , sources ? if isNull sourcesFile then {} else builtins.fromJSON (builtins.readFile sourcesFile) + , system ? builtins.currentSystem + , pkgs ? mkPkgs sources system + }: rec { + # The sources, i.e. the attribute set of spec name to spec + inherit sources; + + # The "pkgs" (evaluated nixpkgs) to use for e.g. non-builtin fetchers + inherit pkgs; + }; + +in +mkSources (mkConfig {}) // { __functor = _: settings: mkSources (mkConfig settings); } diff --git a/package.yaml b/package.yaml deleted file mode 100644 index 5368236..0000000 --- a/package.yaml +++ /dev/null @@ -1,132 +0,0 @@ -name: haskell-starter-kit -version: 0.1.0.0 -github: "fullstack-development/haskell-starter-kit" -license: BSD3 -author: "Author name here" -maintainer: "example@example.com" -copyright: "2021 Author name here" - -extra-source-files: -- README.md -- ChangeLog.md - -# Metadata used when publishing your package -# synopsis: Short description of your package -# category: Web - -# To avoid duplicated efforts in documentation and dealing with the -# complications of embedding Haddock markup inside cabal files, it is -# common to point users to the README.md file. -description: Please see the README on GitHub at - -dependencies: -- base >= 4.7 && < 5 -- mtl -- bytestring -- text -- containers -- time -- unliftio-core -- unliftio -- safe-exceptions -- typerep-map -- random -- stm -- directory -- cryptonite -- memory -- basement - -- configurator -- resourcet -- resource-pool - -# Logger -- co-log -- monad-logger - -# Database -- postgresql-simple -- persistent -- persistent-postgresql -- persistent-template -- esqueleto - -# Web -- aeson -- jose -- http-client -- http-client-tls -- wai -- warp -- wai-logger -- wai-cors -- servant-client -- servant-options -- servant-server -- servant-auth -- servant-auth-server -- servant-foreign - -ghc-options: -- -Wall -- -Werror=missing-fields -- -Werror=incomplete-record-updates -- -Werror=missing-methods -- -Werror=incomplete-patterns - -default-extensions: -- RecordWildCards -- NamedFieldPuns -- OverloadedStrings -- FlexibleContexts -- FlexibleInstances -- StandaloneDeriving -- ScopedTypeVariables -- ExplicitForAll -- TypeOperators -- DeriveGeneric -- MonoLocalBinds -- LambdaCase - -library: - source-dirs: src - - -executables: - haskell-starter-kit-exe: - main: Main.hs - source-dirs: app - ghc-options: - - -threaded - - -rtsopts - - -with-rtsopts=-N - dependencies: - - haskell-starter-kit - when: # workaround from https://github.com/sol/hpack/issues/303 - - condition: false - other-modules: Paths_haskell_starter_kit - -tests: - haskell-starter-kit-test: - main: Spec.hs - source-dirs: test - ghc-options: - - -threaded - - -rtsopts - - -with-rtsopts=-N - dependencies: - - haskell-starter-kit - - - hspec - - hspec-core - - hspec-wai - - tasty - - tasty-hspec - - # extra - - http-types - - wai-extra - when: # workaround from https://github.com/sol/hpack/issues/303 - - condition: false - other-modules: Paths_haskell_starter_kit diff --git a/shell.nix b/shell.nix new file mode 100644 index 0000000..4208e76 --- /dev/null +++ b/shell.nix @@ -0,0 +1,14 @@ +let + project = import ./default.nix; +in +project.shellFor { + withHoogle = true; + tools = { + cabal = "3.2.0.0"; + hlint = "latest"; + haskell-language-server = "latest"; + }; + + buildInputs = [(import {}).git]; + exactDeps = true; +} \ No newline at end of file diff --git a/src/AppName/Auth/ServantInstances.hs b/src/AppName/Auth/ServantInstances.hs index df90238..e507c6c 100644 --- a/src/AppName/Auth/ServantInstances.hs +++ b/src/AppName/Auth/ServantInstances.hs @@ -1,6 +1,10 @@ {-# LANGUAGE DataKinds #-} {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE TypeFamilies #-} +{-# LANGUAGE RankNTypes #-} +{-# LANGUAGE TypeOperators #-} +{-# LANGUAGE FlexibleInstances #-} +{-# LANGUAGE FlexibleContexts #-} {-# OPTIONS_GHC -fno-warn-orphans #-} module AppName.Auth.ServantInstances where diff --git a/src/AppName/Gateways/Database/Setup.hs b/src/AppName/Gateways/Database/Setup.hs index 7294fa1..172e738 100644 --- a/src/AppName/Gateways/Database/Setup.hs +++ b/src/AppName/Gateways/Database/Setup.hs @@ -16,7 +16,8 @@ import Control.Monad.Logger NoLoggingT, runNoLoggingT, runStdoutLoggingT, - ) + MonadLoggerIO + )˝ import Data.Functor (($>)) import Data.Pool (Pool, destroyAllResources) import Database.Persist.Postgresql (SqlBackend, createPostgresqlPoolModified) @@ -29,7 +30,7 @@ withDbPool :: C.Config -> (Pool SqlBackend -> NoLoggingT IO a) -> IO a withDbPool = withLoggedDbPool runNoLoggingT withLoggedDbPool :: - (MonadUnliftIO m, MonadLogger m, MonadMask m) => + (MonadUnliftIO m, MonadMask m, MonadLoggerIO m) => (m a -> IO a) -> C.Config -> (Pool SqlBackend -> m a) -> diff --git a/stack.yaml b/stack.yaml deleted file mode 100644 index 27c28f7..0000000 --- a/stack.yaml +++ /dev/null @@ -1,70 +0,0 @@ -# This file was automatically generated by 'stack init' -# -# Some commonly used options have been documented as comments in this file. -# For advanced use and comprehensive documentation of the format, please see: -# https://docs.haskellstack.org/en/stable/yaml_configuration/ - -# Resolver to choose a 'specific' stackage snapshot or a compiler version. -# A snapshot resolver dictates the compiler version and the set of packages -# to be used for project dependencies. For example: -# -# resolver: lts-3.5 -# resolver: nightly-2015-09-21 -# resolver: ghc-7.10.2 -# -# The location of a snapshot can be provided as a file or url. Stack assumes -# a snapshot provided as a file might change, whereas a url resource does not. -# -# resolver: ./custom-snapshot.yaml -# resolver: https://example.com/snapshots/2018-01-01.yaml -resolver: lts-17.5 - -# User packages to be built. -# Various formats can be used as shown in the example below. -# -# packages: -# - some-directory -# - https://example.com/foo/bar/baz-0.0.2.tar.gz -# subdirs: -# - auto-update -# - wai -packages: -- . -# Dependency packages to be pulled from upstream that are not in the resolver. -# These entries can reference officially published versions as well as -# forks / in-progress versions pinned to a git hash. For example: -# -# - acme-missiles-0.3 -# - git: https://github.com/commercialhaskell/stack.git -# commit: e7b331f14bcffb8367cd58fbfc8b40ec7642100a -extra-deps: - - servant-options-0.1.0.0 - - servant-auth-0.4.0.0 - - servant-auth-server-0.4.6.0 - -# -# extra-deps: [] - -# Override default flag values for local packages and extra-deps -# flags: {} - -# Extra package databases containing global packages -# extra-package-dbs: [] - -# Control whether we use the GHC we find on the path -# system-ghc: true -# -# Require a specific version of stack, using version ranges -# require-stack-version: -any # Default -# require-stack-version: ">=2.5" -# -# Override the architecture used by stack, especially useful on Windows -# arch: i386 -# arch: x86_64 -# -# Extra directories used by stack for building -# extra-include-dirs: [/path/to/dir] -# extra-lib-dirs: [/path/to/dir] -# -# Allow a newer minor version of GHC than the snapshot specifies -# compiler-check: newer-minor diff --git a/stack.yaml.lock b/stack.yaml.lock deleted file mode 100644 index 92040a6..0000000 --- a/stack.yaml.lock +++ /dev/null @@ -1,33 +0,0 @@ -# This file was autogenerated by Stack. -# You should not edit this file by hand. -# For more information, please see the documentation at: -# https://docs.haskellstack.org/en/stable/lock_files - -packages: -- completed: - hackage: servant-options-0.1.0.0@sha256:39f50166a68006250e79370372f553ca476f14d06d93fa0c401050253aeba803,914 - pantry-tree: - size: 293 - sha256: 7812baf1b27c62068ba634207fbd7077b886424d666b36739a78c6d4008a1fd4 - original: - hackage: servant-options-0.1.0.0 -- completed: - hackage: servant-auth-0.4.0.0@sha256:01d02dfb7df4747fc96442517146d7d4ab0e575e597a124e238e8763036ea4ff,2125 - pantry-tree: - size: 332 - sha256: a7ec2c54fddfa417ee2467f1131b1d83ad43cc29ddfd4b98869fc924e4b5e980 - original: - hackage: servant-auth-0.4.0.0 -- completed: - hackage: servant-auth-server-0.4.6.0@sha256:b411b44f4252e91e5da2455d71a7113c8b5b8ff2d943d19b2ddedcfcf0392351,5111 - pantry-tree: - size: 1376 - sha256: 8cd29612112546a02085e2cb30dd5a72ea46ed81e644195c589d0b26652279cb - original: - hackage: servant-auth-server-0.4.6.0 -snapshots: -- completed: - size: 565266 - url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/17/5.yaml - sha256: 78e8ebabf11406261abbc95b44f240acf71802630b368888f6d758de7fc3a2f7 - original: lts-17.5