一直想要有一個本身的文檔管理系統:html
第一點用Makrdown來寫文檔是一個很是好的選擇,第二點天然想到了git,
第三點用一個靜態的網站來瀏覽和管理是一個不錯的選擇,這裏選擇了hugo。python
Hugo是由Go語言實現的靜態網站生成器。 注意是生成器。他雖然自帶webserver,可是沒有Nigix強大了。
他能很是方便的把markdown文件轉換爲html。git
首先必須有一臺服務器,我選擇了阿里雲。而後:github
這時候咱們就能夠看到第一個頁面了。web
利用git來管理文檔是一個很是好的方式。這裏直接想到用github來存儲文檔。gitbhu支持收到push請求的時候調用固定的地址http。
因此咱們能夠用這個來實現完美的功能。服務器
首先在github上配置請求:settings->Webhooksmarkdown
而後咱們要在外面的服務器上搭建一個Http服務器來接受這個請求, 這裏選擇用python的import http.server來搭建,簡單方便:socket
class EntranceHttpRequestHandler(http.server.CGIHTTPRequestHandler): def do_POST(self): print('begin') if __name__=='__main__': handler = EntranceHttpRequestHandler.EntranceHttpRequestHandler httpd = socketserver.TCPServer(("", 8001), handler) httpd.serve_forever()
import http.server import Convertor import os import _thread TargetPath = "/root/root/site/content/post/blog" GitSrcPath = "/root/root/site/blog" HugeSitePath = "/root/root/site/" HugeStatCommond = r'hugo server --buildDrafts -p 80 --bind 115.28.83.94 -b http://115.28.83.94/' class EntranceHttpRequestHandler(http.server.CGIHTTPRequestHandler): def do_POST(self): print('begin') self.gitpull(GitSrcPath) self.stopHugo() convert = Convertor.Convertor() convert.excute(GitSrcPath,TargetPath) self.startHugo() print("finished") self.wfile.write(b"msg finished") def gitpull(self, filePath): os.chdir(filePath) command = "git pull " os.system(command) def startHugo(self): _thread.start_new_thread(self.doStartHugo, ()) def doStartHugo(self): os.chdir(HugeSitePath) output = os.system(HugeStatCommond) print(output) print('sartHugo finished') def stopHugo(self): command = 'kill -9 $(pidof hugo)' os.system(command) print('stopHugo finished')
其中Convert 是對文檔作一些分類和tag的轉換不詳細介紹。
到如今一個完整的網站就搭建完成了,每一次只要在本地push文檔,就能在網站上自動更新。post
完整代碼看這裏網站