agrafix / Spock-scotty-benchmark

Haskell: Spock vs scotty benchmark
10 stars 2 forks source link

Upgrade code to scotty 0.11.0 and spock 0.12.0.0 #5

Open flip111 opened 6 years ago

flip111 commented 6 years ago

I was trying to upgrade the code to the latest versions of this framework. I made some modifications the code compiles but ./bench.sh gives Invalid http://localhost:8080/echo/plain/hello repsonse! Expected 'hello' got '' (Is the server running?)

{-# LANGUAGE OverloadedStrings #-}
module Main where

import System.Environment

import qualified Web.Scotty as Scotty
import qualified Web.Spock as Spock
import qualified Web.Spock.Config as SpockConfig

main =
    do args <- getArgs
       let port = 8080
       case args of
         ["scotty"] ->
             Scotty.scotty port $
             do Scotty.get "/echo/hello-world" $
                  Scotty.text "Hello World"
                Scotty.get "/echo/plain/:param" $
                  do p <- Scotty.param "param"
                     Scotty.text p
                Scotty.get (Scotty.regex "^/echo/regex/([0-9]+)$") $
                  do p <- Scotty.param "1"
                     Scotty.text p
         ["spock"] -> do
             spockCfg <- SpockConfig.defaultSpockCfg () SpockConfig.PCNoDatabase ()
             Spock.runSpock port $ Spock.spock spockCfg $
                 do Spock.get "/echo/hello-world" $
                      Spock.text "Hello World"
                    Spock.get "/echo/plain/:param" $
                      do Just p <- Spock.param "param"
                         Spock.text p
                    Spock.get "/echo/regex/{param:^[0-9]+$}" $
                      do Just p <- Spock.param "param"
                         Spock.text p
         _ -> putStrLn "Usage: ./Spock-scotty-benchmark spock|scotty"
agrafix commented 6 years ago

Well the way to define routes changed. /echo/regex/{param:^[0-9]+$} is not supported in that syntax anymore.

flip111 commented 6 years ago

I fixed the code, but how to specify the regex now?

{-# LANGUAGE OverloadedStrings #-}
module Main where

import System.Environment

import qualified Web.Scotty as Scotty
import qualified Web.Spock as Spock
import qualified Web.Spock.Config as SpockConfig

main =
    do args <- getArgs
       let port = 8080
       case args of
         ["scotty"] ->
             Scotty.scotty port $
             do Scotty.get "/echo/hello-world" $
                  Scotty.text "Hello World"
                Scotty.get "/echo/plain/:param" $
                  do p <- Scotty.param "param"
                     Scotty.text p
                Scotty.get (Scotty.regex "^/echo/regex/([0-9]+)$") $
                  do p <- Scotty.param "1"
                     Scotty.text p
         ["spock"] -> do
             spockCfg <- SpockConfig.defaultSpockCfg () SpockConfig.PCNoDatabase ()
             Spock.runSpock port $ Spock.spock spockCfg $
                 do Spock.get ("echo" Spock.<//> "hello-world") $
                      Spock.text "Hello World"
                    Spock.get ("echo" Spock.<//> "plain" Spock.<//> Spock.var) $ \param ->
                      Spock.text param
                    Spock.get ("echo" Spock.<//> "regex" Spock.<//> Spock.var) $ \param ->
                      do Spock.text param
         _ -> putStrLn "Usage: ./Spock-scotty-benchmark spock|scotty"
agrafix commented 6 years ago

Write a newtype for it and then implement the regex checking using http://hackage.haskell.org/package/http-api-data-0.3.5/docs/Web-Internal-HttpApiData.html#t:FromHttpApiData

flip111 commented 6 years ago

The documentation you linked doesn't contain anything about regex. Do you have a recommendation about a regex library? There seem to be quite a few https://wiki.haskell.org/Regular_expressions