drone/internal/router/web.go
Johannes Batzill 1115a5083b Add Paths support and error improvements (#11)
This change is adding the concept of Paths.
A repository and space always have a Primary Path which always is represents the ancestry to the root space.
All access history / resource visibility / child listings / UI traversal / etc. is done via that path.

Additionally, repos and spaces can have Alias Paths, which as the name states are aliases. via the primary path.
They sole impact is that a space or repo can be reached via different paths from the UI / rest apis / git apis.
This fulfills two major purposes:
- Customers can rename or move projects and spaces without breaking any existing references from CI pipeliens / code bases / local repos / ...
- Customer can create shorter aliases for important repos when in harness embeded mode! (acc/org/proj/repo can be shortened to acc/repo, or acc/repo'

Apart from the path changes, this PR adds:

Improved User facing errors
Improved internal error handling and wrapping
update / rename operation for repo and space
path list / delete / create operation for repo and space
2022-09-08 21:39:15 -07:00

67 lines
2.2 KiB
Go

package router
import (
"net/http"
"github.com/harness/gitness/internal/api/middleware/encode"
"github.com/harness/gitness/internal/store"
"github.com/harness/gitness/web"
"github.com/swaggest/swgui/v3emb"
"github.com/unrolled/secure"
"github.com/go-chi/chi"
)
/*
* Mounts the WEB Router under mountPath.
* The handler is wrapped within a layer that handles encoding Paths.
*/
func newWebHandler(
mountPath string,
systemStore store.SystemStore) (http.Handler, error) {
config := systemStore.Config(nocontext)
// Use go-chi router for inner routing (restricted to mountPath!)
r := chi.NewRouter()
r.Route(mountPath, func(r chi.Router) {
// create middleware to enforce security best practices for
// the user interface. note that theis middleware is only used
// when serving the user interface (not found handler, below).
sec := secure.New(
secure.Options{
AllowedHosts: config.Secure.AllowedHosts,
HostsProxyHeaders: config.Secure.HostsProxyHeaders,
SSLRedirect: config.Secure.SSLRedirect,
SSLTemporaryRedirect: config.Secure.SSLTemporaryRedirect,
SSLHost: config.Secure.SSLHost,
SSLProxyHeaders: config.Secure.SSLProxyHeaders,
STSSeconds: config.Secure.STSSeconds,
STSIncludeSubdomains: config.Secure.STSIncludeSubdomains,
STSPreload: config.Secure.STSPreload,
ForceSTSHeader: config.Secure.ForceSTSHeader,
FrameDeny: config.Secure.FrameDeny,
ContentTypeNosniff: config.Secure.ContentTypeNosniff,
BrowserXssFilter: config.Secure.BrowserXSSFilter,
ContentSecurityPolicy: config.Secure.ContentSecurityPolicy,
ReferrerPolicy: config.Secure.ReferrerPolicy,
},
)
// openapi playground endpoints
swagger := v3emb.NewHandler("API Definition", "/api/v1/swagger.yaml", "/swagger")
r.With(sec.Handler).Handle("/swagger", swagger)
r.With(sec.Handler).Handle("/swagger/*", swagger)
// serve all other routes from the embedded filesystem,
// which in turn serves the user interface.
r.With(sec.Handler).NotFound(
web.Handler(),
)
})
// web doesn't have any prefixes for terminated paths
return encode.TerminatedPathBefore([]string{""}, r.ServeHTTP), nil
}