diff --git a/README.md b/README.md index 380c946..3494d74 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,3 @@ # goget-redirect +Quick and dirty app to handle redirects for our infrastructure. \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..3280615 --- /dev/null +++ b/go.mod @@ -0,0 +1,11 @@ +module git.kolaente.de/konrad/goget-redirect + +require ( + github.com/labstack/echo v3.3.5+incompatible + github.com/labstack/gommon v0.2.8 // indirect + github.com/mattn/go-colorable v0.0.9 // indirect + github.com/mattn/go-isatty v0.0.4 // indirect + github.com/valyala/bytebufferpool v1.0.0 // indirect + github.com/valyala/fasttemplate v0.0.0-20170224212429-dcecefd839c4 // indirect + golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..85241b1 --- /dev/null +++ b/go.sum @@ -0,0 +1,14 @@ +github.com/labstack/echo v3.3.5+incompatible h1:9PfxPUmasKzeJor9uQTaXLT6WUG/r+vSTmvXxvv3JO4= +github.com/labstack/echo v3.3.5+incompatible/go.mod h1:0INS7j/VjnFxD4E2wkz67b8cVwCLbBmJyDaka6Cmk1s= +github.com/labstack/gommon v0.2.8 h1:JvRqmeZcfrHC5u6uVleB4NxxNbzx6gpbJiQknDbKQu0= +github.com/labstack/gommon v0.2.8/go.mod h1:/tj9csK2iPSBvn+3NLM9e52usepMtrd5ilFYA+wQNJ4= +github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRUIY4= +github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= +github.com/mattn/go-isatty v0.0.4 h1:bnP0vzxcAdeI1zdubAl5PjU6zsERjGZb7raWodagDYs= +github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= +github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= +github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= +github.com/valyala/fasttemplate v0.0.0-20170224212429-dcecefd839c4 h1:gKMu1Bf6QINDnvyZuTaACm9ofY+PRh+5vFz4oxBZeF8= +github.com/valyala/fasttemplate v0.0.0-20170224212429-dcecefd839c4/go.mod h1:50wTf68f99/Zt14pr046Tgt3Lp2vLyFZKzbFXTOabXw= +golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9 h1:mKdxBk7AujPs8kU4m80U72y/zjbZ3UcXC7dClwKbUI0= +golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= diff --git a/main.go b/main.go new file mode 100644 index 0000000..1b025cd --- /dev/null +++ b/main.go @@ -0,0 +1,59 @@ +package main + +import ( + "github.com/labstack/echo" + "html/template" + "io" + "net/http" +) + +const BaseUrl = `https://git.kolaente.de/vikunja` +const Domain = `code.vikunja.io` +const GoGetTemplate = ` + + + + + + + + go get ` + Domain + `{{.}} + + +` + +type Template struct { + templates *template.Template +} + +func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error { + return t.templates.ExecuteTemplate(w, name, data) +} + +func redirectToBase(c echo.Context) error { + return c.Redirect(http.StatusFound, BaseUrl+c.Request().URL.String()) +} + +func showGoGetMeta(c echo.Context) error { + switch c.Param("repo") { + case "web": + return c.Render(http.StatusOK, "goget", c.Request().URL.Path) + case "api": + return c.Render(http.StatusOK, "goget", c.Request().URL.Path) + default: + return redirectToBase(c) + } + return redirectToBase(c) +} + +func main() { + e := echo.New() + renderer := &Template{ + templates: template.Must(template.New("goget").Parse(GoGetTemplate)), + } + e.Renderer = renderer + + e.GET("/*", redirectToBase) + e.GET("/:repo", showGoGetMeta) + e.Logger.Fatal(e.Start(":8080")) +}