Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't Aleph's HTTP server do anything?

Tags:

clojure

aleph

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.

like image 851
mipadi Avatar asked Sep 14 '16 00:09

mipadi


2 Answers

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

like image 178
ztellman Avatar answered Sep 18 '22 02:09

ztellman


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.

like image 29
Jonah Benton Avatar answered Sep 18 '22 02:09

Jonah Benton