手撸了一个简单 web 框架,拿来做博客
这两个半个月自己简单手撸了个 web 框架。用得是 PureScript 撸的, PureScript 跟 CoffeeScript 一样是一门 target 到 JavaScript 的语言,虽然 PureScript 从语言本身的设计角度来说比 CoffeeScript 不知道高到哪里去了……但那也架不住 CoffeeScript 简单方便易学大众有生态。
其实拿 PureScript 手撸这个 web 框架是用来炫技的, PureScript 的强类型纯函数式的特性我觉得真的不太适合应用场景,把本身很简单的事情变复杂了。用 JavaScript / Ruby / Python / PHP 我实现这些东西顶多就两三天。
》》Github 传送门
》》LoveAria.Me 博客主站
下面讲讲我主要做的事情:
1.SQLite3 的 PureScript Binding ,以及一个简单的 ORM (还是半残状态,连 JOIN 我都没实现好,强类型面对这种场景只能日狗)。
虽然是自己手撸的……但是放心,注入不了的,本宝宝做了防注入的。
可以用下面哪种好看的方式做查询了,例如:
insert "article" [ "title" .= title
, "category_id" .= category_id
, "raw_content" .= content
, "content" .= Markdown content ]
update "article" [ "title" .= title
, "category_id" .= category_id
, "raw_content" .= content
, "content" .= Markdown content] ("id" .== id)
first "article" ("id" .== id .&& "user_id" .== user_id) (Asc "id")
findall "article" ("category_id" .<- [cid1, cid2, cid3]) (Desc "id")
2.一门 HTML 模板 DSL 。因为是 PureScript 的 DSL ,所以其实还是 PureScript 本身。只是语法看上去像一门单独的语言而已, 并且还是有些丑的。
list :: Array Category.RichArticle -> Template
list articles = do
base
title "Article"
extend "body" $ do
ifLogined $ \_ ->
t_a [a_href := "/article/create"] $ text "Create"
article_list articles
article_list :: Array Category.RichArticle -> Template
article_list articles = do
t_table [] do
t_tr [] do
t_th [] $ text "Id"
t_th [] $ text "Title"
t_th [] $ text "Category"
t_th [] $ text "Create At"
t_th [] $ text "Update At"
forT articles $ \article -> do
t_tr [] do
t_td [] $ text $ show article.id
t_td [] do
t_a [a_href := "/article/show/" ++ show article.id]
$ text article.title
t_td [] do
t_a [a_href := "/article/category/" ++ show article.category.id]
$ text article.category.name
t_td [] $ text article.create_at
t_td [] $ text article.update_at
3.基础的框架是在 PureScript-Express 上面建立的。所以基本的 handler 处理还是沿用 Express 。
main :: forall e. ModelApp (console :: CONSOLE | e)
main = do
liftEff $ log "Setting up"
setProp "json spaces" 4.0
useExternal $ MW.bodyParser {extended: false}
static_path <- liftEff $ Config.static_path
useExternalAt "/static" $ MW.static static_path
useExternal $ cookieSession {secret: Config.security_key}
useExternal $ MW.setCookiesMaxAge (3600 * 24 * 30)
use CacheHandler.logger
use CacheHandler.cacheMiddleware
mount "/" HomeHandler.main
mount "/user" UserHandler.main
mount "/article" ArticleHandler.main
mount "/category" CategoryHandler.main
mount "/cache" CacheHandler.main
4.现在博客的功能基本完善。讲一下框架之上博客的功能:
- 文章的增删改,搜索功能还没做,感觉没什么必要做。在下一条讲
- 不限制层次的分类。对的,分类就是树状递归结构,不限制层数。你爱几层就几层,跟文件系统的目录类似。用这样的分类来整理文章,我觉得根本没必要搜索了吧。而且我这一辈子能写一千篇高质量的博客不?
- 页面缓存。因为 ORM 设计的问题和分类目录递归查询量很大,所以我干脆就直接给他们做了一个 10 分钟的页面缓存。登录成功的用户忽略缓存。成功发布或者修改 文章和分类 会简单全部刷新缓存。
5.关于前端
前端方面我准备使用 PureCSS 来做基本的 CSS 框架,然后做一个简洁好看的 UI 。 /w\ 没错,本宝宝就是那么纯( Pure ) 。 JavaScript 部分依旧还是用 PureScript 来做。当然 PureScript 写前端也有些蛋疼,不过我能折腾 /w\。
啊~ 终于能够好好写东西并且实验奇怪小东西的地方啊啦~