Example #1
0
func init() {
	revel.OnAppStart(func() {
		go common.UnzipSwaggerAssets()
		// build IndexArgs for rendering index template

		// collect all of the swagger-endpoints then build the spec
		routes := make([]*revel.Route, 0)
		for _, route := range revel.MainRouter.Routes {
			if strings.ToLower(route.Action) == "swaggify.spec" {
				routes = append(routes, route)
			}
		}

		for _, route := range routes {
			// TODO test what cases cause bounds panic
			// Don't duplicate building API specs
			if _, exists := APIs[route.FixedParams[0]]; exists {
				continue
			}

			APIs[route.FixedParams[0]] = newSpec(route.FixedParams[0])
			fmt.Println(APIs[route.FixedParams[0]])
		}
	})
}
Example #2
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)
		}
	})
}