mirror of https://github.com/perkeep/perkeep.git
website: derive godoc package domain from serving dir
this is a straight revert of 02938281
plus the additional logic to
derive the domain name from the serving directory. This allows package
docs to also be served from localhost during development.
Updates #1010
Change-Id: I07f817edb0b83b03e363373945d143ed3cb82fbf
This commit is contained in:
parent
fafbd14415
commit
101ce48bdc
|
@ -318,9 +318,9 @@ func (root *Directory) listing(skipRoot bool) *DirList {
|
|||
p.Depth = d.Depth - minDepth
|
||||
p.Height = maxHeight - p.Depth
|
||||
// the suffix is absolute, with the camlistore dir as the root
|
||||
domainName, idx, err := domainInPath(d.Path)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
idx := strings.LastIndex(d.Path, domainName)
|
||||
if idx == -1 {
|
||||
log.Fatalf("No \"%s\" in path to file %s", domainName, d.Path)
|
||||
}
|
||||
suffix := pathpkg.Clean(d.Path[idx+len(domainName):])
|
||||
|
||||
|
|
|
@ -49,16 +49,22 @@ const (
|
|||
)
|
||||
|
||||
var (
|
||||
docRx = regexp.MustCompile(`^/((?:pkg|cmd|app)/([\w/]+?)(\.go)??)/?$`)
|
||||
// TODO(mpl): in retrospect, I don't think both domains are needed since we're
|
||||
// supposed to redirect everything to perkeep.org before we get to that point. So
|
||||
// just replacing camlistore.org with perkeep.org as the single domain in that file
|
||||
// should work. But better safe than sorry for now.
|
||||
domainNames = []string{"perkeep.org", "camlistore.org"}
|
||||
domainName string
|
||||
docRx = regexp.MustCompile(`^/((?:pkg|cmd|app)/([\w/]+?)(\.go)??)/?$`)
|
||||
)
|
||||
|
||||
var tabwidth = 4
|
||||
|
||||
func init() {
|
||||
// calculate the domain name we are serving packages for based on the
|
||||
// directory we are serving from.
|
||||
r := *root
|
||||
if r == "" {
|
||||
r, _ = os.Getwd()
|
||||
}
|
||||
domainName = pathpkg.Base(pathpkg.Dir(pathpkg.Clean(r)))
|
||||
}
|
||||
|
||||
type PageInfo struct {
|
||||
Dirname string // directory containing the package
|
||||
Err error // error or nil
|
||||
|
@ -182,25 +188,6 @@ func comment_htmlFunc(comment string) string {
|
|||
return buf.String()
|
||||
}
|
||||
|
||||
// domainInPath verifies that filePath contains one of the allowed domains we're
|
||||
// serving for. On success, it returns which domain was found, and at which
|
||||
// position index in filePath.
|
||||
func domainInPath(filePath string) (string, int, error) {
|
||||
var idx int
|
||||
var domainName string
|
||||
for _, v := range domainNames {
|
||||
domainName = v
|
||||
idx = strings.LastIndex(filePath, domainName)
|
||||
if idx != -1 {
|
||||
break
|
||||
}
|
||||
}
|
||||
if idx == -1 {
|
||||
return "", idx, fmt.Errorf("No domain name \"%s\" in path to file %s", domainName, filePath)
|
||||
}
|
||||
return domainName, idx, nil
|
||||
}
|
||||
|
||||
func posLink_urlFunc(node ast.Node, fset *token.FileSet) string {
|
||||
var relpath string
|
||||
var line int
|
||||
|
@ -208,9 +195,9 @@ func posLink_urlFunc(node ast.Node, fset *token.FileSet) string {
|
|||
|
||||
if p := node.Pos(); p.IsValid() {
|
||||
pos := fset.Position(p)
|
||||
domainName, idx, err := domainInPath(pos.Filename)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
idx := strings.LastIndex(pos.Filename, domainName)
|
||||
if idx == -1 {
|
||||
log.Fatalf("No \"%s\" in path to file %s", domainName, pos.Filename)
|
||||
}
|
||||
relpath = pathpkg.Clean(pos.Filename[idx+len(domainName):])
|
||||
line = pos.Line
|
||||
|
@ -242,9 +229,9 @@ func posLink_urlFunc(node ast.Node, fset *token.FileSet) string {
|
|||
}
|
||||
|
||||
func srcLinkFunc(s string) string {
|
||||
domainName, idx, err := domainInPath(s)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
idx := strings.LastIndex(s, domainName)
|
||||
if idx == -1 {
|
||||
log.Fatalf("No \"%s\" in path to file %s", domainName, s)
|
||||
}
|
||||
return pathpkg.Clean(s[idx+len(domainName):])
|
||||
}
|
||||
|
@ -256,8 +243,7 @@ func (pi *PageInfo) populateDirs(diskPath string, depth int) {
|
|||
pi.DirTime = time.Now()
|
||||
}
|
||||
|
||||
func getPageInfo(domainName, suffix, diskPath string) (pi PageInfo, err error) {
|
||||
pkgName := pathpkg.Join(domainName, suffix)
|
||||
func getPageInfo(pkgName, diskPath string) (pi PageInfo, err error) {
|
||||
if pkgName == pathpkg.Join(domainName, pkgPattern) ||
|
||||
pkgName == pathpkg.Join(domainName, cmdPattern) ||
|
||||
pkgName == pathpkg.Join(domainName, appPattern) {
|
||||
|
@ -425,21 +411,6 @@ type godocHandler struct{}
|
|||
|
||||
func (godocHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
m := docRx.FindStringSubmatch(r.URL.Path)
|
||||
hostName := r.Host
|
||||
var domainName string
|
||||
found := false
|
||||
for _, v := range domainNames {
|
||||
domainName = v
|
||||
if hostName == domainName {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
log.Printf("%v hostname not an allowed domain name", hostName)
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
suffix := ""
|
||||
if m == nil {
|
||||
if r.URL.Path != pkgPattern && r.URL.Path != cmdPattern && r.URL.Path != appPattern {
|
||||
|
@ -458,14 +429,15 @@ func (godocHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
pi, err := getPageInfo(domainName, suffix, diskPath)
|
||||
pkgName := pathpkg.Join(domainName, suffix)
|
||||
pi, err := getPageInfo(pkgName, diskPath)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return
|
||||
}
|
||||
|
||||
subtitle := pathpkg.Base(diskPath)
|
||||
title := subtitle + " (" + pathpkg.Join(domainName, suffix) + ")"
|
||||
title := subtitle + " (" + pkgName + ")"
|
||||
servePage(w, r, pageParams{
|
||||
title: title,
|
||||
subtitle: subtitle,
|
||||
|
|
Loading…
Reference in New Issue