golang web框架 配置文件读取 借鉴 beego
2017-03-31 17:37
627 查看
package gobbs import ( "encoding/xml" "io/ioutil" "log" "os" "path/filepath" "github.com/liangguangchuan/gobbs/lib" ) var ( //基础配置文件 BConf *Conf //项目访问路径 AppPath string //运行模式 dev prod RunMode string ) type Conf struct { Host string `xml:"server_host"` Port int64 `xml:"server_port"` AppName string `xml:"app_name"` RunMode string `xml:"run_mode"` } func init() { //创建 Conf BConf = newConf() var err error //获取当前运行的 路径 如果获取失败抛出错误 if AppPath, err = filepath.Abs(filepath.Dir(os.Args[0])); err != nil { panic(err) } //获取工作目录 workPath, err := os.Getwd() if err != nil { panic(err) } //拼接 conf 路径 confPath := filepath.Join(workPath, "conf", "conf.xml") //如果项目目录拼接conf/conf.xml 不存在对应文件 if !lib.FileExists(confPath) { confPath = filepath.Join(AppPath, "conf", "conf.xml") // 根据运行文件目录拼接conf/conf.xml 不存在对应文件 if !lib.FileExists(confPath) { return } } //读取文件并赋值 conf if err = parseConfig(confPath); err != nil { panic(nil) } //输出 最终构造体值 log.Fatal(BConf) } func newConf() *Conf { return &Conf{ Host: "127.0.0.1", Port: 8080, AppName: "xiaochuan", RunMode: DEV, } } //解析 conf.xml func parseConfig(confPath string) error { fileData, err := ioutil.ReadFile(confPath) if err != nil { return err } err = xml.Unmarshal(fileData, BConf) return err }
相关文章推荐
- 编写GO的WEB开发框架 (十三): 配置文件读取
- Golang Web 框架 Beego 静态文件处理 - 07
- Web.config文件中节点的配置数值读取的2种方法,哪种更快?
- sliverlight读取配置文件web.config的问题
- Java中框架的概念,以及从配置文件中读取配置信息并加载指定类
- Spring Junit 读取WEB-INF下的配置文件
- 深入浅出学习Struts1框架(七):ActionServlet实例化之读取struts-config.xml配置文件
- WEB应用中读取配置文件
- Silverlight读取Web.config配置文件
- Java中框架的概念,以及从配置文件中读取配置信息并加载指定类
- spring mvc框架 web.xml配置文件 Could not open ServletContext resource [/WEB-INF/classes/spring-servlet.xml] 错误
- java读取配置文件(web项目和java项目都可以)
- Web读取配置文件得到数据库连接(转JavaEye 青春飞扬)
- tomcat web 工程下配置文件读取的问题
- web容器读取配置文件过程中的一个问题
- Web应用中读取配置文件
- Spring Junit 读取WEB-INF下的配置文件
- web项目 读取src下的配置文件
- Portal-Basic Web 应用开发框架:应用篇(一) —— 配置文件
- Java 读取配置文件Web方案