Clojure学习:使用compojure做个特简单的web的例子

照着文档,做个很简单的web,用来学习。
web的功能:把数据库(文本文件)的内容显示到网页上。


1. lein new hello 会产生一个hello文件夹

2. 会在刚产生的hello文件夹里,自动添加一个project.clj文件,内容如下: (不一样可以改)

(defproject hello "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:dependencies [[org.clojure/clojure "1.4.0"]
[compojure "1.1.1"]]
:plugins [[lein-ring "0.7.3"]]
:ring {:handler hello.handler/app}
:profiles
{:dev {:dependencies [[ring-mock "0.1.3"]]}})


3. 把src/hello/handler.clj的内容改成:

(ns hello.handler
(:use compojure.core)
(:require [compojure.handler :as handler]
[compojure.route :as route])
(:import (java.io BufferedReader FileReader)))

(defn db-to-html [file]
(with-open [rdr (BufferedReader. (FileReader. file))]
(apply str (map (fn [line] (str "<h1>"
((read-string line) :title)
"</h1><br><p>"
((read-string line) :body) "</p>\n<hr>"))
(line-seq rdr)))))

(defroutes app-routes
(GET "/" [] (db-to-html "f:/hello/res/a.db"))
(route/not-found "Not Found"))

(def app
(handler/site app-routes))



3. 在hello文件夹下添加res/a.db文件(其实就是文本文件,暂时当做数据库用)
a.db文件的内容是

{:title "标题", :body "文摘内容"}
。。。
。。。


4. CD HELLO -> lein ring server启动服务器
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值