Skip to content

Commit

Permalink
add support for go-source meta tags
Browse files Browse the repository at this point in the history
Go Packages inspects this to generate source links. Legacy GoDoc
websites do too. There are perhaps other reasons a go-get redirector
would want to include these. This change adds primitive support for
them.
  • Loading branch information
djmoch committed Dec 9, 2023
1 parent 08f6126 commit e29abc4
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 31 deletions.
18 changes: 11 additions & 7 deletions cmd/shrt/internal/base/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ import (

// Environment variable keys
const (
SHRTENV = "SHRTENV"
SHRT_SRVNAME = "SHRT_SRVNAME"
SHRT_SCMTYPE = "SHRT_SCMTYPE"
SHRT_SUFFIX = "SHRT_SUFFIX"
SHRT_RDRNAME = "SHRT_RDRNAME"
SHRT_BARERDR = "SHRT_BARERDR"
SHRT_DBPATH = "SHRT_DBPATH"
SHRTENV = "SHRTENV"
SHRT_SRVNAME = "SHRT_SRVNAME"
SHRT_SCMTYPE = "SHRT_SCMTYPE"
SHRT_SUFFIX = "SHRT_SUFFIX"
SHRT_RDRNAME = "SHRT_RDRNAME"
SHRT_BARERDR = "SHRT_BARERDR"
SHRT_DBPATH = "SHRT_DBPATH"
SHRT_GOSOURCEDIR = "SHRT_GOSOURCEDIR"
SHRT_GOSOURCEFILE = "SHRT_GOSOURCEFILE"
)

// KnownEnv is a list of environment variables that affect the
Expand All @@ -30,6 +32,8 @@ const KnownEnv = `
SHRT_RDRNAME
SHRT_BARERDR
SHRT_DBPATH
SHRT_GOSOURCEDIR
SHRT_GOSOURCEFILE
`

type Command struct {
Expand Down
32 changes: 19 additions & 13 deletions cmd/shrt/internal/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ import (
)

const (
srvNameDefault = "example.com"
scmTypeDefault = "git"
suffixDefault = ".git"
rdrNameDefault = "github.com/user"
bareRdrDefault = "example.org"
srvNameDefault = "example.com"
scmTypeDefault = "git"
suffixDefault = ".git"
rdrNameDefault = "github.com/user"
bareRdrDefault = "example.org"
goSourceDirDefault = ""
goSourceFileDefault = ""
)

var Cmd = &base.Command{
Expand Down Expand Up @@ -187,7 +189,9 @@ func ConfigFromEnv() shrt.Config {
RdrName: envOrDefault(base.SHRT_RDRNAME, rdrNameDefault),
BareRdr: envOrDefault(base.SHRT_BARERDR, bareRdrDefault),
// Trim the leading / to satisfy fs.FS
DbPath: strings.TrimPrefix(envOrDefault(base.SHRT_DBPATH, dbPathDefault), "/"),
DbPath: strings.TrimPrefix(envOrDefault(base.SHRT_DBPATH, dbPathDefault), "/"),
GoSourceDir: envOrDefault(base.SHRT_GOSOURCEDIR, goSourceDirDefault),
GoSourceFile: envOrDefault(base.SHRT_GOSOURCEFILE, goSourceFileDefault),
}
}

Expand Down Expand Up @@ -224,13 +228,15 @@ func MergeEnv() {
}

defaults := map[string]string{
base.SHRTENV: envDefault,
base.SHRT_SRVNAME: srvNameDefault,
base.SHRT_SCMTYPE: scmTypeDefault,
base.SHRT_SUFFIX: suffixDefault,
base.SHRT_RDRNAME: rdrNameDefault,
base.SHRT_BARERDR: bareRdrDefault,
base.SHRT_DBPATH: dbPathDefault,
base.SHRTENV: envDefault,
base.SHRT_SRVNAME: srvNameDefault,
base.SHRT_SCMTYPE: scmTypeDefault,
base.SHRT_SUFFIX: suffixDefault,
base.SHRT_RDRNAME: rdrNameDefault,
base.SHRT_BARERDR: bareRdrDefault,
base.SHRT_DBPATH: dbPathDefault,
base.SHRT_GOSOURCEDIR: goSourceDirDefault,
base.SHRT_GOSOURCEFILE: goSourceFileDefault,
}

// Populate missing environment variables with defaults
Expand Down
35 changes: 24 additions & 11 deletions shrt.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ var shrtrsp = `<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>"
<meta name="go-import" content="{{ .SrvName }}/{{ .Repo }} {{ .ScmType }} {{ .URL }}">
<meta name="go-import" content="{{ .SrvName }}/{{ .Repo }} {{ .ScmType }} {{ .URL }}">{{ if ne .GoSourceDir "" }}
<meta name="go-source" content="{{ .SrvName }}/{{ .Repo }} {{ .URL }} {{ .URL }}/{{ .GoSourceDir }} {{ .URL }}/{{ .GoSourceFile }}">{{ end }}
<meta http-equiv="refresh" content="0; url=https://godoc.org/{{ .SrvName }}/{{ .DocPath }}">
</head>
<body>
Expand All @@ -43,11 +44,13 @@ Redirecting to docs at <a href="https://godoc.org/{{ .SrvName }}/{{ .DocPath }}"
`

type shrtRequest struct {
SrvName string
Repo string
ScmType string
URL string
DocPath string
SrvName string
Repo string
ScmType string
URL string
DocPath string
GoSourceDir string
GoSourceFile string
}

// Config contains all of the global configuration for Shrt. All
Expand All @@ -66,6 +69,14 @@ type Config struct {
BareRdr string
// The path to the [ShrtFile]-formatted database file.
DbPath string
// The string to append to the URL for go-get redirects to
// form the directory entry in the go-source meta tag. This
// key is experimental and may be removed in a future release.
GoSourceDir string
// The string to append to the URL for go-get redirects to
// form the file entry in the go-source meta tag. This
// key is experimental and may be removed in a future release.
GoSourceFile string
}

// ShrtHandler is the core [http.Handler] for go-shrt.
Expand Down Expand Up @@ -124,11 +135,13 @@ func (s *ShrtHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
log.Println("go-get request for", key)
t := template.Must(template.New("shrt").Parse(shrtrsp))
sReq := shrtRequest{
SrvName: s.Config.SrvName,
Repo: key,
ScmType: s.Config.ScmType,
URL: val.URL,
DocPath: p,
SrvName: s.Config.SrvName,
Repo: key,
ScmType: s.Config.ScmType,
URL: val.URL,
DocPath: p,
GoSourceDir: s.Config.GoSourceDir,
GoSourceFile: s.Config.GoSourceFile,
}
if err := t.Execute(w, sReq); err != nil {
log.Println("error executing template:", err)
Expand Down

0 comments on commit e29abc4

Please sign in to comment.