forked from gomods/athens
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Backfill the cache on a miss (gomods#342)
* add middlewares to fill the cache and to populate contexts with module and versions * Carolyn tries to fix Aaron's code * #DOINSTUFF * updated env var * fix all the things * magic patch for olympus * Add latest handler * Remove deprecated return param * go fmt all the things
- Loading branch information
Showing
14 changed files
with
132 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package download | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/bketelsen/buffet" | ||
"github.com/gobuffalo/buffalo" | ||
"github.com/gobuffalo/buffalo/render" | ||
"github.com/gomods/athens/pkg/errors" | ||
"github.com/gomods/athens/pkg/log" | ||
"github.com/gomods/athens/pkg/paths" | ||
) | ||
|
||
// PathLatest URL. | ||
const PathLatest = "/{module:.+}/@latest" | ||
|
||
// LatestHandler implements GET baseURL/module/@latest | ||
func LatestHandler(dp Protocol, lggr *log.Logger, eng *render.Engine) func(c buffalo.Context) error { | ||
return func(c buffalo.Context) error { | ||
sp := buffet.SpanFromContext(c) | ||
sp.SetOperationName("latestHandler") | ||
mod, err := paths.GetModule(c) | ||
if err != nil { | ||
lggr.SystemErr(err) | ||
return c.Render(500, nil) | ||
} | ||
|
||
info, err := dp.Latest(c, mod) | ||
if err != nil { | ||
lggr.SystemErr(err) | ||
return c.Render(errors.Kind(err), eng.JSON(errors.KindText(err))) | ||
} | ||
|
||
return c.Render(http.StatusOK, eng.JSON(info)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,35 @@ | ||
package download | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/bketelsen/buffet" | ||
"github.com/gobuffalo/buffalo" | ||
"github.com/gobuffalo/buffalo/render" | ||
"github.com/gomods/athens/pkg/paths" | ||
"github.com/gomods/athens/pkg/storage" | ||
"github.com/gomods/athens/pkg/errors" | ||
"github.com/gomods/athens/pkg/log" | ||
) | ||
|
||
// PathVersionModule URL. | ||
const PathVersionModule = "/{module:.+}/@v/{version}.mod" | ||
|
||
// VersionModuleHandler implements GET baseURL/module/@v/version.mod | ||
func VersionModuleHandler(getter storage.Getter, eng *render.Engine) func(c buffalo.Context) error { | ||
func VersionModuleHandler(dp Protocol, lggr *log.Logger, eng *render.Engine) buffalo.Handler { | ||
return func(c buffalo.Context) error { | ||
const op errors.Op = "download.VersionModuleHandler" | ||
|
||
sp := buffet.SpanFromContext(c) | ||
sp.SetOperationName("versionModuleHandler") | ||
params, err := paths.GetAllParams(c) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
version, err := getter.Get(params.Module, params.Version) | ||
if storage.IsNotFoundError(err) { | ||
msg := fmt.Sprintf("%s@%s not found", params.Module, params.Version) | ||
return c.Render(http.StatusNotFound, eng.JSON(msg)) | ||
} else if err != nil { | ||
return err | ||
mod, ver, verInfo, err := getModuleVersion(c, lggr, dp) | ||
if err != nil { | ||
err := errors.E(op, errors.M(mod), errors.V(ver), err) | ||
lggr.SystemErr(err) | ||
c.Render(http.StatusInternalServerError, nil) | ||
} | ||
|
||
c.Response().WriteHeader(http.StatusOK) | ||
_, err = c.Response().Write(version.Mod) | ||
_, err = c.Response().Write(verInfo.Mod) | ||
return err | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,49 @@ | ||
package download | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"net/http" | ||
|
||
"github.com/bketelsen/buffet" | ||
"github.com/gobuffalo/buffalo" | ||
"github.com/gobuffalo/buffalo/render" | ||
"github.com/gomods/athens/pkg/config" | ||
"github.com/gomods/athens/pkg/errors" | ||
"github.com/gomods/athens/pkg/log" | ||
"github.com/gomods/athens/pkg/paths" | ||
"github.com/gomods/athens/pkg/storage" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
// PathVersionZip URL. | ||
const PathVersionZip = "/{module:.+}/@v/{version}.zip" | ||
|
||
// VersionZipHandler implements GET baseURL/module/@v/version.zip | ||
func VersionZipHandler(getter storage.Getter, eng *render.Engine, lggr *log.Logger) func(c buffalo.Context) error { | ||
func VersionZipHandler(dp Protocol, lggr *log.Logger, eng *render.Engine) buffalo.Handler { | ||
const op errors.Op = "download.VersionZipHandler" | ||
|
||
return func(c buffalo.Context) error { | ||
sp := buffet.SpanFromContext(c) | ||
sp.SetOperationName("versionZipHandler") | ||
params, err := paths.GetAllParams(c) | ||
if err != nil { | ||
lggr.SystemErr(errors.E(op, err)) | ||
c.Render(http.StatusInternalServerError, nil) // 500 because handler should not be called in the first place. | ||
return nil | ||
} | ||
|
||
version, err := getter.Get(params.Module, params.Version) | ||
lggr.Println("===getModuleVersion") | ||
mod, ver, verInfo, err := getModuleVersion(c, lggr, dp) | ||
if err != nil { | ||
lvl := logrus.ErrorLevel | ||
status := http.StatusInternalServerError | ||
msg := http.StatusText(status) | ||
// TODO: move this function to pkg/errors | ||
if storage.IsNotFoundError(err) { | ||
lvl = logrus.DebugLevel | ||
msg = fmt.Sprintf("%v not found", config.FmtModVer(params.Module, params.Version)) | ||
status = http.StatusNotFound | ||
} | ||
lggr.Println("===carolyn found an error", err) | ||
|
||
lggr.SystemErr(errors.E(op, errors.M(params.Module), errors.V(params.Version), lvl, err)) | ||
return c.Render(status, eng.JSON(msg)) | ||
err := errors.E(op, errors.M(mod), errors.V(ver), err) | ||
lggr.SystemErr(err) | ||
return c.Render(http.StatusInternalServerError, nil) | ||
} | ||
defer version.Zip.Close() | ||
|
||
lggr.Println("===Copy") | ||
status := http.StatusOK | ||
|
||
_, err = io.Copy(c.Response(), version.Zip) | ||
_, err = io.Copy(c.Response(), verInfo.Zip) | ||
if err != nil { | ||
lggr.Println("===Copy errored out", err) | ||
status = http.StatusInternalServerError | ||
lggr.SystemErr(errors.E(op, errors.M(params.Module), errors.V(params.Version), err)) | ||
lggr.SystemErr(errors.E(op, errors.M(mod), errors.V(ver), err)) | ||
} | ||
|
||
c.Response().WriteHeader(status) | ||
lggr.Println("===writeheader") | ||
|
||
c.Response().WriteHeader(status) | ||
return nil | ||
} | ||
} |
Oops, something went wrong.