Simple HTTP Service - Programming Language Comparison

Posted on September 6, 2015

Clojure

(ns myapp.core
  (:require [cheshire.core :refer :all]
            [compojure.core :refer :all]
            [org.httpkit.server :refer [run-server]]
            [ring.middleware.defaults :refer :all]))

(defroutes myapp
  (GET "/hello" req
       (let [name (get (:params req) :name)]
         (generate-string {:greeting (str "hello, " name)}))))

(defn -main []
  (run-server (wrap-defaults myapp site-defaults) {:port 5000}))

Haskell

{-# LANGUAGE OverloadedStrings #-}

module Main where

import Data.Map as M
import Web.Scotty

main :: IO ()
main = scotty 5000 $ do
  get "/hello" $ do
    name <- param "name"
    json $ M.fromList [("greeting" :: String, "hello, " ++ name :: String)]

Python

from flask import Flask, request
import json

app = Flask(__name__)

@app.route('/')
def hello():
    return json.dumps({'greeting': 'hello, %s' % request.args.get('name')})

if __name__ == '__main__':
    app.run()