mirror of
https://github.com/gomods/athens
synced 2026-02-03 12:10:32 +00:00
* feat: add golangci-lint linting * chore: fix linter issues * feat: add linting into the workflow * docs: update lint docs * fix: cr suggestions * fix: remove old formatting and vetting scripts * fix: add docker make target * fix: action go caching * fix: depreciated actions checkout version * fix: cr suggestion * fix: cr suggestions --------- Co-authored-by: Manu Gupta <manugupt1@gmail.com>
66 lines
2.3 KiB
Go
66 lines
2.3 KiB
Go
package download
|
|
|
|
import (
|
|
"net/http"
|
|
"net/url"
|
|
"path"
|
|
|
|
"github.com/gomods/athens/pkg/download/mode"
|
|
"github.com/gomods/athens/pkg/log"
|
|
"github.com/gomods/athens/pkg/middleware"
|
|
"github.com/gorilla/mux"
|
|
)
|
|
|
|
// ProtocolHandler is a function that takes all that it needs to return
|
|
// a ready-to-go http handler that serves up cmd/go's download protocol.
|
|
type ProtocolHandler func(dp Protocol, lggr log.Entry, df *mode.DownloadFile) http.Handler
|
|
|
|
// HandlerOpts are the generic options for a ProtocolHandler.
|
|
type HandlerOpts struct {
|
|
Protocol Protocol
|
|
Logger *log.Logger
|
|
DownloadFile *mode.DownloadFile
|
|
}
|
|
|
|
// LogEntryHandler pulls a log entry from the request context. Thanks to the
|
|
// LogEntryMiddleware, we should have a log entry stored in the context for each
|
|
// request with request-specific fields. This will grab the entry and pass it to
|
|
// the protocol handlers.
|
|
func LogEntryHandler(ph ProtocolHandler, opts *HandlerOpts) http.Handler {
|
|
f := func(w http.ResponseWriter, r *http.Request) {
|
|
ent := log.EntryFromContext(r.Context())
|
|
handler := ph(opts.Protocol, ent, opts.DownloadFile)
|
|
handler.ServeHTTP(w, r)
|
|
}
|
|
return http.HandlerFunc(f)
|
|
}
|
|
|
|
// RegisterHandlers is a convenience method that registers
|
|
// all the download protocol paths for you.
|
|
func RegisterHandlers(r *mux.Router, opts *HandlerOpts) {
|
|
// If true, this would only panic at boot time, static nil checks anyone?
|
|
if opts == nil || opts.Protocol == nil || opts.Logger == nil {
|
|
panic("absolutely unacceptable handler opts")
|
|
}
|
|
noCacheMw := middleware.CacheControl("no-cache, no-store, must-revalidate")
|
|
|
|
listHandler := LogEntryHandler(ListHandler, opts)
|
|
r.Handle(PathList, noCacheMw(listHandler))
|
|
|
|
latestHandler := LogEntryHandler(LatestHandler, opts)
|
|
r.Handle(PathLatest, noCacheMw(latestHandler)).Methods(http.MethodGet)
|
|
|
|
r.Handle(PathVersionInfo, LogEntryHandler(InfoHandler, opts)).Methods(http.MethodGet)
|
|
r.Handle(PathVersionModule, LogEntryHandler(ModuleHandler, opts)).Methods(http.MethodGet)
|
|
r.Handle(PathVersionZip, LogEntryHandler(ZipHandler, opts)).Methods(http.MethodGet, http.MethodHead)
|
|
}
|
|
|
|
func getRedirectURL(base, downloadPath string) (string, error) {
|
|
url, err := url.Parse(base)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
url.Path = path.Join(url.Path, downloadPath)
|
|
return url.String(), nil
|
|
}
|