在Pi和Github上搭建自己的个人博客

方法如下:

本站同时托管在家里的Raspberry PiGithub Pages上,并同步保持更新,海外用户会访问Github,国内用户则会访问Pi,不同线路解析域名hugozhu.myalert.info到不同的服务器是通过DnsPod的服务实现的,这么好的服务还是免费的,这里推荐一下。

因为Github Pages只能支持静态网页,你需要一个能生成静态网页的博客生成引擎。我使用的是gor , 也可以使用ruhohGoogle一下还有很多

静态页面博客的好处:

  1. 性能是最好的,很合适用Raspberry Pi来做服务器,节省资源;
  2. 文章可以用Markdown格式来编写,采用Github来做版本控制,我的Blog仓库在 http://github.com/hugozhu/blog ,数据安全很好,误删除也不担心了;
  3. 很容易找到托管环境,方便迁移;
  4. 用Gor在Pi上生成速度很快;再用Nginx提供Web服务,可以直接在Pi上写Blog;
  5. 大繁至简

Github设置

  1. 在你的仓库里增加一个your_github_id.github.com,比如我的github ID是hugozhu,相应的仓库名就是hugozhu.github.com,这个仓库也就是网站的根目录了,在这里放生成好的静态文件
  2. 如果你需要用自己的域名,而不是Github提供的,可以在根目录下增加一个CNAME文件,文件内容则是你的域名,在DnsPod上需要建一个CNAME记录,将你的域名指向your_github_id.github.com. 也就是github原来分配给你的,完成这个设置后,访问your_github_id.github.com会跳转到你的域名;
  3. 每次更新后,Github会在10分钟内生效。

更新博客

  1. Gor的使用详细说明可见 https://github.com/wendal/gor
  2. 我的整个网站的内容也通过Github开源了: https://github.com/hugozhu/blog

以我的网站为例:

git clone https://github.com/hugozhu/blog
    Cloning into 'blog'...
    remote: Counting objects: 190, done.
    remote: Compressing objects: 100% (146/146), done.
    remote: Total 190 (delta 81), reused 132 (delta 23)
    Receiving objects: 100% (190/190), 155.48 KiB | 171 KiB/s, done.
    Resolving deltas: 100% (81/81), done.
cd blog
gor compile
    2013/02/27 13:17:19 gor.go:21: gor ver 2.1
    2013/02/27 13:17:19 payload.go:572: Load Layout : default
    2013/02/27 13:17:19 payload.go:572: Load Layout : page
    2013/02/27 13:17:19 payload.go:572: Load Layout : post
    2013/02/27 13:17:19 config.go:61: Look lile a Json, try it
    2013/02/27 13:17:19 config.go:64: It is Json Map
    2013/02/27 13:17:19 widgets.go:111: Load widget from  widgets/analytics/config.yml
    2013/02/27 13:17:19 widgets.go:111: Load widget from  widgets/comments/config.yml
    2013/02/27 13:17:19 widgets.go:111: Load widget from  widgets/google_prettify/config.yml
    2013/02/27 13:17:19 compile.go:125: Done
cd compiled
git init
git add -A 
git commit -m "update website" .
git remote add origin hugozhu@github.com:hugozhu/hugozhu.github.com
git push -u origin master

最后等待10分钟,再打开 http://hugozhu.github.com 就好了。。。

[Read More]

动态DNS程序

动态根据宽带public ip更新dnspod登记的域名

按照 https://gist.github.com/833369 逻辑重新用Go实现了,用更少的内存开销在Raspberry Pi上跑。

替换上你的Email,密码,域名ID,记录ID等参数,就可以运行了。 会在后台一直运行,每隔30秒检查一遍IP,如果修改了就更新IP。

获得domain_id可以用:

curl curl -k https://dnsapi.cn/Domain.List -d "login_email=xxx&login_password=xxx" 

获得record_id:

curl -k https://dnsapi.cn/Record.List -d "login_email=xxx&login_password=xxx&domain_id=xxx"

将Java的Properties文件转换成环境变量

Overview

在Java程序中使用properties文件很方便,但有时候需要和脚本配合使用时,需要把properties文件内的多个变量转换成环境变量,本文提供一个转换脚本示范:

比如env.properties如下(=附近可以有空格,也可以有空行)

MYSQL_URL = jdbc:mysql://localhost:3306/test?autoReconnect=true&useUnicode=true&characterEncoding=gbk
MYSQL_USER = root
MYSQL_PASS = 

执行下面的脚本后就相当于

export MYSQL_URL="//localhost:3306/test?autoReconnect=true&useUnicode=true&characterEncoding=gbk"
export MYSQL_USER="root"
export MYSQL_PASS="" 

env.sh脚本代码

#!/bin/bash

property_file=env.properties

get_prop(){
    propfile=$1
    key=$2
    grep  "^${2}=" ${1}| sed "s%${2}=\(.*\)%\1%"
}

trim() {
    trimmed=$1
    trimmed=${trimmed%% }
    trimmed=${trimmed## }
    echo "$trimmed"
}

`grep -v "^#" $property_file | sed -e '/^$/d' | while read line
do
    key=$(echo $line | awk -F "=" '{print $1}')
    trimmed_key=$(trim $key)
    trimmed_val=$(trim $(get_prop $property_file "$key")
    echo "export $trimmed_key=\"$trimmed_val\")"
done`