5

I've written a relatively simple HTTP server using Clojure's Aleph library. It's not very complicated:

(ns cxpond.xmlrpc.core
  (:gen-class)
  (:require [aleph.http :as http]))

(defn handler [req]
  {:status 200
   :headers {"Content-Type" "text/plain"}
   :body "HELLO, WORLD!"})

(defn -main [& args]
  (http/start-server service/handler {:port 8005}))

Obviously it's pretty simple, and follows the example given in Aleph's doc pretty closely. It compiles fine, but when I run it (via lein run) it just...does nothing. The program just exits immediately; obviously it doesn't listen on port 8005 or anything like that. What am I missing here? Clearly there must be something else I need to do to start a server in Aleph.

mipadi
  • 398,885
  • 90
  • 523
  • 479
  • 1
    Have you looked at Ring? There is a good introduction online at: https://github.com/clojure-cookbook/clojure-cookbook/blob/master/07_webapps/7-00_introduction.asciidoc A very nice print version of the book (O'Reilly) is also available: http://clojure-cookbook.com/ – Alan Thompson Sep 14 '16 at 03:49
  • I am also very impressed with the book Web Development with Clojure: https://pragprog.com/book/dswdcloj2/web-development-with-clojure-second-edition – Alan Thompson Sep 14 '16 at 03:50
  • @AlanThompson: Thanks! I hadn't thought to look at ring, but I think it may do the job nicely here. – mipadi Sep 14 '16 at 17:13

2 Answers2

11

You'll want to call 'aleph.netty/wait-for-close' on the value returned by 'start-server' to block until the server is closed.

ztellman
  • 756
  • 4
  • 7
5

http/start-server doesn't block, just returns an object, so with nothing else to do execution of -main finishes and the program ends.

I don't use aleph and don't see an obvious join-like pattern. It looks as though one has to do one's own lifecycle management, then call .close on the object returned from start-server to gracefully shut down.

Jonah Benton
  • 3,598
  • 1
  • 16
  • 27