之前的博客上講了開發板上移植appweb的過程,這篇博客就記錄一下如何開發一個典型的基於appweb的mvc架構的網站
1.前言
1.1環境參數
開發板:mini2440 64MB內存 256MB Flash
PC系統:X86 ubuntu10.04 gcc:4.4.3
交叉編譯器:gcc 4.4.3
appweb版本:5.0.0-rc1
1.2簡介
ESP全稱是Embedded Server Page,和jsp(Java Server Pages)很相似,前者支持將C語言嵌套到html中後者支持將Java語言嵌套到html中來實現動態網頁。下面摘抄一點官網上的原話:
ESP is the blazing fast "C" language web framework that works at thought-speed.ESP is not a traditional low-level environment. It is a powerhouse MVC framework in a tiny footprint with most things you'd expect from an enterprise web framework including: MVC, scaffolds, templates, WebSockets, integrated databases, database migrations and more.
ESP的document:https://embedthis.com/esp/doc/index.html
AppWeb的document:https://embedthis.com/appweb/doc/index.html
其他工具的網址:https://embedthis.com/products.html
2.一個簡單的MVC登錄
由於開發的是嵌入式web,每改動一點內容就要重新編譯然後再下載到開發板中測試,很是麻煩,所以一般採取的方法是先在PC上開發好,測試好後然後移植到開發板上。所以我們先要在PC上安裝AppWeb以及其它工具,然後開發一個web應用,最後移植到開發板上。
2.1向PC上安裝AppWeb
下載源碼:
去網址:http://appwebserver.org/downloads/appweb/download.esp下載appweb的源碼,我下載的版本是appweb5.0
編譯:
(i)解壓源碼文件
(ii)把文件appweb-linux-default-me.h中的#define ME_COMPILER_HAS_SYNC64 1註釋掉,不然會報錯“undefined reference to `__sync_add_and_fetch_8'collect2: ld returned 1 exit status”
//#define ME_COMPILER_HAS_SYNC64 1
(iii)make
make -f projects/appweb-linux-default.mk
編譯完後會在../appweb-5.0.0-rc1/linux-x86-default文件下生成目標文件將
(iiii)安裝
make –f project/appweb-linux-default.mk install
2.2利用appweb提供的工具建立MVC模板
參考網址:https://embedthis.com/esp/doc/guide/esp/start/mvc-tour.html
這個程序很簡單,就是輸入用戶名和密碼,如果用戶名是“abc”密碼是“123”那麼頁面就跳到loginOK頁面,如果不是那麼頁面就跳轉到loginError頁面。
目的是熟悉流程和簡單地理解appweb,裏面沒有涉及到數據庫,所以MVC中的M就沒有很好地體現,主要是V和C,如果要完整的實現可以認真地閱讀上面的參考網址。
2.2.1模板生成
(1)$ mkdirmylogin
(2)$cd mylogin
(3)$esp install esp-html-mvc
執行完(3)後會在文件夾裏面出現:client layouts package.json paks這幾個文件或是文件夾,將package.json中的auth中的內容type刪除,不然會報錯:esp: Error: Cannot find auth type app
(4)$esp generate controller login
執行(4)的目的是產生controller,執行玩了之後就會發現文件夾裏面多了個文件夾controllers,裏面已經自動生成了login.c
2.2.2 修改/編寫View
(1)修改mylogin/client/index.esp頁面爲用戶名密碼形式,這裏一定要加這句話<% inputSecurityToken(); %>不然的話會報錯誤:
Access Error: 401 -- Unauthorized
Security token is stale. Please reload page.
<html>
<head>
<title>MyLogin</title>
</head>
<body>
<h1>MyLogin</h1>
<form action=/do/login/check method=POST>
<table>
<tr>
<td>Name:</td><td><input type=text name=name size=50 value=""></td>
</tr>
<tr>
<td>Pwd:</td><td><input type=text name=pwd size=50 value=""></td>
</tr>
<tr>
<td align="CENTER">
<input type=submit name=login value="Login">
<% inputSecurityToken();%>
</td>
</tr>
</table>
</form>
</body>
</html>
(2)在mylogin/client文件夾下建立app/login文件夾,在裏面新建兩個esp頁面,login-ok.esp和login-error.esp
login-ok.esp:
<html>
<head>
<title>OK</title>
</head>
<body>
<h1>login OK</h1>
</body>
</html>
login-error.esp:
<html>
<head>
<title>error</title>
</head>
<body>
<h1>login Error</h1>
</body>
</html>
2.2.3 修改/編寫Controller這裏主要是修改controller文件夾下的login.c,這裏的action的命名是這樣的:比如./do/login/check的uri對應的就是login-cmd-check
/*
login Controller for esp-html-mvc (esp-html-mvc)
*/
#include "esp.h"
static void checkLogin() {
cchar *name = param("name");
cchar *pwd = param("pwd");
if(smatch("abc",name) && smatch("123",pwd))
{
renderView("login/login-ok");
}else{
renderView("login/login-error");
}
}
static void common(HttpConn *conn) {
}
/*
Dynamic module initialization
*/
ESP_EXPORT int esp_controller_mylogin_login(HttpRoute *route, MprModule *module) {
espDefineBase(route, common);
espDefineAction(route, "login-cmd-check", checkLogin);
return 0;
}
2.2.3編譯
$esp compile
如果沒有錯誤的話,機會生成cache文件,裏面的.so文件就是appweb要加載的文件,這裏我們可以看到,esp會被“翻譯”成c文件,就像jsp會被“翻譯”成java文件(servlet)一樣
2.2.4配置文件
爲了節省空間我把不必要的註釋和其它的東西都刪除了,如果想看完整版(裏面有很多的註釋,建議看一下),就去看appweb-5.0.0-rc1/src/server/sample.conf
需要注意的是以下幾個:
(1)Listen 4000 監聽端口
(2)Documents /home/jxm 這個表示你的web應用所在的目錄,比如我的文件夾mylogin就在/home/jxm文件夾下
(3)EspRoute
name="login" methods="GET,POST" prefix="^/{controller}(~/{action}~)" target="${controller}-${action}" source="${controller}.c" 這個文件就是配置action的語句,只需要修改name就行,其它的用模板裏面的就行
ErrorLog "error.log" size=10MB level=2 backup=5 anew stamp=1hr
Listen 4000
Documents /home/jxm
AddHandler fileHandler html gif jpeg jpg png pdf ico css js txt ""
<if ESP_MODULE>
LoadModule espHandler libmod_esp
AddHandler espHandler esp
EspUpdate off
EspKeepSource on
EspApp name=mylogin
EspRoute name="login" methods="GET,POST" prefix="^/{controller}(~/{action}~)" target="${controller}-${action}" source="${controller}.c"
<else>
AddHandler errorHandler esp
</if>
Cache 1day
SessionTimeout 30mins
RequestParseTimeout 5sec
RequestTimeout 10mins
InactivityTimeout 1min
ExitTimeout 30secs
LimitWorkers 4
MemoryPolicy restart
LimitBuffer 32K
LimitMemory 100MB
LimitCache 10MB
LimitCacheItem 200K
LimitClients 20
LimitChunk 64K
LimitConnections 50
LimitFiles 0
LimitRequestsPerClient 20
LimitKeepAlive 200
LimitRequestBody 100K
LimitRequestForm 32K
LimitRequestHeader 32K
LimitRequestHeaderLines 64
LimitResponseBody 2GB
LimitSessions 100
LimitUpload 1GB
LimitUri 8K
2.2.5啓動
$appweb
--config sample.conf
2.2.6訪問
向瀏覽器中輸入網址:127.0.0.1:4000就會出現登錄界面,如果要去掉最上面的“Home”banner,需要將文件夾mylogin/layouts中的default.esp文件刪除,然後重新編譯再運行
2.2移植到開發版
2.2.1 交叉編譯
在編譯前建議將cache文件夾刪除
$export CC=arm-linux-gcc
$esp --platform../appweb-5.0.0-r1/linux-arm-release(這個路徑是你之前交叉編譯appweb產生的目錄) compile
然後將整個mylogin文件拷貝到開發板中,這裏需要注意的是,由於目錄變化所以在sample.conf 中的Document的目錄也要變