不可不說,靜態語言的源碼看起來確實比較方便,以前看python的源碼但是很累的。python
journey的入口函數在main.go
中,main()
函數作了如下幾件事:git
設置了GOMAXPROCS
爲CPU核數github
使用flag
讀取命令行配置sql
初始化數據庫database.Initialize()
,看下Initialize()
源碼:數據庫
func Initialize() error { // 若是journey.db不存在,查找Ghost數據庫並轉換它 if !helpers.FileExists(filenames.DatabaseFilename) { // Convert Ghost database if available (time format needs to change to be compatible with journey) migration.Ghost() } // 打開或者建立一個數據庫 var err error readDB, err = sql.Open("sqlite3", filenames.DatabaseFilename) if err != nil { return err } readDB.SetMaxIdleConns(256) // TODO: is this enough? err = readDB.Ping() if err != nil { return err } currentTime := time.Now() // 看下stmtInitialization語句可知,若是不存在相應的表纔會建立。 // 後面跟的參數用於填充stmtInitialization中的佔位符,佔位符是問號`?`。 _, err = readDB.Exec(stmtInitialization, uuid.Formatter(uuid.NewV4(), uuid.CleanHyphen), currentTime, currentTime, uuid.Formatter(uuid.NewV4(), uuid.CleanHyphen), currentTime, currentTime, uuid.Formatter(uuid.NewV4(), uuid.CleanHyphen), currentTime, currentTime, uuid.Formatter(uuid.NewV4(), uuid.CleanHyphen), currentTime, currentTime, uuid.Formatter(uuid.NewV4(), uuid.CleanHyphen), currentTime, currentTime, uuid.Formatter(uuid.NewV4(), uuid.CleanHyphen), currentTime, currentTime, uuid.Formatter(uuid.NewV4(), uuid.CleanHyphen), currentTime, currentTime, uuid.Formatter(uuid.NewV4(), uuid.CleanHyphen), currentTime, currentTime, uuid.Formatter(uuid.NewV4(), uuid.CleanHyphen), currentTime, currentTime, uuid.Formatter(uuid.NewV4(), uuid.CleanHyphen), currentTime, currentTime, uuid.Formatter(uuid.NewV4(), uuid.CleanHyphen), currentTime, currentTime, uuid.Formatter(uuid.NewV4(), uuid.CleanHyphen), currentTime, currentTime) // TODO: Is Commit()/Rollback() needed for DB.Exec()? if err != nil { return err } err = checkBlogSettings() if err != nil { return err } return nil }
生成博客首頁基本信息methods.GenerateBlog()
,生成的Blog
對象是全局變量,只會生成一次:json
func GenerateBlog() error { // 寫鎖定全局Blog變量 if Blog != nil { Blog.Lock() defer Blog.Unlock() } // 從數據庫讀取博客,database.RetrieveBlog()使用預先定義好的sql語句獲取內容,生成blog對象。 blog, err := database.RetrieveBlog() if err != nil { return err } // 添加數據庫中沒有保存的參數。 // 從配置文件中獲取博客的首頁鏈接。 blog.Url = []byte(configuration.Config.Url) //這個是幹嗎的? blog.AssetPath = assetPath // 建立導航欄的slugs,slug即頁面的惟一標識符,可用在url上。 for index, _ := range blog.NavigationItems { blog.NavigationItems[index].Slug = slug.Generate(blog.NavigationItems[index].Label, "navigation") } Blog = blog return nil }
模板編譯(不是渲染哦)templates.Generate()
,後面的文章會分析編譯流程。服務器
加載插件plugins.Load()
。plug的官方介紹:https://github.com/kabukky/journey/wiki/Creating-a-Journey-Plugin函數
從配置文件(config.json)讀取http/https監聽端口。ui
從配置文件讀取服務器啓動方式。有AdminOnly
,All
,default
三種,默認是default
,下面看下default
作了哪些事:this
httpRouter := httptreemux.New() // 註冊博客路由 server.InitializeBlog(httpRouter) server.InitializePages(httpRouter) // 註冊admin路由 server.InitializeAdmin(httpRouter) // 啓動HTTP server,它使用的服務go使用標準庫的http包。 log.Println("Starting server without HTTPS support. Please enable HTTPS in " + filenames.ConfigFilename + " to improve security.") log.Println("Starting http server on port " + httpPort + "...") err := http.ListenAndServe(httpPort, httpRouter) if err != nil { log.Fatal("Error: Couldn't start the HTTP server:", err)
路由的註冊流程將在後面的文章中介紹。