代码改变世界

Beego的使用

2021-07-25 22:40  宋海宾  阅读(569)  评论(0编辑  收藏  举报

 

1.Beego概述

官网帮助文档连接

https://beego.me/docs/quickstart/router.md

 

2.Beego的启动

启动Beego

import (
        _ "quickstart/routers"
        "github.com/beego/beego/v2/server/web"
)

func main() {
        web.Run()
}

添加控制器
package routers

import (
        "quickstart/controllers"
        "github.com/beego/beego/v2/server/web"
)

func init() {
   web.Router("/", &controllers.MainController{})
}

说明:
web.Router添加Uri相应的Controller对象
web.Run进行如下四个处理:
解析configuration file,即app.conf文件,其中监听端口的配置

2.1 配置文件

appname = beepkg httpaddr = "127.0.0.1" httpport = 9090 runmode ="dev" autorender = false recoverpanic = false viewspath = "myview"

These configurations will replace Beego’s default values.

Other application specific values can also be set using this file, such as database connection details:

mysqluser = "root"
mysqlpass = "rootpass"
mysqlurls = "127.0.0.1"
mysqldb   = "beego"

These configurations can be accessed like this:

beego.AppConfig.String("mysqluser")
beego.AppConfig.String("mysqlpass")
beego.AppConfig.String("mysqlurls")
beego.AppConfig.String("mysqldb")

AppConfig’s methods:

  • Set(key, val string) error
  • String(key string) string
  • Strings(key string) []string
  • Int(key string) (int, error)
  • Int64(key string) (int64, error)
  • Bool(key string) (bool, error)
  • Float(key string) (float64, error)
  • DefaultString(key string, defaultVal string) string
  • DefaultStrings(key string, defaultVal []string)
  • DefaultInt(key string, defaultVal int) int
  • DefaultInt64(key string, defaultVal int64) int64
  • DefaultBool(key string, defaultVal bool) bool
  • DefaultFloat(key string, defaultVal float64) float64
  • DIY(key string) (interface{}, error)
  • GetSection(section string) (map[string]string, error)
  • SaveConfigFile(filename string) error

When using the INI format the key supports the section::key pattern.

The Default* methods can be used to return default values if the config file cannot be read.