代码拉取完成,页面将自动刷新
package main
import (
"io/ioutil"
"log"
"os"
)
func main() {
app := application{}
//初始化
app.init()
//启动
app.start()
}
type application struct {
config *Config
server []*HttpServer
dispatch *DispatchManager
}
func (this *application) init() {
log.Println("myway-mock service begin start")
//加载配置文件
this.config = new(Config)
this.config.LoadFromYaml("configs.yaml")
//初始化api
this.dispatch = new(DispatchManager)
this.dispatch.Init()
//加载apis目录下的api定义
this.loadApis(string(os.PathSeparator), "apis")
}
func (this *application) loadApis(pathSeparator string, fileDir string) {
files, _ := ioutil.ReadDir(fileDir)
for _, onefile := range files {
filename := fileDir + pathSeparator + onefile.Name()
if onefile.IsDir() {
this.loadApis(pathSeparator, filename)
} else {
api := new(Api)
api.LoadFromYaml(filename)
if api.Url != "" {
log.Println("load the define of api:", api.Name)
this.dispatch.AddApi("", "datas", api)
}
}
}
}
/**
启动应用
默认端口 80
持多个端口监听
*/
func (this *application) start() {
default_port := 80
if this.config.Port != nil && len(this.config.Port) > 0 {
default_port = this.config.Port[0]
}
if this.config.Maxdelay != 0 {
maxRandomDelay = this.config.Maxdelay
}
//多个端口时候,对第一个以为的端口使用协程启动
if len(this.config.Port) > 1 {
//启动服务
for index, port := range this.config.Port {
if index == 0 {
continue
}
log.Println("start server in port: ", port)
go this.runServer(port)
}
log.Println("start server in port: ", default_port)
this.runServer(default_port)
}
}
//启动端口监听服务,提供http服务
func (this *application) runServer(port int) {
server := new(HttpServer)
server.dispatch = this.dispatch
server.port = port
this.server = append(this.server, server)
server.Start()
}
func (this *application) shutdown() {
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。