Journey源码分析二:整体启动流程

不可不说,静态语言的源码看起来确实比较方便,之前看python的源码可是很累的。

journey的入口函数在main.go中,main()函数做了以下几件事:

  1. 设置了GOMAXPROCS为CPU核数

  2. 使用flag读取命令行配置

  3. 初始化数据库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
    	}
    
    
  4. 生成博客首页基本信息methods.GenerateBlog(),生成的Blog对象是全局变量,只会生成一次:

    	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
    	}
    
  5. 模板编译(不是渲染哦)templates.Generate(),后面的文章会分析编译流程。

  6. 加载插件plugins.Load()。plug的官方介绍:https://github.com/kabukky/journey/wiki/Creating-a-Journey-Plugin

  7. 从配置文件(config.json)读取http/https监听端口。

  8. 从配置文件读取服务器启动方式。有AdminOnly,All,default三种,默认是default,下面看下default做了哪些事:

    	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)
    

    路由的注册流程将在后面的文章中介绍。

转载于:https://my.oschina.net/fmnisme/blog/502485

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值