common-lisphunchentoot

Endpoint defined with define-easy-handler returns 404


I have defined a simple system in helloworld.asd:

(asdf:defsystem #:helloworld
  :description "helloworld"
  :author "Duncan Bayne <duncan@bayne.id.au>"
  :license "WTFNMF"
  :depends-on (#:hunchentoot)
  :serial t
  :components ((:file "package")
               (:file "helloworld")))

... a package definition in package.lisp:

(defpackage #:helloworld
  (:use #:cl #:hunchentoot))

... and a corresponding hello world webserver in helloworld.lisp:

(in-package #:helloworld)

(defvar *acceptor* (make-instance 'acceptor :port 4242))

(start *acceptor*)

(define-easy-handler (greet :uri "/hello") ()
  "<html><body><h1>Hello World!</h1></body></html>")

In the SLIME REPL I start the web server with:

CL-USER> (load "/usr/home/duncan/code/helloworld/helloworld.asd")
CL-USER> (ql:quickload "helloworld")

If I navigate to http://localhost:4242/hello, I'd expect to see my hello world HTML. Instead I get a 404 error, and the log shows:

127.0.0.1 - [2017-08-10 08:18:19] "GET /hello HTTP/1.1" 404 341 "-" "Mozilla/5.0 (X11; FreeBSD amd64; rv:54.0) Gecko/20100101 Firefox/54.0"

I suspect I'm missing something fairly obvious here; any tips / pointers to documentation would be appreciated. System details are:

Clozure Common Lisp Version 1.11 (FreebsdX8664)
FreeBSD 11.1-RELEASE amd64
Hunchentoot 1.2.37
Mozilla Firefox 54.0.1
SLIME 20170804.1113

Solution

  • You are making an instance of ACCEPTOR instead of EASY-ACCEPTOR (or a subclass). The easy handler is registered but your acceptor is not going to use it. This should work, for example:

    (defvar *acceptor* (make-instance 'easy-acceptor :port 4242))
    (start *acceptor*)
    (define-easy-handler (test :uri "/test") () "Pass")