Ejemplo n.º 1
0
func loadRevel(routes []route) http.Handler {
	router := revel.NewRouter("")

	// parseRoutes
	var rs []*revel.Route
	for _, r := range routes {
		rs = append(rs, revel.NewRoute(r.method, r.path, "RevelController.Handle", "", "", 0))
	}
	router.Routes = rs

	// updateTree
	router.Tree = pathtree.New()
	for _, r := range router.Routes {
		err := router.Tree.Add(r.TreePath, r)
		// Allow GETs to respond to HEAD requests.
		if err == nil && r.Method == "GET" {
			err = router.Tree.Add("/HEAD"+r.Path, r)
		}
		// Error adding a route to the pathtree.
		if err != nil {
			panic(err)
		}
	}

	rc := new(RevelController)
	rc.router = router
	return rc
}
Ejemplo n.º 2
0
func loadRevelSingle(method, path, action string) http.Handler {
	router := revel.NewRouter("")

	route := revel.NewRoute(method, path, action, "", "", 0)
	if err := router.Tree.Add(route.TreePath, route); err != nil {
		panic(err)
	}

	rc := new(RevelController)
	rc.router = router
	return rc
}
Ejemplo n.º 3
0
// init creates routes based on swagger specs defined in the conf/swagger folder
// it will create asset endpoints at /@basePath for serving the swagger-spec and UI
// which can be overridden with swaggerapi.add-ui = false
// TODO look into adding a Giles
func init() {
	revel.OnAppStart(func() {
		// TODO find out why AppRoot is empty
		//doc, err := spec.Load(filepath.Join(revel.AppRoot, "conf", "swagger", "swagger.yml"))
		//fmt.Println(revel.AppRoot, filepath.Join(revel.AppRoot, "conf", "swagger", "swagger.yml"))

		// grab the old routes to append
		oldRoutes := revel.MainRouter.Routes
		// new and emtpy router
		// NOTE WARNING calling MainRouter.Refresh will destroy the swagger routes since they do not have a routes file
		revel.MainRouter = revel.NewRouter(filepath.Join(revel.BasePath, "conf", "routes"))

		configs := revel.Config.StringDefault("swaggerapi.specs", "")
		for _, config := range strings.Split(configs, ",") {
			found := false
			for _, path := range revel.ConfPaths {
				doc, err := spec.Load(filepath.Join(path, config))
				if os.IsNotExist(err) {
					continue
				}
				if err != nil {
					_fail(err)
				}

				// TODO decide if multiple of same name is allowed or not
				// if found { }

				if revel.Config.BoolDefault("swaggerapi.add-ui", true) {
					AddSwaggerUi(doc.BasePath(), config)
					// conncurently download or unzip the swagger ui assets
					// only runs once
					go common.UnzipSwaggerAssets()
				}

				AddSwaggerRoutes(doc)
				found = true
			}
			if !found {
				_fail(fmt.Errorf("Swagger configuration '%s' not found found in any of these folders:\t%v",
					config, configs))
			}
		}

		// Add oldRoutes back into the router
		for _, route := range oldRoutes {
			_fail(revel.MainRouter.Tree.Add(route.TreePath, route))
			revel.MainRouter.Routes = append(revel.MainRouter.Routes, route)
		}
	})
}