diff --git a/.github/workflows/build-and-release.yaml b/.github/workflows/build-and-release.yaml index 53b1594..3e5fc5d 100644 --- a/.github/workflows/build-and-release.yaml +++ b/.github/workflows/build-and-release.yaml @@ -9,11 +9,14 @@ on: pull_request: branches: [ main ] +concurrency: + group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} + cancel-in-progress: true env: - GO_VERSION: 1.19.8 - RUBY_VERSION: 3.1.2 - BUNDLER_VERSION: 2.3.7 + GO_VERSION: 1.22.1 + RUBY_VERSION: 3.3.1 + BUNDLER_VERSION: 2.5.9 jobs: build: diff --git a/Dockerfile b/Dockerfile index ebeeaa0..54c335f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM golang:1.19 +FROM golang:1.22 LABEL org.opencontainers.image.source="https://github.com/gemfast/server" WORKDIR /app diff --git a/README.md b/README.md index 79f687d..c6bf5e7 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,8 @@ For more information see: https://gemfast.io/docs/configuration/ ## UI +🚧 **The UI feature is currently under construction and is considered experimental** 🚧 + ![Dashboard UI](https://github.com/gemfast/server/raw/main/SCREENSHOT.png) Gemfast includes a basic ui which is accessible from `my.server.url/ui`. For example, running it locally you can access it at `http://localhost:2020/ui`. diff --git a/VERSION b/VERSION index 7dea76e..9084fa2 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.0.1 +1.1.0 diff --git a/go.mod b/go.mod index 8fe0d6d..f385c51 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,8 @@ module github.com/gemfast/server -go 1.19 +go 1.22 + +toolchain go1.22.1 require ( github.com/akyoto/cache v1.0.6 diff --git a/go.sum b/go.sum index 40de97b..60ab5c0 100644 --- a/go.sum +++ b/go.sum @@ -13,6 +13,7 @@ github.com/agext/levenshtein v1.2.1/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki github.com/akyoto/cache v1.0.6 h1:5XGVVYoi2i+DZLLPuVIXtsNIJ/qaAM16XT0LaBaXd2k= github.com/akyoto/cache v1.0.6/go.mod h1:WfxTRqKhfgAG71Xh6E3WLpjhBtZI37O53G4h5s+3iM4= github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be h1:9AeTilPcZAjCFIImctFaOjnTIavg87rW78vTPkQqLI8= +github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be/go.mod h1:ySMOLuWl6zY27l47sB3qLNK6tF2fkHG55UZxx8oIVo4= github.com/apparentlymart/go-textseg/v13 v13.0.0 h1:Y+KvPE1NYz0xl601PVImeQfFyEy6iT90AvPUL1NNfNw= github.com/apparentlymart/go-textseg/v13 v13.0.0/go.mod h1:ZK2fH7c4NqDTLtiYLvIkEghdlcqw7yxLeM89kiTRPUo= github.com/appleboy/gin-jwt/v2 v2.8.0 h1:Glo7cb9eBR+hj8Y7WzgfkOlqCaNLjP+RV4dNO3fpdps= @@ -26,6 +27,7 @@ github.com/aquasecurity/go-gem-version v0.0.0-20201115065557-8eed6fe000ce/go.mod github.com/aquasecurity/go-version v0.0.0-20201107203531-5e48ac5d022a h1:SMEtDBnLyP/EVOeJhj4yeR8GYPFpBsFBk3lSrpjZ8yI= github.com/aquasecurity/go-version v0.0.0-20201107203531-5e48ac5d022a/go.mod h1:9Beu8XsUNNfzml7WBf3QmyPToP1wm1Gj/Vc5UJKqTzU= github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio= +github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 h1:DDGfHa7BWjL4YnC6+E63dPcxHo2sUxDIu8g3QgEJdRY= @@ -53,6 +55,7 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/elazarl/goproxy v0.0.0-20230808193330-2592e75ae04a h1:mATvB/9r/3gvcejNsXKSkQ6lcIaNec2nyfOdlTBR2lU= +github.com/elazarl/goproxy v0.0.0-20230808193330-2592e75ae04a/go.mod h1:Ro8st/ElPeALwNFlcTpWmkr6IoMFfkjXAvTHpevnDsM= github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc= github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ= github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= @@ -68,15 +71,18 @@ github.com/gin-gonic/gin v1.7.7/go.mod h1:axIBovoeJpVj8S3BwE0uPMTeReE4+AfFtqpqaZ github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg= github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU= github.com/gliderlabs/ssh v0.3.5 h1:OcaySEmAQJgyYcArR+gGGTHCyE7nvhEMTlYY+Dp8CpY= +github.com/gliderlabs/ssh v0.3.5/go.mod h1:8XB4KraRrX39qHhT6yxPsHedjA08I/uBVwj4xC+/+z4= github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 h1:+zs/tPmkDkHx3U66DAb0lQFJrpS6731Oaa12ikc+DiI= github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376/go.mod h1:an3vInlBmSxCcxctByoQdvwPiA7DTK7jaaFDBTtu0ic= github.com/go-git/go-billy/v5 v5.5.0 h1:yEY4yhzCDuMGSv83oGxiBotRzhwhNr8VZyphhiu+mTU= github.com/go-git/go-billy/v5 v5.5.0/go.mod h1:hmexnoNsr2SJU1Ju67OaNz5ASJY3+sHgFRpCtpDCKow= github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20231010084843-55a94097c399 h1:eMje31YglSBqCdIqdhKBW8lokaMrL3uTkpGYlE2OOT4= +github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20231010084843-55a94097c399/go.mod h1:1OCfN199q1Jm3HZlxleg+Dw/mwps2Wbk9frAWm+4FII= github.com/go-git/go-git/v5 v5.11.0 h1:XIZc1p+8YzypNr34itUfSvYJcv+eYdTnTvOZ2vD3cA4= github.com/go-git/go-git/v5 v5.11.0/go.mod h1:6GFcX2P3NM7FPBfpePbpLd21XxsgdAt+lKqXmCUiUCY= github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= +github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8= github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= @@ -87,6 +93,7 @@ github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn github.com/go-playground/validator/v10 v10.14.0 h1:vgvQWe3XCz3gIeFDm/HnTIbj6UGmg/+t63MyGU2n5js= github.com/go-playground/validator/v10 v10.14.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU= github.com/go-test/deep v1.0.3 h1:ZrJSEWsXzPOxaZnFteGEfooLba+ju3FYIbOrS+rQd68= +github.com/go-test/deep v1.0.3/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA= github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= github.com/goccy/go-yaml v1.8.1 h1:JuZRFlqLM5cWF6A+waL8AKVuCcqvKOuhJtUQI+L3ez0= @@ -132,10 +139,13 @@ github.com/klauspost/cpuid/v2 v2.2.4/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8t github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348 h1:MtvEpTB6LX3vkb4ax0b5D2DHbNAUsen0Gx5wZoq3lV4= +github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348/go.mod h1:B69LEHPfb2qLo0BaaOLcbitczOKLWTsrBG9LczfCD4k= github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII= github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q= github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4= @@ -161,6 +171,7 @@ github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lN github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= github.com/onsi/gomega v1.27.10 h1:naR28SdDFlqrG6kScpT8VWpu1xWY5nJRCF3XaYyBjhI= +github.com/onsi/gomega v1.27.10/go.mod h1:RsS8tutOdbdgzbPtzzATp12yT7kM5I5aElG3evPbQ0M= github.com/pelletier/go-toml/v2 v2.0.8 h1:0ctb6s9mE31h0/lhu+J6OPmVeDxJn+kYnJc2jZR9tGQ= github.com/pelletier/go-toml/v2 v2.0.8/go.mod h1:vuYfssBdrU2XDZ9bYydBu6t+6a6PYNcZljzZR9VXg+4= github.com/pjbgf/sha1cd v0.3.0 h1:4D5XXmUUBUl/xQ6IjCkEAbqXskkq/4O7LmGn0AqMDs4= @@ -178,6 +189,7 @@ github.com/prometheus/common v0.44.0/go.mod h1:ofAIvZbQ1e/nugmZGz4/qCb9Ap1VoSTIO github.com/prometheus/procfs v0.11.1 h1:xRC8Iq1yyca5ypa9n1EZnWZkt7dwcoRPQwX/5gwaUuI= github.com/prometheus/procfs v0.11.1/go.mod h1:eesXgaPo1q7lBpVMoMy0ZOFTth9hBn4W/y0/p/ScXhY= github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= +github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= github.com/rs/xid v1.3.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= github.com/rs/zerolog v1.27.0 h1:1T7qCieN22GVc8S4Q2yuexzBb1EqjbgjSH9RohbMjKs= github.com/rs/zerolog v1.27.0/go.mod h1:7frBqO0oezxmnO7GF86FY++uy8I0Tk/If5ni1G9Qc0U= @@ -269,6 +281,7 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E= +golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -302,6 +315,7 @@ golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc= golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U= golang.org/x/term v0.18.0 h1:FcHjZXDMxI8mM3nwhX9HlKop4C0YQvCVCdwYl2wOtE8= +golang.org/x/term v0.18.0/go.mod h1:ILwASektA3OnRv7amZ1xhE/KTR+u50pbXfZ03+6Nx58= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= @@ -331,6 +345,7 @@ google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHh gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE= gopkg.in/go-playground/validator.v9 v9.30.0 h1:Wk0Z37oBmKj9/n+tPyBHZmeL19LaCoK3Qq48VwYENss= gopkg.in/go-playground/validator.v9 v9.30.0/go.mod h1:+c9/zcJMFNgbLvly1L1V+PpxWdVbfP1avr/N00E2vyQ= diff --git a/internal/api/api.go b/internal/api/api.go index d1087e5..214a53f 100644 --- a/internal/api/api.go +++ b/internal/api/api.go @@ -49,7 +49,7 @@ func (api *API) Run() { if api.cfg.Mirrors[0].Enabled { log.Info().Str("detail", api.cfg.Mirrors[0].Upstream).Msg("mirroring upstream gem server") } - log.Info().Str("detail", port).Msg("gemfast server listening on port") + log.Info().Str("detail", fmt.Sprintf("http://localhost%s", port)).Msg("gemfast server started") err := api.router.Run(port) if err != nil { log.Fatal().Err(err).Msg("failed to start server") @@ -86,7 +86,7 @@ func (api *API) registerRoutes() { case "local": api.configureLocalAuth() case "none": - api.configureNoneAuth() + api.configureNoneAuth(ui) default: log.Fatal().Msg(fmt.Sprintf("invalid auth type: %s", authMode)) } @@ -137,7 +137,7 @@ func (api *API) configureLocalAuth() { api.configurePrivate() } -func (api *API) configureNoneAuth() { +func (api *API) configureNoneAuth(ui *ui.UI) { if api.cfg.Mirrors[0].Enabled { mirror := api.router.Group("/") api.configureMirror(mirror) @@ -147,6 +147,12 @@ func (api *API) configureNoneAuth() { api.configurePrivateWrite(private) admin := api.router.Group(adminAPIPath) api.configureAdmin(admin) + if !api.cfg.UIDisabled { + api.router.StaticFS("/ui/assets", http.FS(ui.Assets)) + uiGroup := api.router.Group("/ui") + api.configureUI(ui, uiGroup) + log.Info().Str("detail", "/ui").Msg("gemfast ui enabled") + } } // / diff --git a/internal/indexer/indexer.go b/internal/indexer/indexer.go index feb6e74..3a05918 100644 --- a/internal/indexer/indexer.go +++ b/internal/indexer/indexer.go @@ -397,7 +397,6 @@ func (indexer *Indexer) updateSpecsIndex(updated []*spec.Spec, src string, dest ) if err != nil { log.Error().Err(err).Str("detail", dest).Msg("failed to create destination spec index file") - panic(err) } defer file.Close() @@ -405,7 +404,6 @@ func (indexer *Indexer) updateSpecsIndex(updated []*spec.Spec, src string, dest bytesWritten, err := file.Write(dump) if err != nil { log.Error().Err(err).Str("detail", dest).Msg("failed to write destination spec index file") - panic(err) } log.Info().Str("detail", src).Int("len", len(uniqSpecsIdx)).Msg("updated index") ch <- bytesWritten diff --git a/internal/spec/spec.go b/internal/spec/spec.go index b50b504..defc7fa 100644 --- a/internal/spec/spec.go +++ b/internal/spec/spec.go @@ -30,8 +30,8 @@ type Spec struct { RubyGems string //TODO: parse required_rubygems_version from metadata } -func untar(dir, full_name, gemfile string) ([]byte, string, error) { - tmpdir, err := os.MkdirTemp(dir, full_name) +func untar(dir, fullName, gemfile string) ([]byte, string, error) { + tmpdir, err := os.MkdirTemp(dir, fullName) if err != nil { log.Error().Err(err).Msg("failed to create tmpdir") return nil, "", err @@ -62,7 +62,7 @@ func untar(dir, full_name, gemfile string) ([]byte, string, error) { if err == io.EOF { break } - log.Error().Err(err).Str("detail", full_name).Msg("bad header") + log.Error().Err(err).Str("detail", fullName).Msg("bad header") return nil, "", err } @@ -105,10 +105,10 @@ func untar(dir, full_name, gemfile string) ([]byte, string, error) { } func GunzipMetadata(path string) (string, error) { - fname := fmt.Sprintf("%s/metadata.gz", path) - file, err := os.Open(fname) + meta := fmt.Sprintf("%s/metadata.gz", path) + file, err := os.Open(meta) if err != nil { - log.Error().Err(err).Str("detail", fname).Msg("failed to open file") + log.Error().Err(err).Str("detail", meta).Msg("failed to open file") return "", err } defer file.Close() @@ -116,13 +116,13 @@ func GunzipMetadata(path string) (string, error) { var fileReader io.ReadCloser = file gzreader, err := gzip.NewReader(fileReader) if err != nil { - log.Error().Err(err).Str("detail", fname).Msg("failed to create gzip reader") + log.Error().Err(err).Str("detail", meta).Msg("failed to create gzip reader") return "", err } output, err := io.ReadAll(gzreader) if err != nil { - log.Error().Err(err).Str("detail", fname).Msg("failed to read gzip content") + log.Error().Err(err).Str("detail", meta).Msg("failed to read gzip content") return "", err } @@ -258,8 +258,8 @@ func ParseGemMetadata(yamlBytes []byte) (*GemMetadata, error) { } func FromFile(dir, gemfile string) (*Spec, error) { - path_chunks := strings.Split(gemfile, "/") - full := path_chunks[len(path_chunks)-1] + chunks := strings.Split(gemfile, "/") + full := chunks[len(chunks)-1] ogName := strings.TrimSuffix(full, ".gem") log.Trace().Str("detail", gemfile).Msg("untarring gemfile") sum, tmpdir, err := untar(dir, full, gemfile) diff --git a/internal/ui/templates/ui/index.tmpl b/internal/ui/templates/ui/index.tmpl index b0f8609..e252594 100644 --- a/internal/ui/templates/ui/index.tmpl +++ b/internal/ui/templates/ui/index.tmpl @@ -41,12 +41,11 @@ + {{ if eq .authType "github" }} - + {{ end }}
{{ if eq .authType "github" }} @@ -77,7 +76,7 @@ -

© 2023 Gemfast

+

Built with htmx

diff --git a/internal/ui/ui.go b/internal/ui/ui.go index 8792e5f..3b0aaf5 100644 --- a/internal/ui/ui.go +++ b/internal/ui/ui.go @@ -35,6 +35,35 @@ type UI struct { Assets fs.FS } +func getUser(c *gin.Context, ui *UI) (string, error) { + if ui.cfg.Auth.Type == "github" { + session := sessions.Default(c) + sessionAuth := session.Get("authToken") + jwtToken, err := jwt.Parse(sessionAuth.(string), func(t *jwt.Token) (interface{}, error) { + if jwt.GetSigningMethod("HS256") != t.Method { + return nil, errors.New("invalid signing method") + } + return []byte(ui.cfg.Auth.JWTSecretKey), nil + }) + if err != nil { + log.Error().Err(err).Msg("failed to parse jwt token from request") + return "", err + } + if !jwtToken.Valid { + log.Error().Msg("invalid jwt token") + return "", err + } + claims := jmw.ExtractClaimsFromToken(jwtToken) + username, ok := claims[middleware.IdentityKey].(string) + if !ok { + log.Error().Str("username", username).Msg("failed to get user from jwt token") + return "", err + } + return username, nil + } + return "anon", nil +} + func NewUI(cfg *config.Config, db *db.DB) *UI { static, err := fs.Sub(assets, "assets") if err != nil { @@ -51,24 +80,10 @@ func (ui *UI) Index(c *gin.Context) { c.Abort() return } - session := sessions.Default(c) - sessionAuth := session.Get("authToken") - jwtToken, err := jwt.Parse(sessionAuth.(string), func(t *jwt.Token) (interface{}, error) { - if jwt.GetSigningMethod("HS256") != t.Method { - return nil, errors.New("invalid signing method") - } - return []byte(ui.cfg.Auth.JWTSecretKey), nil - }) + username, err := getUser(c, ui) if err != nil { - log.Error().Err(err).Msg("failed to parse jwt token from request") - } - if !jwtToken.Valid { - log.Error().Msg("invalid jwt token") - } - claims := jmw.ExtractClaimsFromToken(jwtToken) - username, ok := claims[middleware.IdentityKey].(string) - if !ok { - log.Error().Str("username", username).Msg("failed to get user from jwt token") + c.String(http.StatusInternalServerError, err.Error()) + return } c.HTML(http.StatusOK, "index", gin.H{ "authType": ui.cfg.Auth.Type,