星期三, 7月 16, 2008

[tips] 淺嘗 lift

lift是個由Scala語言所開發的web framework,由於想試玩一下據說連James Gosling都玩的Scala,索性就試著裝裝看lift,看看有沒有機會在上面開發Web APP。據說Scala在.NET及java平台下都可以執行,不過我試的平台是sun jdk 1.5。

首先先安裝好jdk跟maven2,再來打入這一大串,


mvn archetype:create -U \
-DarchetypeGroupId=net.liftweb \
-DarchetypeArtifactId=lift-archetype-basic \
-DarchetypeVersion=0.9 \
-DremoteRepositories=http://scala-tools.org/repo-releases \
-DgroupId=mytestorm.group -DartifactId=mytestorm.app


這會自動建立一個可連結derby database ,有models的ORM骨架的web application. 最厲害的是maven這個工具連scala,jetty這些你缺的dependency都能幫你裝到好。

接下來可以修改mytestorm.app/src/main/scala/bootstrap/liftweb/Boot.scala將db的connection string改成:"jdbc:derby:mytest;create=true" 這等一下會在我的專案目錄mytestorm.app裡建立一個名為mytest的derby db,再打入mvn jetty:run 就可以啟動webserver了(這裡打入mvn tomcat:run的話會幫你裝好tomcat). 因為lift已經事先幫你建好了model,所以現在連到server的8080 port或http://127.0.0.1:8080,就可以看到一個可以登入的歡迎畫面:





到這邊其實就已經有一點django admin模組的味道了,可以註冊帳號跟login什麼的,
如果用django的術語來講,整個lift的架構也不難解釋,django的urlconf跟settings被放在bootstrap/Boot.scala,
model.py被放到scala/your-proj's-group/model這個目錄裡,template是在webapp裡,template tag在scala/your-proj's-group/snippet裡,view在scala/your-proj's-group/view/,
說來說去,實在也是換湯不換藥,大底上目前的web開發就是如此。

如果你還有興趣的話,可以到抓下lift 0.9的release tarball, 然後解開之後到lift-0.9/sites/example裡,用mvn jetty:run 將example都跑起來玩一玩。裏面有幾個sample還滿有趣的,還包括一個comet的聊天室實作。

稍微玩了一下其實沒啥大感覺,主要覺得lift用的maven工具太複雜了,讓整個開發像在變魔術一樣,老是要找東西被裝到那,反而覺得scala沒什麼玩到,主要都在搞設定,另外mavan在裝dependency的時候整個download的過程都要連到國外總站,要裝的package又不少,導致安裝速度變得有點慢,如果能有台灣mirror應該會好一點。

星期一, 7月 14, 2008

[tips] rewrite debian/ubuntu 's lighttpd conf script from perl to python

Today I want to port lighttpd on another platform which basically a debian sarge system but without perl and dpkg package system on it. Since it's a debian based platform so I start from porting debian's binary lighttpd package, however I've found there're some perl script lays in /usr/share/lighttpd which are used when lighttpd startup.

While I can easily dump the result of perl script into a textfile,
and then startup my lighttpd correctly, I thought "maybe port it to python is not a bad idea." (since my target platform has python!), so here is the effort:
create-mime.assign.py

#!/usr/bin/python
#
# This script directly translate from debian's lighttpd perl script:
# create-mime.assign.pl
#
# Author: timchen119.at.nospam.gmail.com
# License: Public Domain
#
import sys

try:
f = open("/etc/mime.types",'r')
extensions = {}
print "mimetype.assign = ("
for line in f:
line = line.strip()
if line.startswith('#'): continue
if line != "":
splitlist = line.split()
if len(splitlist) < 2: continue
mime = splitlist[0]
for ext in splitlist[1:]:
if ext in extensions.keys(): continue
extensions[ext] = 1
print '".%s" => "%s",' % (ext,mime)
f.close()
print ")"
except Exception,e:
print e
sys.exit(1)


include-conf-enabled.py
#!/usr/bin/python
#
# This script directly translate from debian's lighttpd perl script:
# include-conf-enabled.pl
#
# Author: timchen119.at.nospam.gmail.com
# License: Public Domain
#

import os,glob

confdir = "/etc/lighttpd/"
enabled = "conf-enabled/*.conf"

os.chdir(confdir)

for file in sorted(glob.glob(enabled)):
print 'include "%s"' % file

use-ipv6.py
#!/usr/bin/python
#
# This script directly translate from ubuntu's lighttpd perl script:
# use-ipv6.pl
#
# Author: timchen119.at.nospam.gmail.com
# License: Public Domain
#

import socket

##this sometimes not accurate. (like in vserver mode)
#if socket.has_ipv6:
#

try:
if socket.socket(socket.AF_INET6,socket.SOCK_STREAM,0):
print 'server.use-ipv6 = "enable"'
except:
pass

All of these files can be found in http://kalug.linux.org.tw/~tim/lighttpd-debian-python-script/
Well something quite interesting happened when I port the debian's create-mime.assign.pl into python, It's that my python script's final result is not equivalent to perl one and has more mime types than its :
--- perlmime.txt    2008-07-14 15:29:23.000000000 +0800
+++ pymime.txt 2008-07-14 15:29:33.000000000 +0800
@@ -114,6 +114,11 @@
".dvi" => "application/x-dvi",
".rhtml" => "application/x-httpd-eruby",
".flac" => "application/x-flac",
+".pfa" => "application/x-font",
+".pfb" => "application/x-font",
+".gsf" => "application/x-font",
+".pcf" => "application/x-font",
+".pcf.Z" => "application/x-font",
".mm" => "application/x-freemind",
".gnumeric" => "application/x-gnumeric",
".sgf" => "application/x-go-sgf",
@@ -193,6 +198,11 @@
".pk" => "application/x-tex-pk",
".texinfo" => "application/x-texinfo",
".texi" => "application/x-texinfo",
+".~" => "application/x-trash",
+".%" => "application/x-trash",
+".bak" => "application/x-trash",
+".old" => "application/x-trash",
+".sik" => "application/x-trash",
".t" => "application/x-troff",
".tr" => "application/x-troff",
".roff" => "application/x-troff",
@@ -282,6 +292,7 @@
".tgf" => "chemical/x-mdl-tgf",
".mcif" => "chemical/x-mmcif",
".mol2" => "chemical/x-mol2",
+".b" => "chemical/x-molconn-Z",
".gpt" => "chemical/x-mopac-graph",
".mop" => "chemical/x-mopac-input",
".mopcrt" => "chemical/x-mopac-input",

So I start to dig why this happened, and I've found a strange perl regex filter all these mimetypes out, I believe it's a minor bug in original perl program. (or it does implicitly doing something meaningful? well I can't figure it out.)
--- create-mime.assign.pl    2008-07-14 15:35:58.000000000 +0800
+++ create-mime.assign.pl.new 2008-07-14 15:36:07.000000000 +0800
@@ -7,7 +7,7 @@
chomp;
s/\#.*//;
next if /^\w*$/;
- if(/^([a-z0-9\/+-.]+)\s+((?:[a-z0-9.+-]+[ ]?)+)$/) {
+ if(/^([A-Za-z0-9\/+-.~%]+)\s+((?:[A-Za-z0-9.+-~%]+[ ]?)+)$/) {
foreach(split / /, $2) {
# mime.types can have same extension for different
# mime types

replace this line and this will produce same results as mine.

usage:
just copy these py scripts to /usr/share/lighttpd
and change these lines if you're using debian based system
#### external configuration files
## mimetype mapping
#include_shell "/usr/share/lighttpd/create-mime.assign.pl"
include_shell "/usr/share/lighttpd/create-mime.assign.py"

## load enabled configuration files,
## read /etc/lighttpd/conf-available/README first
#include_shell "/usr/share/lighttpd/include-conf-enabled.pl"
include_shell "/usr/share/lighttpd/include-conf-enabled.py"