服务配置

我们推荐使用go.mod来管理项目依赖。

go.mod

https://github.com/gogf/gf-demos/blob/master/go.mod

  1. module github.com/gogf/gf-demos
  2. require github.com/gogf/gf latest
  3. go 1.12

其中注意module名称设置为github.com/gogf/gf-demos。这里我们只需要依赖GF框架即可。其中的go 1.12表示运行该项目所需的最低Go版本,这里也可以不设置。Goland会自动帮我们设置为当前使用的Go版本。

配置文件

GF框架的核心组件均实现了便捷的文件配置管理方式,包括Server、日志组件、数据库ORM、模板引擎等等,非常强大便捷。具体的配置项可以查看后续对应的章节介绍。

https://github.com/gogf/gf-demos/blob/master/config/config.example.toml

  1. # HTTP Server配置
  2. [server]
  3. Address = ":8199"
  4. ServerRoot = "public"
  5. ServerAgent = "gf-demos"
  6. LogPath = "/tmp/log/gf-demos/server"
  7. NameToUriType = 2
  8. RouteOverWrite = true
  9. # 全局日志配置
  10. [logger]
  11. Path = "/tmp/log/gf-demos"
  12. Level = "all"
  13. Stdout = true
  14. # 模板引擎配置
  15. [viewer]
  16. Path = "template"
  17. DefaultFile = "index.html"
  18. Delimiters = ["${", "}"]
  19. # 数据库连接
  20. [database]
  21. link = "mysql:root:12345678@tcp(127.0.0.1:3306)/test"
  22. debug = true
  23. # 数据库日志对象配置
  24. [database.logger]
  25. Path = "/tmp/log/gf-demos/sql"
  26. Level = "all"
  27. Stdout = true

启动设置

boot包中执行代码层级的初始化,比如一些组件模块的设置。

https://github.com/gogf/gf-demos/blob/master/boot/boot.go

  1. package boot
  2. // 用于应用初始化。
  3. func init() {
  4. // 添加代码层级的启动配置
  5. }

可以看到,我们的包初始化管理使用了包初始化方法init,这样做的好处是可以在boot目录中使用不同的go文件注册不同的init来分别实现不同的初始化配置管理,在业务比较复杂的项目中比较实用。