diff --git a/conf/zone.go b/conf/zone.go index c417d87..004bda3 100644 --- a/conf/zone.go +++ b/conf/zone.go @@ -8,12 +8,13 @@ type ZoneYaml struct { CssURL string `yaml:"cssURL"` HavePageContents bool `yaml:"havePageContents"` BasePath string `yaml:"basePath"` - UsernameProvided bool `yaml:"usernameProvided"` + UsernameProvided bool `yaml:"usernameProvided"` //If set, the outputter will do /{user}/{repo}/ for repos rather than /{repo}/ ; Should really be named usernameProvidedByRequest Username string `yaml:"username"` BasePrefixURL string `yaml:"basePrefixURL"` SuffixDirectoryURL string `yaml:"suffixDirectoryURL"` SuffixFileURL string `yaml:"suffixFileURL"` RangeSupported bool `yaml:"rangeSupported"` + PathLengthLimit uint `yaml:"pathLengthLimit"` //The length of the path (Number of entries in the path) to return in the responses; (If 0: defaults to 1, if the username is not expected to be provided by the request, otherwise defaulting to 2) CacheSettings CacheSettingsYaml `yaml:"cacheSettings"` } @@ -22,11 +23,20 @@ func (zy ZoneYaml) GetPackageMetaTagOutputter() *outputMeta.PackageMetaTagOutput if !zy.UsernameProvided { theUsername = zy.Username } + pthLength := zy.PathLengthLimit + if pthLength == 0 { + if zy.UsernameProvided { + pthLength = 2 + } else { + pthLength = 1 + } + } return &outputMeta.PackageMetaTagOutputter{ BasePath: zy.BasePath, Username: theUsername, BasePrefixURL: zy.BasePrefixURL, SuffixDirectoryURL: zy.SuffixDirectoryURL, SuffixFileURL: zy.SuffixFileURL, + PathLengthLimit: pthLength, } } diff --git a/config.example.yml b/config.example.yml index 8c9235e..d8db737 100644 --- a/config.example.yml +++ b/config.example.yml @@ -12,11 +12,12 @@ zones: #An array of zones havePageContents: true #Output a header and link to the target repo basePath: "localhost" #The base-path, also known as, package name basePrefixURL: "http://localhost" #The base git URL - usernameProvided: true #If the username would be provided in requests to the server (When false the value of username can be used) + usernameProvided: true #If the username is expected to be provided in requests to the server (When false the value of username can be used) username: "captain-alm" #The username to append to the start of a path under the prefix suffixDirectoryURL: "src/branch/master{/dir}" #The suffix location of the main branch for directory usage suffixFileURL: "src/branch/master{/dir}/{file}#L{line}" #The suffix location of the main branch for file usage rangeSupported: true #Are range requests supported + pathLengthLimit: 0 #The length of the returned paths in the responses (Number of path entries); (If 0: defaults to 1, if the username is not expected to be provided by the request, otherwise defaulting to 2) cacheSettings: #Cache settings maxAge: 0 #The maximum age of the cache notModifiedUsingLastModified: true #Are the conditional headers attached to Last-Modified used to work out if to send a 304 Cache Redirect diff --git a/outputMeta/package-meta-tag-outputter.go b/outputMeta/package-meta-tag-outputter.go index 5380b48..07a275e 100644 --- a/outputMeta/package-meta-tag-outputter.go +++ b/outputMeta/package-meta-tag-outputter.go @@ -11,6 +11,7 @@ type PackageMetaTagOutputter struct { BasePrefixURL string SuffixDirectoryURL string SuffixFileURL string + PathLengthLimit uint //The number of path entries in the go import paths } func (pkgMTO *PackageMetaTagOutputter) GetMetaTags(pathIn string) string { @@ -19,12 +20,27 @@ func (pkgMTO *PackageMetaTagOutputter) GetMetaTags(pathIn string) string { } func (pkgMTO *PackageMetaTagOutputter) GetMetaContentForGoImport(pathIn string) string { - return pkgMTO.getPrefix(pathIn) + " git " + pkgMTO.getHomeURL(pathIn) + pathLoc := pkgMTO.GetPath(pathIn) + return pkgMTO.getPrefix(pathLoc) + " git " + pkgMTO.getHomeURL(pathLoc) } func (pkgMTO *PackageMetaTagOutputter) GetMetaContentForGoSource(pathIn string) string { - return pkgMTO.getPrefix(pathIn) + " " + pkgMTO.getHomeURL(pathIn) + " " + - pkgMTO.getDirectoryURL(pathIn) + " " + pkgMTO.getFileURL(pathIn) + pathLoc := pkgMTO.GetPath(pathIn) + return pkgMTO.getPrefix(pathLoc) + " " + pkgMTO.getHomeURL(pathLoc) + " " + + pkgMTO.getDirectoryURL(pathLoc) + " " + pkgMTO.getFileURL(pathLoc) +} + +func (pkgMTO *PackageMetaTagOutputter) GetPath(pathIn string) string { + cleaned := path.Clean(pathIn) + if cleaned == "/" || cleaned == "." { + return cleaned + } + split := strings.Split(cleaned, "/") + toReturn := "" + for i := 1; i < len(split) && i < int(pkgMTO.PathLengthLimit)+1; i++ { + toReturn += split[i] + "/" + } + return toReturn[:len(toReturn)-1] } func (pkgMTO *PackageMetaTagOutputter) assureBasePrefixURL() (failed bool) { diff --git a/web/page-handler-template-marshal.go b/web/page-handler-template-marshal.go index bcde894..bc0b8af 100644 --- a/web/page-handler-template-marshal.go +++ b/web/page-handler-template-marshal.go @@ -20,8 +20,8 @@ func (htm handlerTemplateMarshal) GetGoSourceMetaContent() string { func (htm handlerTemplateMarshal) GetLink() string { if htm.PageHandler.MetaOutput.Username == "" { - return htm.PageHandler.MetaOutput.BasePrefixURL + "/" + strings.TrimLeft(path.Clean(htm.RequestPath), "/") + return htm.PageHandler.MetaOutput.BasePrefixURL + "/" + strings.TrimLeft(path.Clean(htm.PageHandler.MetaOutput.GetPath(htm.RequestPath)), "/") } else { - return htm.PageHandler.MetaOutput.BasePrefixURL + "/" + strings.TrimLeft(path.Join(htm.PageHandler.MetaOutput.Username, htm.RequestPath), "/") + return htm.PageHandler.MetaOutput.BasePrefixURL + "/" + strings.TrimLeft(path.Join(htm.PageHandler.MetaOutput.Username, htm.PageHandler.MetaOutput.GetPath(htm.RequestPath)), "/") } }