Implement TLS for API server

This commit is contained in:
Rene Vergara 2022-05-19 09:52:17 -05:00
parent 0d56026183
commit 3574beab58
Signed by: pitmutt
GPG key ID: 65122AD495A7F5B2
5 changed files with 182 additions and 184 deletions

View file

@ -6,7 +6,9 @@ import Control.Concurrent (forkIO)
import Data.Configurator import Data.Configurator
import Data.SecureMem import Data.SecureMem
import Database.MongoDB import Database.MongoDB
import Network.Wai.Handler.WarpTLS (tlsSettings) import Network.Wai.Handler.Warp (defaultSettings, setPort)
import Network.Wai.Handler.WarpTLS (runTLS, tlsSettings)
import Web.Scotty
import ZGoBackend import ZGoBackend
main :: IO () main :: IO ()
@ -36,4 +38,9 @@ main = do
_ <- forkIO (setInterval 75 (scanZcash nodeAddress pipe dbName)) _ <- forkIO (setInterval 75 (scanZcash nodeAddress pipe dbName))
_ <- forkIO (setInterval 60 (checkPayments pipe dbName)) _ <- forkIO (setInterval 60 (checkPayments pipe dbName))
_ <- forkIO (setInterval 60 (expireOwners pipe dbName)) _ <- forkIO (setInterval 60 (expireOwners pipe dbName))
app pipe dbName passkey nodeAddress port myTlsSettings let appRoutes = routes pipe dbName passkey nodeAddress
case myTlsSettings of
Nothing -> scotty port appRoutes
Just tls -> do
apiCore <- scottyApp appRoutes
runTLS tls (setPort port defaultSettings) apiCore

View file

@ -72,6 +72,7 @@ executables:
- bytestring - bytestring
- configurator - configurator
- warp-tls - warp-tls
- warp
tests: tests:
zgo-backend-test: zgo-backend-test:

View file

@ -29,7 +29,6 @@ import GHC.Generics
import Item import Item
import Network.HTTP.Simple import Network.HTTP.Simple
import Network.HTTP.Types.Status import Network.HTTP.Types.Status
import Network.Wai.Handler.WarpTLS (TLSSettings, runTLS)
import Network.Wai.Middleware.Cors import Network.Wai.Middleware.Cors
import Network.Wai.Middleware.HttpAuth import Network.Wai.Middleware.HttpAuth
import Numeric import Numeric
@ -314,18 +313,10 @@ upsertZGoTx :: T.Text -> ZGoTx -> Action IO ()
upsertZGoTx coll t = do upsertZGoTx coll t = do
upsert (select ["txid" =: txid t] coll) (encodeZGoTxBson t) upsert (select ["txid" =: txid t] coll) (encodeZGoTxBson t)
-- | Main API function -- | Main API routes
app :: routes :: Pipe -> T.Text -> SecureMem -> T.Text -> ScottyM ()
Pipe routes pipe db passkey nodeAddress = do
-> T.Text
-> SecureMem
-> T.Text
-> Integer
-> Maybe TLSSettings
-> IO ()
app pipe db passkey nodeAddress port tls = do
let run = access pipe master db let run = access pipe master db
scotty 3000 $ do
middleware $ middleware $
cors $ cors $
const $ const $
@ -443,9 +434,7 @@ app pipe db passkey nodeAddress port tls = do
status ok200 status ok200
Web.Scotty.json Web.Scotty.json
(object (object
[ "message" .= ("Items found!" :: String) ["message" .= ("Items found!" :: String), "items" .= toJSON pItems])
, "items" .= toJSON pItems
])
--Upsert item --Upsert item
post "/api/item" $ do post "/api/item" $ do
i <- jsonData i <- jsonData

View file

@ -81,6 +81,7 @@ executable zgo-backend-exe
, text , text
, time , time
, wai-extra , wai-extra
, warp
, warp-tls , warp-tls
, zgo-backend , zgo-backend
default-language: Haskell2010 default-language: Haskell2010

View file

@ -5,5 +5,5 @@ dbUser = "zgo"
dbPassword = "zcashrules" dbPassword = "zcashrules"
port = 3000 port = 3000
tls = false tls = false
cert = "/path/to/cert.pem" certificate = "/path/to/cert.pem"
key = "/path/to/key.pem" key = "/path/to/key.pem"