Ejemplo n.º 1
0
func (p *program) Start(srv service.Service) error {
	p.router = httpway.New()

	c := GetConfig().WebServerConfig()
	if c.Enable_Access_Log {
		accessLogger := golog.GetLogger("access").Info
		p.router = p.router.Middleware(httpwaymid.AccessLog(accessLogger))
	}

	if golog.ToLogLevel(c.Log_Level) != golog.DEBUG {
		p.router = p.router.Middleware(httpwaymid.PanicCatcher)
	}

	p.server = p.initWebServer()

	p.router.Logger = golog.GetLogger("general")
	p.router.SessionManager = httpwaymid.NewSessionManager(c.Session_Timeout, c.Session_Expiration, golog.GetLogger("general"))

	if err := p.bootstrap(p.server, golog.GetLogger("general"), p.router); err != nil {
		return err
	}

	p.router = p.router.Middleware(httpwaymid.JSONRenderer("jsonData", "statusCode"))

	if c.Template_Dir != "" {
		p.router = p.router.Middleware(httpwaymid.TemplateRenderer(c.Template_Dir, "templateName", "templateData", "statusCode"))
	}

	p.router.NotFound = httpwaymid.NotFound(p.router)
	p.router.MethodNotAllowed = httpwaymid.MethodNotAllowed(p.router)

	middlewares := p.middlewareFactory(p.router)

	for middlewareName, routeFactory := range p.routeFactory {
		middleware, ok := middlewares[middlewareName]
		if !ok {
			panic("No middleware chain registered for " + middlewareName)
		}

		routeFactory(middleware)
	}

	if err := p.server.Start(); err != nil {
		return err
	}

	return nil
}
Ejemplo n.º 2
0
func main() {
	router := httpway.New()

	public := router.Middleware(AccessLogger)
	private := public.Middleware(AuthCheck)

	public.GET("/public", testHandler("public"))

	private.GET("/private", testHandler("private"))
	private.GET("/stop", stopServer)

	server = httpway.NewServer(nil)
	server.Addr = ":8080"
	server.Handler = router

	if err := server.Start(); err != nil {
		fmt.Println("Error", err)
		return
	}

	if err := server.WaitStop(10 * time.Second); err != nil {
		fmt.Println("Error", err)
	}
}