Ejemplo n.º 1
0
// Load loads the router
func Load(middleware ...gin.HandlerFunc) http.Handler {

	e := gin.New()
	e.Use(gin.Recovery())

	e.SetHTMLTemplate(template.Load())

	fs := http.FileServer(dist.AssetFS())
	e.GET("/static/*filepath", func(c *gin.Context) {
		fs.ServeHTTP(c.Writer, c.Request)
	})

	e.Use(header.NoCache)
	e.Use(header.Options)
	e.Use(header.Secure)
	e.Use(middleware...)
	e.Use(session.SetUser())
	e.Use(token.Refresh)

	e.GET("/login", server.ShowLogin)
	e.GET("/login/form", server.ShowLoginForm)
	e.GET("/logout", server.GetLogout)
	e.NoRoute(server.ShowIndex)

	// TODO above will Go away with React UI

	user := e.Group("/api/user")
	{
		user.Use(session.MustUser())
		user.GET("", server.GetSelf)
		user.GET("/feed", server.GetFeed)
		user.GET("/repos", server.GetRepos)
		user.GET("/repos/remote", server.GetRemoteRepos)
		user.POST("/token", server.PostToken)
		user.DELETE("/token", server.DeleteToken)
	}

	users := e.Group("/api/users")
	{
		users.Use(session.MustAdmin())
		users.GET("", server.GetUsers)
		users.POST("", server.PostUser)
		users.GET("/:login", server.GetUser)
		users.PATCH("/:login", server.PatchUser)
		users.DELETE("/:login", server.DeleteUser)
	}

	teams := e.Group("/api/teams")
	{
		teams.Use(session.MustTeamAdmin())

		team := teams.Group("/:team")
		{
			team.GET("/secrets", server.GetTeamSecrets)
			team.POST("/secrets", server.PostTeamSecret)
			team.DELETE("/secrets/:secret", server.DeleteTeamSecret)
		}
	}

	global := e.Group("/api/global")
	{
		global.Use(session.MustAdmin())

		global.GET("/secrets", server.GetGlobalSecrets)
		global.POST("/secrets", server.PostGlobalSecret)
		global.DELETE("/secrets/:secret", server.DeleteGlobalSecret)
	}

	repos := e.Group("/api/repos/:owner/:name")
	{
		repos.POST("", server.PostRepo)

		repo := repos.Group("")
		{
			repo.Use(session.SetRepo())
			repo.Use(session.SetPerm())
			repo.Use(session.MustPull)

			repo.GET("", server.GetRepo)
			repo.GET("/builds", server.GetBuilds)
			repo.GET("/builds/:number", server.GetBuild)
			repo.GET("/logs/:number/:job", server.GetBuildLogs)
			repo.POST("/sign", session.MustPush, server.Sign)

			repo.GET("/secrets", session.MustPush, server.GetSecrets)
			repo.POST("/secrets", session.MustPush, server.PostSecret)
			repo.DELETE("/secrets/:secret", session.MustPush, server.DeleteSecret)

			// requires push permissions
			repo.PATCH("", session.MustPush, server.PatchRepo)
			repo.DELETE("", session.MustRepoAdmin(), server.DeleteRepo)
			repo.POST("/chown", session.MustRepoAdmin(), server.ChownRepo)

			repo.POST("/builds/:number", session.MustPush, server.PostBuild)
			repo.DELETE("/builds/:number/:job", session.MustPush, server.DeleteBuild)
		}
	}

	badges := e.Group("/api/badges/:owner/:name")
	{
		badges.GET("/status.svg", server.GetBadge)
		badges.GET("/cc.xml", server.GetCC)
	}

	e.POST("/hook", server.PostHook)
	e.POST("/api/hook", server.PostHook)

	ws := e.Group("/ws")
	{
		ws.GET("/broker", server.Broker)
		ws.GET("/feed", server.EventStream)
		ws.GET("/logs/:owner/:name/:build/:number",
			session.SetRepo(),
			session.SetPerm(),
			session.MustPull,
			server.LogStream,
		)
	}

	auth := e.Group("/authorize")
	{
		auth.GET("", server.GetLogin)
		auth.POST("", server.GetLogin)
		auth.POST("/token", server.GetLoginToken)
	}

	builds := e.Group("/api/builds")
	{
		builds.Use(session.MustAdmin())
		builds.GET("", server.GetBuildQueue)
	}

	agents := e.Group("/api/agents")
	{
		agents.Use(session.MustAdmin())
		agents.GET("", server.GetAgents)
	}

	// DELETE THESE
	// gitlab := e.Group("/gitlab/:owner/:name")
	// {
	// 	gitlab.Use(session.SetRepo())
	// 	gitlab.GET("/commits/:sha", GetCommit)
	// 	gitlab.GET("/pulls/:number", GetPullRequest)
	//
	// 	redirects := gitlab.Group("/redirect")
	// 	{
	// 		redirects.GET("/commits/:sha", RedirectSha)
	// 		redirects.GET("/pulls/:number", RedirectPullRequest)
	// 	}
	// }

	// bots := e.Group("/bots")
	// {
	// 	bots.Use(session.MustUser())
	// 	bots.POST("/slack", Slack)
	// 	bots.POST("/slack/:command", Slack)
	// }

	return e
}
Ejemplo n.º 2
0
func Load(middleware ...gin.HandlerFunc) http.Handler {

	e := gin.New()
	e.Use(gin.Recovery())

	e.SetHTMLTemplate(template.Load())
	e.StaticFS("/static", static.FileSystem())

	e.Use(header.NoCache)
	e.Use(header.Options)
	e.Use(header.Secure)
	e.Use(middleware...)
	e.Use(session.SetUser())
	e.Use(token.Refresh)

	e.GET("/", server.ShowIndex)
	e.GET("/repos", server.ShowAllRepos)
	e.GET("/login", server.ShowLogin)
	e.GET("/login/form", server.ShowLoginForm)
	e.GET("/logout", server.GetLogout)

	// TODO below will Go away with React UI
	settings := e.Group("/settings")
	{
		settings.Use(session.MustUser())
		settings.GET("/profile", server.ShowUser)
	}
	repo := e.Group("/repos/:owner/:name")
	{
		repo.Use(session.SetRepo())
		repo.Use(session.SetPerm())
		repo.Use(session.MustPull)

		repo.GET("", server.ShowRepo)
		repo.GET("/builds/:number", server.ShowBuild)
		repo.GET("/builds/:number/:job", server.ShowBuild)

		repo_settings := repo.Group("/settings")
		{
			repo_settings.GET("", session.MustPush, server.ShowRepoConf)
			repo_settings.GET("/encrypt", session.MustPush, server.ShowRepoEncrypt)
			repo_settings.GET("/badges", server.ShowRepoBadges)
		}
	}
	// TODO above will Go away with React UI

	user := e.Group("/api/user")
	{
		user.Use(session.MustUser())
		user.GET("", server.GetSelf)
		user.GET("/feed", server.GetFeed)
		user.GET("/repos", server.GetRepos)
		user.GET("/repos/remote", server.GetRemoteRepos)
		user.POST("/token", server.PostToken)
		user.DELETE("/token", server.DeleteToken)
	}

	users := e.Group("/api/users")
	{
		users.Use(session.MustAdmin())
		users.GET("", server.GetUsers)
		users.POST("", server.PostUser)
		users.GET("/:login", server.GetUser)
		users.PATCH("/:login", server.PatchUser)
		users.DELETE("/:login", server.DeleteUser)
	}

	repos := e.Group("/api/repos/:owner/:name")
	{
		repos.POST("", server.PostRepo)

		repo := repos.Group("")
		{
			repo.Use(session.SetRepo())
			repo.Use(session.SetPerm())
			repo.Use(session.MustPull)

			repo.GET("", server.GetRepo)
			repo.GET("/builds", server.GetBuilds)
			repo.GET("/builds/:number", server.GetBuild)
			repo.GET("/logs/:number/:job", server.GetBuildLogs)
			repo.POST("/sign", session.MustPush, server.Sign)

			repo.POST("/secrets", session.MustPush, server.PostSecret)
			repo.DELETE("/secrets/:secret", session.MustPush, server.DeleteSecret)

			// requires push permissions
			repo.PATCH("", session.MustPush, server.PatchRepo)
			repo.DELETE("", session.MustPush, server.DeleteRepo)
			repo.POST("/chown", session.MustPush, server.ChownRepo)

			repo.POST("/builds/:number", session.MustPush, server.PostBuild)
			repo.DELETE("/builds/:number/:job", session.MustPush, server.DeleteBuild)
		}
	}

	badges := e.Group("/api/badges/:owner/:name")
	{
		badges.GET("/status.svg", server.GetBadge)
		badges.GET("/cc.xml", server.GetCC)
	}

	e.POST("/hook", server.PostHook)
	e.POST("/api/hook", server.PostHook)

	stream := e.Group("/api/stream")
	{
		stream.Use(session.SetRepo())
		stream.Use(session.SetPerm())
		stream.Use(session.MustPull)

		stream.GET("/:owner/:name", server.GetRepoEvents)
		stream.GET("/:owner/:name/:build/:number", server.GetStream)
	}

	auth := e.Group("/authorize")
	{
		auth.GET("", server.GetLogin)
		auth.POST("", server.GetLogin)
		auth.POST("/token", server.GetLoginToken)
	}

	builds := e.Group("/api/builds")
	{
		builds.Use(session.MustAdmin())
		builds.GET("", server.GetBuildQueue)
	}

	agents := e.Group("/api/agents")
	{
		agents.Use(session.MustAdmin())
		agents.GET("", server.GetAgents)
	}

	queue := e.Group("/api/queue")
	{
		queue.Use(session.AuthorizeAgent)
		queue.POST("/pull", server.Pull)
		queue.POST("/pull/:os/:arch", server.Pull)
		queue.POST("/wait/:id", server.Wait)
		queue.POST("/stream/:id", server.Stream)
		queue.POST("/status/:id", server.Update)
		queue.POST("/ping", server.Ping)

		queue.POST("/logs/:id", server.PostLogs)
		queue.GET("/logs/:id", server.WriteLogs)
	}

	// DELETE THESE
	// gitlab := e.Group("/gitlab/:owner/:name")
	// {
	// 	gitlab.Use(session.SetRepo())
	// 	gitlab.GET("/commits/:sha", GetCommit)
	// 	gitlab.GET("/pulls/:number", GetPullRequest)
	//
	// 	redirects := gitlab.Group("/redirect")
	// 	{
	// 		redirects.GET("/commits/:sha", RedirectSha)
	// 		redirects.GET("/pulls/:number", RedirectPullRequest)
	// 	}
	// }

	// bots := e.Group("/bots")
	// {
	// 	bots.Use(session.MustUser())
	// 	bots.POST("/slack", Slack)
	// 	bots.POST("/slack/:command", Slack)
	// }

	return normalize(e)
}
Ejemplo n.º 3
0
func Load(middleware ...gin.HandlerFunc) http.Handler {
	e := gin.Default()
	e.SetHTMLTemplate(template.Load())
	e.StaticFS("/static", static.FileSystem())

	e.Use(location.Resolve)
	e.Use(header.NoCache)
	e.Use(header.Options)
	e.Use(header.Secure)
	e.Use(middleware...)
	e.Use(session.SetUser())
	e.Use(token.Refresh)

	e.GET("/", controller.ShowIndex)
	e.GET("/login", controller.ShowLogin)
	e.GET("/login/form", controller.ShowLoginForm)
	e.GET("/logout", controller.GetLogout)

	settings := e.Group("/settings")
	{
		settings.Use(session.MustUser())
		settings.GET("/profile", controller.ShowUser)
		settings.GET("/people", session.MustAdmin(), controller.ShowUsers)
		settings.GET("/nodes", session.MustAdmin(), controller.ShowNodes)
	}
	repo := e.Group("/repos/:owner/:name")
	{
		repo.Use(session.SetRepo())
		repo.Use(session.SetPerm())
		repo.Use(session.MustPull)

		repo.GET("", controller.ShowRepo)
		repo.GET("/builds/:number", controller.ShowBuild)
		repo.GET("/builds/:number/:job", controller.ShowBuild)
		repo_settings := repo.Group("/settings")
		{
			repo_settings.GET("", session.MustPush, controller.ShowRepoConf)
			repo_settings.GET("/encrypt", session.MustPush, controller.ShowRepoEncrypt)
			repo_settings.GET("/badges", controller.ShowRepoBadges)
		}
	}

	user := e.Group("/api/user")
	{
		user.Use(session.MustUser())
		user.GET("", controller.GetSelf)
		user.GET("/feed", controller.GetFeed)
		user.GET("/repos", controller.GetRepos)
		user.GET("/repos/remote", controller.GetRemoteRepos)
		user.POST("/token", controller.PostToken)
	}

	users := e.Group("/api/users")
	{
		users.Use(session.MustAdmin())
		users.GET("", controller.GetUsers)
		users.POST("", controller.PostUser)
		users.GET("/:login", controller.GetUser)
		users.PATCH("/:login", controller.PatchUser)
		users.DELETE("/:login", controller.DeleteUser)
	}

	nodes := e.Group("/api/nodes")
	{
		nodes.Use(session.MustAdmin())
		nodes.GET("", controller.GetNodes)
		nodes.POST("", controller.PostNode)
		nodes.DELETE("/:node", controller.DeleteNode)
	}

	repos := e.Group("/api/repos/:owner/:name")
	{
		repos.POST("", controller.PostRepo)

		repo := repos.Group("")
		{
			repo.Use(session.SetRepo())
			repo.Use(session.SetPerm())
			repo.Use(session.MustPull)

			repo.GET("", controller.GetRepo)
			repo.GET("/key", controller.GetRepoKey)
			repo.POST("/key", controller.PostRepoKey)
			repo.GET("/builds", controller.GetBuilds)
			repo.GET("/builds/:number", controller.GetBuild)
			repo.GET("/logs/:number/:job", controller.GetBuildLogs)

			// requires authenticated user
			repo.POST("/encrypt", session.MustUser(), controller.PostSecure)

			// requires push permissions
			repo.PATCH("", session.MustPush, controller.PatchRepo)
			repo.DELETE("", session.MustPush, controller.DeleteRepo)

			repo.POST("/builds/:number", session.MustPush, controller.PostBuild)
			repo.DELETE("/builds/:number/:job", session.MustPush, controller.DeleteBuild)
		}
	}

	badges := e.Group("/api/badges/:owner/:name")
	{
		badges.GET("/status.svg", controller.GetBadge)
		badges.GET("/cc.xml", controller.GetCC)
	}

	e.POST("/hook", controller.PostHook)
	e.POST("/api/hook", controller.PostHook)

	stream := e.Group("/api/stream")
	{
		stream.Use(session.SetRepo())
		stream.Use(session.SetPerm())
		stream.Use(session.MustPull)
		stream.GET("/:owner/:name", controller.GetRepoEvents)
		stream.GET("/:owner/:name/:build/:number", controller.GetStream)
	}

	auth := e.Group("/authorize")
	{
		auth.GET("", controller.GetLogin)
		auth.POST("", controller.GetLogin)
		auth.POST("/token", controller.GetLoginToken)
	}

	gitlab := e.Group("/gitlab/:owner/:name")
	{
		gitlab.Use(session.SetRepo())
		gitlab.GET("/commits/:sha", controller.GetCommit)
		gitlab.GET("/pulls/:number", controller.GetPullRequest)

		redirects := gitlab.Group("/redirect")
		{
			redirects.GET("/commits/:sha", controller.RedirectSha)
			redirects.GET("/pulls/:number", controller.RedirectPullRequest)
		}
	}

	return normalize(e)
}