fizzbuzz-as-a-service (empty) → 0.1.0.0
raw patch · 3 files changed
+101/−0 lines, 3 filesdep +asyncdep +basedep +bytestring
Dependencies added: async, base, bytestring, network-simple, optparse-applicative
Files
- app/fizzbuzz-server.hs +58/−0
- fizzbuzz-as-a-service.cabal +30/−0
- license.txt +13/−0
+ app/fizzbuzz-server.hs view
@@ -0,0 +1,58 @@+{-# LANGUAGE OverloadedStrings #-}++module Main (main) where++import Prelude++import Control.Concurrent (threadDelay)+import Data.ByteString (ByteString)+import Data.Semigroup ((<>))+import Data.String (fromString)+import Numeric.Natural (Natural)++import qualified Network.Simple.TCP as Net++import qualified Options.Applicative as Opt++main :: IO ()+main =+ getArgs >>= \args ->+ Net.serve Net.HostAny (argPort args) accept++accept :: (Net.Socket, Net.SockAddr) -> IO ()+accept (s, _) =+ go 1+ where+ go i =+ Net.send s (fizzbuzz i) *>+ Net.send s "\n" *>+ threadDelay 500000 *>+ go (succ i)++fizzbuzz :: Natural -> ByteString+fizzbuzz i =+ case (i `mod` 3 == 0, i `mod` 5 == 0) of+ (True, False) -> "Fizz"+ (False, True) -> "Buzz"+ (True, True) -> "Fizz buzz"+ (False, False) -> fromString (show i)++data Args = Args+ { argPort :: String+ }++getArgs :: IO Args+getArgs =+ Opt.execParser $+ Opt.info (Opt.helper <*> argP) (Opt.header "FizzBizz as a service.")++argP :: Opt.Parser Args+argP = Args <$> portP++portP :: Opt.Parser Net.ServiceName+portP =+ Opt.strOption $+ Opt.long "port" <>+ Opt.short 'p' <>+ Opt.metavar "PORT" <>+ Opt.help "Service port to bind."
+ fizzbuzz-as-a-service.cabal view
@@ -0,0 +1,30 @@+-- This file has been generated from package.yaml by hpack version 0.17.0.+--+-- see: https://github.com/sol/hpack++name: fizzbuzz-as-a-service+version: 0.1.0.0+synopsis: FizzBuzz as a service.+description: FizzBuzz as a service.+category: Network APIs+homepage: https://github.com/chris-martin/haskell-libraries+author: Chris Martin <ch.martin@gmail.com>+maintainer: Chris Martin <ch.martin@gmail.com>+license: Apache-2.0+license-file: license.txt+build-type: Simple+cabal-version: >= 1.10++executable fizzbuzz-server+ main-is: fizzbuzz-server.hs+ hs-source-dirs:+ app+ default-extensions: NoImplicitPrelude+ ghc-options: -Wall -threaded+ build-depends:+ async+ , base >= 4.9 && < 4.10+ , bytestring+ , network-simple+ , optparse-applicative+ default-language: Haskell2010
+ license.txt view
@@ -0,0 +1,13 @@+Copyright 2017 Chris Martin++Licensed under the Apache License, Version 2.0 (the "License");+you may not use this file except in compliance with the License.+You may obtain a copy of the License at++ http://www.apache.org/licenses/LICENSE-2.0++Unless required by applicable law or agreed to in writing, software+distributed under the License is distributed on an "AS IS" BASIS,+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+See the License for the specific language governing permissions and+limitations under the License.