Exemple #1
0
func RunRestApi(app gof.App, port int) {
	fmt.Println("[ Go2o][ API][ Booted] - Api server running on port " + strconv.Itoa(port))

	//socket client
	time.Sleep(time.Second * 2) //等待启动Socket
	API_DOMAIN = app.Config().GetString(variable.ApiDomain)
	goclient.Configure("tcp", app.Config().GetString(variable.ClientSocketServer), app)

	var in *web.Interceptor = web.NewInterceptor(app, func(ctx *web.Context) {
		host := ctx.Request.URL.Host
		// todo: path compare
		if API_HOST_CHK && host != API_DOMAIN {
			http.Error(ctx.Response, "no such file", http.StatusNotFound)
			return
		}
		api.Handle(ctx)
	})

	//启动服务
	err := http.ListenAndServe(":"+strconv.Itoa(port), in)

	if err != nil {
		app.Log().Fatalln("ListenAndServer ", err)
	}
}
Exemple #2
0
//获取Http请求代理处理程序
func getInterceptor(a gof.App) *web.Interceptor {
	var igor = web.NewInterceptor(a, getHttpExecFunc())
	igor.Except = web.HandleDefaultHttpExcept
	igor.Before = func(ctx *web.Context) bool {

		r := ctx.Request
		//静态资源
		if strings.HasPrefix(r.Host, "static.") {
			http.ServeFile(ctx.Response, r, "./static"+r.URL.Path)
			return false
		} else if strings.HasPrefix(r.Host, "img.") {
			http.ServeFile(ctx.Response, r, "./static/uploads/"+r.URL.Path)
			return false
		}
		return true
	}
	igor.After = nil
	return igor
}