Skip to content

Instantly share code, notes, and snippets.

@dfuenzalida
Created January 14, 2020 02:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dfuenzalida/ade6f79821e88b05c5014c1e350c90f7 to your computer and use it in GitHub Desktop.
Save dfuenzalida/ade6f79821e88b05c5014c1e350c90f7 to your computer and use it in GitHub Desktop.
janet littleserver + json responses
denis@carbon:~/Projects/janet/littleserver$ janet -v
1.6.1-dev-0457966
denis@carbon:~/Projects/janet/littleserver$ jpm --verbose build
generating executable c source...
found native /usr/local/lib/janet/circlet.so...
found native /usr/local/lib/janet/json.so...
compiling and linking build/lserve...
cc -std=c99 -Wall -Wextra -I/usr/local/include/janet -O2 -o build/lserve build/lserve.c /usr/local/lib/janet/circlet.a /usr/local/lib/janet/json.a /usr/local/bin/../lib/libjanet.a -lm -ldl -lrt -lpthread
/usr/local/lib/janet/json.a(json.static.o): In function `_janet_mod_config':
json.c:(.text+0x13f0): multiple definition of `_janet_mod_config'
/usr/local/lib/janet/circlet.a(circlet.static.o):circlet.c:(.text+0xa70): first defined here
collect2: error: ld returned 1 exit status
error: command exited with status 1
in shell [/usr/local/bin/jpm] (tailcall) on line 224, column 5
in do-rule [/usr/local/bin/jpm] on line 89, column 5
in do-rule [/usr/local/bin/jpm] (tailcall) on line 87, column 44
(import circlet)
(import json)
(defn json-response [data]
{:status 200
:headers {"Content-Type" "application/json"}
:body (json/encode data)})
(defn main [&]
(circlet/server
(fn [req]
(let [{:uri uri :method method :protocol protocol :query-string query-string} req]
(printf "%s %s %s %s" protocol method uri query-string)
(cond
(= "/" uri) (json-response @{:message "welcome page" :date (os/date)})
:default (json-response @{:message "default page" :date (os/date)}))))
8000))
(declare-project
:name "littleserver"
:description "Example server to show how to use jpm to build
a project with native, third-party dependencies into an executable."
:dependencies ["https://github.com/janet-lang/circlet.git"
"https://github.com/janet-lang/json.git"])
(declare-executable
:name "lserve"
:entry "main.janet")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment