From b90f9cc5484262a8088f65ba66ca677b3fafe43f Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 20 Nov 2023 07:00:38 +0000 Subject: [PATCH 01/24] Bump docker/build-push-action from 5.0.0 to 5.1.0 Bumps [docker/build-push-action](https://github.com/docker/build-push-action) from 5.0.0 to 5.1.0. - [Release notes](https://github.com/docker/build-push-action/releases) - [Commits](https://github.com/docker/build-push-action/compare/v5.0.0...v5.1.0) --- updated-dependencies: - dependency-name: docker/build-push-action dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] --- .github/workflows/docker.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index 9dcbd25..42afc31 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -49,7 +49,7 @@ jobs: # https://github.com/docker/build-push-action - name: Build and push Docker image id: build-and-push - uses: docker/build-push-action@v5.0.0 + uses: docker/build-push-action@v5.1.0 with: context: . push: true From adccabf7d63295bd092bb4ef10f630d4ea1645e5 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 25 Dec 2023 06:33:05 +0000 Subject: [PATCH 02/24] Bump docker/metadata-action from 5.0.0 to 5.4.0 Bumps [docker/metadata-action](https://github.com/docker/metadata-action) from 5.0.0 to 5.4.0. - [Release notes](https://github.com/docker/metadata-action/releases) - [Commits](https://github.com/docker/metadata-action/compare/v5.0.0...v5.4.0) --- updated-dependencies: - dependency-name: docker/metadata-action dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] --- .github/workflows/docker.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index 42afc31..975cba7 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -41,7 +41,7 @@ jobs: # https://github.com/docker/metadata-action - name: Extract Docker metadata id: meta - uses: docker/metadata-action@v5.0.0 + uses: docker/metadata-action@v5.4.0 with: images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} From f2ca3867dd2d1f0339a2b11a6b0c060ec14e2f23 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 4 Dec 2023 06:39:21 +0000 Subject: [PATCH 03/24] Bump golang.org/x/crypto from 0.14.0 to 0.16.0 Bumps [golang.org/x/crypto](https://github.com/golang/crypto) from 0.14.0 to 0.16.0. - [Commits](https://github.com/golang/crypto/compare/v0.14.0...v0.16.0) --- updated-dependencies: - dependency-name: golang.org/x/crypto dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] --- go.mod | 6 +++--- go.sum | 12 ++++++------ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/go.mod b/go.mod index 3d211a0..9aff556 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( github.com/joho/godotenv v1.5.1 github.com/kelseyhightower/envconfig v1.4.0 github.com/sirupsen/logrus v1.9.3 - golang.org/x/crypto v0.14.0 + golang.org/x/crypto v0.16.0 gorm.io/driver/sqlite v1.5.4 gorm.io/gorm v1.25.5 ) @@ -36,8 +36,8 @@ require ( github.com/ugorji/go/codec v1.2.11 // indirect golang.org/x/arch v0.3.0 // indirect golang.org/x/net v0.17.0 // indirect - golang.org/x/sys v0.13.0 // indirect - golang.org/x/text v0.13.0 // indirect + golang.org/x/sys v0.15.0 // indirect + golang.org/x/text v0.14.0 // indirect google.golang.org/protobuf v1.30.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 149258f..0347a1d 100644 --- a/go.sum +++ b/go.sum @@ -76,17 +76,17 @@ github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZ golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= golang.org/x/arch v0.3.0 h1:02VY4/ZcO/gBOH6PUaoiptASxtXU10jazRCP865E97k= golang.org/x/arch v0.3.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= -golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc= -golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4= +golang.org/x/crypto v0.16.0 h1:mMMrFzRSCF0GvB7Ne27XVtVAaXLrPmgPC7/v0tkwHaY= +golang.org/x/crypto v0.16.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM= golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE= golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= -golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k= -golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= +golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= +golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= From 2f6a548d940219f6b2d68b95cd19f6b9ba558a78 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 2 Jan 2024 16:21:24 +0000 Subject: [PATCH 04/24] Bump golang.org/x/crypto from 0.14.0 to 0.17.0 Bumps [golang.org/x/crypto](https://github.com/golang/crypto) from 0.14.0 to 0.17.0. - [Commits](https://github.com/golang/crypto/compare/v0.14.0...v0.17.0) --- updated-dependencies: - dependency-name: golang.org/x/crypto dependency-type: direct:production ... Signed-off-by: dependabot[bot] --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 9aff556..e5a31dd 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( github.com/joho/godotenv v1.5.1 github.com/kelseyhightower/envconfig v1.4.0 github.com/sirupsen/logrus v1.9.3 - golang.org/x/crypto v0.16.0 + golang.org/x/crypto v0.17.0 gorm.io/driver/sqlite v1.5.4 gorm.io/gorm v1.25.5 ) diff --git a/go.sum b/go.sum index 0347a1d..1d4ecc5 100644 --- a/go.sum +++ b/go.sum @@ -76,8 +76,8 @@ github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZ golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= golang.org/x/arch v0.3.0 h1:02VY4/ZcO/gBOH6PUaoiptASxtXU10jazRCP865E97k= golang.org/x/arch v0.3.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= -golang.org/x/crypto v0.16.0 h1:mMMrFzRSCF0GvB7Ne27XVtVAaXLrPmgPC7/v0tkwHaY= -golang.org/x/crypto v0.16.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= +golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k= +golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM= golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE= golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= From 7fc743abdb3354bb51a08754aa3eda751ebf9ecd Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 18 Dec 2023 06:17:35 +0000 Subject: [PATCH 05/24] Bump github/codeql-action from 2 to 3 Bumps [github/codeql-action](https://github.com/github/codeql-action) from 2 to 3. - [Release notes](https://github.com/github/codeql-action/releases) - [Changelog](https://github.com/github/codeql-action/blob/main/CHANGELOG.md) - [Commits](https://github.com/github/codeql-action/compare/v2...v3) --- updated-dependencies: - dependency-name: github/codeql-action dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] --- .github/workflows/codeql.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index 3e407da..085a3d8 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -23,15 +23,15 @@ jobs: # Initializes the CodeQL tools for scanning. - name: Initialize CodeQL - uses: github/codeql-action/init@v2 + uses: github/codeql-action/init@v3 with: languages: go # Autobuild attempts to build any compiled languages (C/C++, C#, Go, Java, or Swift). - name: Autobuild - uses: github/codeql-action/autobuild@v2 + uses: github/codeql-action/autobuild@v3 - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@v2 + uses: github/codeql-action/analyze@v3 with: category: "/language:go" From 5738476c6b03cae8e2e964cd88f10c161e81aff5 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 4 Dec 2023 06:39:12 +0000 Subject: [PATCH 06/24] Bump github.com/golang-jwt/jwt/v5 from 5.0.0 to 5.2.0 Bumps [github.com/golang-jwt/jwt/v5](https://github.com/golang-jwt/jwt) from 5.0.0 to 5.2.0. - [Release notes](https://github.com/golang-jwt/jwt/releases) - [Changelog](https://github.com/golang-jwt/jwt/blob/main/VERSION_HISTORY.md) - [Commits](https://github.com/golang-jwt/jwt/compare/v5.0.0...v5.2.0) --- updated-dependencies: - dependency-name: github.com/golang-jwt/jwt/v5 dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index e5a31dd..4f5d6ed 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.20 require ( github.com/gin-gonic/gin v1.9.1 - github.com/golang-jwt/jwt/v5 v5.0.0 + github.com/golang-jwt/jwt/v5 v5.2.0 github.com/joho/godotenv v1.5.1 github.com/kelseyhightower/envconfig v1.4.0 github.com/sirupsen/logrus v1.9.3 diff --git a/go.sum b/go.sum index 1d4ecc5..e385120 100644 --- a/go.sum +++ b/go.sum @@ -22,8 +22,8 @@ github.com/go-playground/validator/v10 v10.14.0 h1:vgvQWe3XCz3gIeFDm/HnTIbj6UGmg github.com/go-playground/validator/v10 v10.14.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU= 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/golang-jwt/jwt/v5 v5.0.0 h1:1n1XNM9hk7O9mnQoNBGolZvzebBQ7p93ULHRc28XJUE= -github.com/golang-jwt/jwt/v5 v5.0.0/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk= +github.com/golang-jwt/jwt/v5 v5.2.0 h1:d/ix8ftRUorsN+5eMIlF4T6J8CAt9rch3My2winC1Jw= +github.com/golang-jwt/jwt/v5 v5.2.0/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= From f0f8d4ff8d515a9a7970f4b4a22b68938ff5b147 Mon Sep 17 00:00:00 2001 From: Ivan Reshetnikov Date: Wed, 3 Jan 2024 17:15:49 +0500 Subject: [PATCH 07/24] style: group imports --- views/errors.go | 3 ++- views/groups.go | 5 +++-- views/index.go | 3 ++- views/links.go | 5 +++-- views/settings.go | 3 ++- 5 files changed, 12 insertions(+), 7 deletions(-) diff --git a/views/errors.go b/views/errors.go index 3265114..9a2512d 100644 --- a/views/errors.go +++ b/views/errors.go @@ -1,9 +1,10 @@ package views import ( + "net/http" + "github.com/gin-gonic/gin" "github.com/ordinary-dev/phoenix/config" - "net/http" ) func ShowError(ctx *gin.Context, cfg *config.Config, err error) { diff --git a/views/groups.go b/views/groups.go index 2584e0d..c1bda7a 100644 --- a/views/groups.go +++ b/views/groups.go @@ -2,12 +2,13 @@ package views import ( "fmt" + "net/http" + "strconv" + "github.com/gin-gonic/gin" "github.com/ordinary-dev/phoenix/config" "github.com/ordinary-dev/phoenix/database" "gorm.io/gorm" - "net/http" - "strconv" ) func CreateGroup(cfg *config.Config, db *gorm.DB) gin.HandlerFunc { diff --git a/views/index.go b/views/index.go index f027378..24ab1a7 100644 --- a/views/index.go +++ b/views/index.go @@ -1,11 +1,12 @@ package views import ( + "net/http" + "github.com/gin-gonic/gin" "github.com/ordinary-dev/phoenix/config" "github.com/ordinary-dev/phoenix/database" "gorm.io/gorm" - "net/http" ) func ShowMainPage(cfg *config.Config, db *gorm.DB) gin.HandlerFunc { diff --git a/views/links.go b/views/links.go index f1b1293..6293f74 100644 --- a/views/links.go +++ b/views/links.go @@ -2,12 +2,13 @@ package views import ( "fmt" + "net/http" + "strconv" + "github.com/gin-gonic/gin" "github.com/ordinary-dev/phoenix/config" "github.com/ordinary-dev/phoenix/database" "gorm.io/gorm" - "net/http" - "strconv" ) func CreateLink(cfg *config.Config, db *gorm.DB) gin.HandlerFunc { diff --git a/views/settings.go b/views/settings.go index bfe1642..205681d 100644 --- a/views/settings.go +++ b/views/settings.go @@ -1,11 +1,12 @@ package views import ( + "net/http" + "github.com/gin-gonic/gin" "github.com/ordinary-dev/phoenix/config" "github.com/ordinary-dev/phoenix/database" "gorm.io/gorm" - "net/http" ) func ShowSettings(cfg *config.Config, db *gorm.DB) gin.HandlerFunc { From 6d25c4e8af7c9ccd745ab41039119bb0f524b566 Mon Sep 17 00:00:00 2001 From: Ivan Reshetnikov Date: Mon, 25 Mar 2024 15:52:18 +0500 Subject: [PATCH 08/24] feat!: migrate to net/http With the release of Go 1.22, the standard library now has all the necessary functions that allow us to abandon Gin. I hope this rewrite will lower the entry barrier for new developers. As a nice bonus, the size of the program has decreased from 20 to 15.4 MB. To solve issue #81, request logging has been improved. Now all errors are displayed in the logs. --- config/main.go | 30 ++-- database/admins.go | 13 +- database/db.go | 13 +- go.mod | 26 +--- go.sum | 68 --------- jwttoken/token.go | 33 +++++ main.go | 20 +-- templates/auth.html.tmpl | 8 +- templates/error.html.tmpl | 4 +- templates/{ => fragments}/head.html.tmpl | 4 +- templates/index.html.tmpl | 2 +- templates/settings.html.tmpl | 13 +- views/auth.go | 176 ----------------------- views/errors.go | 15 -- views/groups.go | 71 --------- views/index.go | 30 ---- views/links.go | 91 ------------ views/main.go | 63 -------- views/middleware/auth.go | 82 +++++++++++ views/middleware/logging.go | 22 +++ views/middleware/security.go | 18 +++ views/pages/errors.go | 18 +++ views/pages/groups.go | 63 ++++++++ views/pages/index.go | 30 ++++ views/pages/links.go | 82 +++++++++++ views/pages/registration.go | 50 +++++++ views/pages/settings.go | 26 ++++ views/pages/signin.go | 43 ++++++ views/pages/templates.go | 70 +++++++++ views/render.go | 13 -- views/routes.go | 62 ++++++++ views/security.go | 14 -- views/settings.go | 30 ---- 33 files changed, 661 insertions(+), 642 deletions(-) create mode 100644 jwttoken/token.go rename templates/{ => fragments}/head.html.tmpl (90%) delete mode 100644 views/auth.go delete mode 100644 views/errors.go delete mode 100644 views/groups.go delete mode 100644 views/index.go delete mode 100644 views/links.go delete mode 100644 views/main.go create mode 100644 views/middleware/auth.go create mode 100644 views/middleware/logging.go create mode 100644 views/middleware/security.go create mode 100644 views/pages/errors.go create mode 100644 views/pages/groups.go create mode 100644 views/pages/index.go create mode 100644 views/pages/links.go create mode 100644 views/pages/registration.go create mode 100644 views/pages/settings.go create mode 100644 views/pages/signin.go create mode 100644 views/pages/templates.go delete mode 100644 views/render.go create mode 100644 views/routes.go delete mode 100644 views/security.go delete mode 100644 views/settings.go diff --git a/config/main.go b/config/main.go index be3e7e6..2d09e3b 100644 --- a/config/main.go +++ b/config/main.go @@ -6,18 +6,29 @@ import ( "github.com/sirupsen/logrus" ) +var Cfg Config + type Config struct { - SecretKey string `required:"true"` - DBPath string `required:"true"` - LogLevel string `default:"warning"` - EnableGinLogger bool `default:"false"` - Production bool `default:"true"` - HeaderAuth bool `default:"false"` + // A long and random secret string used for authorization. + SecretKey string `required:"true"` + // Path to the sqlite database. + DBPath string `required:"true"` + + LogLevel string `default:"warning"` + + // Allows you to skip authorization if the "Remote-User" header is specified. + // Don't use it if you don't know why you need it. + HeaderAuth bool `default:"false"` + + // Data for the first user. + // Optional, the site also allows you to create the first user. DefaultUsername string DefaultPassword string + // Controls the "secure" option for a token cookie. SecureCookie bool `default:"true"` + // Site title. Title string `default:"Phoenix"` // Any supported css value, embedded directly into every page. FontFamily string `default:"sans-serif"` @@ -29,13 +40,12 @@ func GetConfig() (*Config, error) { logrus.Infof("Config: %v", err) } - var cfg Config - err = envconfig.Process("p", &cfg) + err = envconfig.Process("p", &Cfg) if err != nil { return nil, err } - return &cfg, nil + return &Cfg, nil } func (cfg *Config) GetLogLevel() logrus.Level { @@ -44,7 +54,7 @@ func (cfg *Config) GetLogLevel() logrus.Level { return logrus.DebugLevel case "info": return logrus.InfoLevel - case "warning": + case "warning", "warn": return logrus.WarnLevel case "error": return logrus.ErrorLevel diff --git a/database/admins.go b/database/admins.go index b23e4ec..fd57d46 100644 --- a/database/admins.go +++ b/database/admins.go @@ -2,7 +2,6 @@ package database import ( "golang.org/x/crypto/bcrypt" - "gorm.io/gorm" ) type Admin struct { @@ -11,14 +10,14 @@ type Admin struct { Bcrypt string `gorm:"notNull"` } -func CountAdmins(db *gorm.DB) int64 { +func CountAdmins() int64 { var admins []Admin var count int64 - db.Model(&admins).Count(&count) + DB.Model(&admins).Count(&count) return count } -func CreateAdmin(db *gorm.DB, username string, password string) (Admin, error) { +func CreateAdmin(username string, password string) (Admin, error) { hash, err := bcrypt.GenerateFromPassword([]byte(password), 10) if err != nil { return Admin{}, err @@ -28,7 +27,7 @@ func CreateAdmin(db *gorm.DB, username string, password string) (Admin, error) { Username: username, Bcrypt: string(hash), } - result := db.Create(&admin) + result := DB.Create(&admin) if result.Error != nil { return Admin{}, result.Error @@ -37,9 +36,9 @@ func CreateAdmin(db *gorm.DB, username string, password string) (Admin, error) { return admin, nil } -func AuthorizeAdmin(db *gorm.DB, username string, password string) (Admin, error) { +func AuthorizeAdmin(username string, password string) (Admin, error) { var admin Admin - result := db.Where("username = ?", username).First(&admin) + result := DB.Where("username = ?", username).First(&admin) if result.Error != nil { return Admin{}, result.Error diff --git a/database/db.go b/database/db.go index 51303d5..22e551d 100644 --- a/database/db.go +++ b/database/db.go @@ -6,14 +6,17 @@ import ( "gorm.io/gorm" ) -func GetDatabaseConnection(cfg *config.Config) (*gorm.DB, error) { - db, err := gorm.Open(sqlite.Open(cfg.DBPath), &gorm.Config{}) +var DB *gorm.DB + +func EstablishDatabaseConnection(cfg *config.Config) error { + var err error + DB, err = gorm.Open(sqlite.Open(cfg.DBPath), &gorm.Config{}) if err != nil { - return nil, err + return err } // Migrate the schema - db.AutoMigrate(&Admin{}, &Group{}, &Link{}) + DB.AutoMigrate(&Admin{}, &Group{}, &Link{}) - return db, nil + return nil } diff --git a/go.mod b/go.mod index 4f5d6ed..8da8c4c 100644 --- a/go.mod +++ b/go.mod @@ -1,9 +1,8 @@ module github.com/ordinary-dev/phoenix -go 1.20 +go 1.22 require ( - github.com/gin-gonic/gin v1.9.1 github.com/golang-jwt/jwt/v5 v5.2.0 github.com/joho/godotenv v1.5.1 github.com/kelseyhightower/envconfig v1.4.0 @@ -14,30 +13,9 @@ require ( ) require ( - github.com/bytedance/sonic v1.9.1 // indirect - github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect - github.com/gabriel-vasile/mimetype v1.4.2 // indirect - github.com/gin-contrib/sse v0.1.0 // indirect - github.com/go-playground/locales v0.14.1 // indirect - github.com/go-playground/universal-translator v0.18.1 // indirect - github.com/go-playground/validator/v10 v10.14.0 // indirect - github.com/goccy/go-json v0.10.2 // indirect github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect - github.com/json-iterator/go v1.1.12 // indirect - github.com/klauspost/cpuid/v2 v2.2.4 // indirect - github.com/leodido/go-urn v1.2.4 // indirect - github.com/mattn/go-isatty v0.0.19 // indirect github.com/mattn/go-sqlite3 v1.14.17 // indirect - github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect - github.com/modern-go/reflect2 v1.0.2 // indirect - github.com/pelletier/go-toml/v2 v2.0.8 // indirect - github.com/twitchyliquid64/golang-asm v0.15.1 // indirect - github.com/ugorji/go/codec v1.2.11 // indirect - golang.org/x/arch v0.3.0 // indirect - golang.org/x/net v0.17.0 // indirect + github.com/stretchr/testify v1.8.3 // indirect golang.org/x/sys v0.15.0 // indirect - golang.org/x/text v0.14.0 // indirect - google.golang.org/protobuf v1.30.0 // indirect - gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index e385120..e52a1c9 100644 --- a/go.sum +++ b/go.sum @@ -1,98 +1,31 @@ -github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM= -github.com/bytedance/sonic v1.9.1 h1:6iJ6NqdoxCDr6mbY8h18oSO+cShGSMRGCEo7F2h0x8s= -github.com/bytedance/sonic v1.9.1/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U= -github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY= -github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 h1:qSGYFH7+jGhDF8vLC+iwCD4WpbV1EBDSzWkJODFLams= -github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 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/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU= -github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA= -github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= -github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= -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/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= -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= -github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= -github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= -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/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= -github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= github.com/golang-jwt/jwt/v5 v5.2.0 h1:d/ix8ftRUorsN+5eMIlF4T6J8CAt9rch3My2winC1Jw= github.com/golang-jwt/jwt/v5 v5.2.0/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk= -github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= -github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= -github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= -github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= -github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8= github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg= -github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= -github.com/klauspost/cpuid/v2 v2.2.4 h1:acbojRNwl3o09bUq+yDCtZFc1aiwaAAxtcn8YkZXnvk= -github.com/klauspost/cpuid/v2 v2.2.4/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8tyFgg4nqhuY= -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= -github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= -github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6YIM= github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= -github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= -github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= -github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= -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/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/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= -github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= -github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= -github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= -github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY= github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= -github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= -github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= -github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU= -github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= -golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= -golang.org/x/arch v0.3.0 h1:02VY4/ZcO/gBOH6PUaoiptASxtXU10jazRCP865E97k= -golang.org/x/arch v0.3.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k= golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= -golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM= -golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE= -golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= -golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= -google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng= -google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= @@ -101,4 +34,3 @@ gorm.io/driver/sqlite v1.5.4 h1:IqXwXi8M/ZlPzH/947tn5uik3aYQslP9BVveoax0nV0= gorm.io/driver/sqlite v1.5.4/go.mod h1:qxAuCol+2r6PannQDpOP1FP6ag3mKi4esLnB/jHed+4= gorm.io/gorm v1.25.5 h1:zR9lOiiYf09VNh5Q1gphfyia1JpiClIWG9hQaxB/mls= gorm.io/gorm v1.25.5/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8= -rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4= diff --git a/jwttoken/token.go b/jwttoken/token.go new file mode 100644 index 0000000..fa857c5 --- /dev/null +++ b/jwttoken/token.go @@ -0,0 +1,33 @@ +package jwttoken + +import ( + "net/http" + "time" + + "github.com/golang-jwt/jwt/v5" + + "github.com/ordinary-dev/phoenix/config" +) + +const ( + TOKEN_LIFETIME_IN_SECONDS = 60 * 60 * 24 * 30 + TOKEN_COOKIE_NAME = "phoenix-token" +) + +func GetJWTToken() (string, error) { + claims := jwt.RegisteredClaims{ + ExpiresAt: jwt.NewNumericDate(time.Now().Add(time.Second * TOKEN_LIFETIME_IN_SECONDS)), + } + token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) + return token.SignedString([]byte(config.Cfg.SecretKey)) +} + +func TokenToCookie(value string) *http.Cookie { + return &http.Cookie{ + Name: TOKEN_COOKIE_NAME, + Value: value, + HttpOnly: true, + Secure: config.Cfg.SecureCookie, + MaxAge: TOKEN_LIFETIME_IN_SECONDS, + } +} diff --git a/main.go b/main.go index e7239f5..4a1be63 100644 --- a/main.go +++ b/main.go @@ -25,21 +25,23 @@ func main() { logrus.Infof("Setting log level to %v", logLevel) // Connect to the database - db, err := database.GetDatabaseConnection(cfg) + err = database.EstablishDatabaseConnection(cfg) if err != nil { logrus.Fatalf("%v", err) } // Create the first user - if cfg.DefaultUsername != "" && cfg.DefaultPassword != "" { - if database.CountAdmins(db) < 1 { - _, err := database.CreateAdmin(db, cfg.DefaultUsername, cfg.DefaultPassword) - if err != nil { - logrus.Errorf("%v", err) - } + if cfg.DefaultUsername != "" && cfg.DefaultPassword != "" && database.CountAdmins() < 1 { + _, err := database.CreateAdmin(cfg.DefaultUsername, cfg.DefaultPassword) + if err != nil { + logrus.Errorf("%v", err) } } - engine := views.GetGinEngine(cfg, db) - engine.Run(":8080") + server, err := views.GetHttpServer() + if err != nil { + logrus.Fatal(err) + } + + server.ListenAndServe() } diff --git a/templates/auth.html.tmpl b/templates/auth.html.tmpl index bd0163c..021e7ea 100644 --- a/templates/auth.html.tmpl +++ b/templates/auth.html.tmpl @@ -1,14 +1,14 @@ - {{template "head" .}} + {{ template "head" . }}
-
-

{{.title}}

-

{{.description}}

+ +

{{ .title }}

+

{{ .description }}

- {{template "head" .}} + {{ template "head" . }}

Error

- {{.error}} + {{ .error }}

Main page

diff --git a/templates/head.html.tmpl b/templates/fragments/head.html.tmpl similarity index 90% rename from templates/head.html.tmpl rename to templates/fragments/head.html.tmpl index 3487400..f05e657 100644 --- a/templates/head.html.tmpl +++ b/templates/fragments/head.html.tmpl @@ -1,5 +1,5 @@ {{define "head"}} -{{.WebsiteTitle}} +{{ .title }} @@ -10,7 +10,7 @@ {{end}} diff --git a/templates/index.html.tmpl b/templates/index.html.tmpl index e3550ab..f3bf377 100644 --- a/templates/index.html.tmpl +++ b/templates/index.html.tmpl @@ -6,7 +6,7 @@
-

{{.WebsiteTitle}}

+

{{ .title }}

{{if not .groups}}

You don't have any links. diff --git a/templates/settings.html.tmpl b/templates/settings.html.tmpl index c2cf34c..b096583 100644 --- a/templates/settings.html.tmpl +++ b/templates/settings.html.tmpl @@ -11,7 +11,7 @@ {{range .groups}}

Group "{{.Name}}"

- + -
+
-
+ +
+ + diff --git a/templates/settings.html.tmpl b/templates/settings.html.tmpl index b096583..4fcedf9 100644 --- a/templates/settings.html.tmpl +++ b/templates/settings.html.tmpl @@ -6,7 +6,18 @@

Settings

- Main page + + {{range .groups}}

Group "{{.Name}}"

diff --git a/views/pages/export.go b/views/pages/export.go new file mode 100644 index 0000000..e199917 --- /dev/null +++ b/views/pages/export.go @@ -0,0 +1,29 @@ +package pages + +import ( + "encoding/json" + "net/http" + + "github.com/ordinary-dev/phoenix/database" +) + +type ExportFile struct { + Groups []database.Group `json:"groups"` +} + +func Export(w http.ResponseWriter, _ *http.Request) { + groups, err := database.GetGroupsWithLinks() + if err != nil { + ShowError(w, http.StatusInternalServerError, err) + return + } + + w.Header().Set("Content-Type", "application/json") + w.Header().Set("Content-Disposition", "attachment; filename=phoenix.json") + w.WriteHeader(http.StatusOK) + + enc := json.NewEncoder(w) + enc.Encode(&ExportFile{ + Groups: groups, + }) +} diff --git a/views/pages/import.go b/views/pages/import.go new file mode 100644 index 0000000..8d19367 --- /dev/null +++ b/views/pages/import.go @@ -0,0 +1,38 @@ +package pages + +import ( + "encoding/json" + "net/http" + + "github.com/ordinary-dev/phoenix/database" +) + +func ImportPage(w http.ResponseWriter, _ *http.Request) { + Render("import.html.tmpl", w, map[string]any{}) +} + +func Import(w http.ResponseWriter, r *http.Request) { + var exportFile ExportFile + data := []byte(r.FormValue("exportFile")) + if err := json.Unmarshal(data, &exportFile); err != nil { + ShowError(w, http.StatusBadRequest, err) + return + } + + for _, g := range exportFile.Groups { + if err := database.CreateGroup(&g); err != nil { + ShowError(w, http.StatusInternalServerError, err) + return + } + + for _, l := range g.Links { + l.GroupID = g.ID + if err := database.CreateLink(&l); err != nil { + ShowError(w, http.StatusInternalServerError, err) + return + } + } + } + + http.Redirect(w, r, "/", http.StatusFound) +} diff --git a/views/routes.go b/views/routes.go index 768ab53..e51a882 100644 --- a/views/routes.go +++ b/views/routes.go @@ -53,6 +53,11 @@ func GetHttpServer() (*http.Server, error) { // Delete link. protectedMux.HandleFunc("POST /links/{id}/delete", pages.DeleteLink) + // Import-export + protectedMux.HandleFunc("GET /export", pages.Export) + protectedMux.HandleFunc("GET /import", pages.ImportPage) + protectedMux.HandleFunc("POST /import", pages.Import) + return &http.Server{ Addr: ":8080", Handler: middleware.LoggingMiddleware( From 5aa439614851e69739552bcfc75bef9f81202e12 Mon Sep 17 00:00:00 2001 From: Ivan Reshetnikov Date: Mon, 1 Apr 2024 23:49:41 +0500 Subject: [PATCH 21/24] docs: update feature list --- readme.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/readme.md b/readme.md index 273e43a..d5f12ba 100644 --- a/readme.md +++ b/readme.md @@ -11,6 +11,8 @@ Self-hosted start page without the extra stuff. - Authorization support - SSO via Trusted Header Auth (_Reverse Proxy_) - Font Awesome integration +- Multiple styles +- Export and import ## Configuration Service settings can be set through environment variables. From c8f8a7feb415f2b7d3f23cc76d62f2be4055ec24 Mon Sep 17 00:00:00 2001 From: Ivan Reshetnikov Date: Mon, 1 Apr 2024 23:55:43 +0500 Subject: [PATCH 22/24] chore: log rendering errors --- views/pages/index.go | 6 +----- views/pages/signin.go | 7 +------ views/pages/templates.go | 7 +++++-- 3 files changed, 7 insertions(+), 13 deletions(-) diff --git a/views/pages/index.go b/views/pages/index.go index 25320eb..356e1ed 100644 --- a/views/pages/index.go +++ b/views/pages/index.go @@ -5,7 +5,6 @@ import ( "time" "github.com/ordinary-dev/phoenix/database" - log "github.com/sirupsen/logrus" ) func ShowMainPage(w http.ResponseWriter, r *http.Request) { @@ -38,12 +37,9 @@ func ShowMainPage(w http.ResponseWriter, r *http.Request) { style = "list" } - err = Render("index.html.tmpl", w, map[string]any{ + Render("index.html.tmpl", w, map[string]any{ "description": "Self-hosted start page.", "groups": groups, "style": style, }) - if err != nil { - log.Error(err) - } } diff --git a/views/pages/signin.go b/views/pages/signin.go index b02484e..f7cc4e0 100644 --- a/views/pages/signin.go +++ b/views/pages/signin.go @@ -4,22 +4,17 @@ import ( "net/http" "strings" - log "github.com/sirupsen/logrus" - "github.com/ordinary-dev/phoenix/database" "github.com/ordinary-dev/phoenix/jwttoken" ) func ShowSignInForm(w http.ResponseWriter, _ *http.Request) { - err := Render("auth.html.tmpl", w, map[string]any{ + Render("auth.html.tmpl", w, map[string]any{ "title": "Sign in", "description": "Authorization is required to view this page.", "button": "Sign in", "formAction": "/signin", }) - if err != nil { - log.Error(err) - } } func AuthorizeUser(w http.ResponseWriter, r *http.Request) { diff --git a/views/pages/templates.go b/views/pages/templates.go index 575b3e1..9a05c72 100644 --- a/views/pages/templates.go +++ b/views/pages/templates.go @@ -59,12 +59,15 @@ func LoadTemplates() error { return nil } -func Render(template string, wr io.Writer, data map[string]any) error { +func Render(template string, wr io.Writer, data map[string]any) { data["fontFamily"] = config.Cfg.FontFamily if _, ok := data["title"]; !ok { data["title"] = config.Cfg.Title } - return templates[template].Execute(wr, data) + err := templates[template].Execute(wr, data) + if err != nil { + log.WithField("template", template).Error(err) + } } From 4104b04f17bc6b110e9d0062c17f8a39a8fc0a3a Mon Sep 17 00:00:00 2001 From: Ivan Date: Tue, 2 Apr 2024 18:51:43 +0500 Subject: [PATCH 23/24] chore(docker): return to alpine, add dockerignore Alpine builds are working again. Apparently, we removed the dependency on the non-working library. --- .dockerignore | 5 +++++ Dockerfile | 7 +++---- 2 files changed, 8 insertions(+), 4 deletions(-) create mode 100644 .dockerignore diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..7984597 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,5 @@ +*~ +*.db +*.bak +*.sqlite3 +**/.DS_Store diff --git a/Dockerfile b/Dockerfile index e245d9c..1e24efe 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,6 @@ -FROM golang:1.22 AS builder +FROM golang:1.22-alpine AS builder -RUN apt install -y --no-install-recommends gcc +RUN apk add --no-cache gcc libc-dev WORKDIR /app @@ -11,7 +11,7 @@ ADD . . RUN go build -o main -FROM debian:bookworm-slim +FROM alpine:3.19 WORKDIR /app COPY --from=builder /app/main /usr/local/bin/phoenix @@ -20,7 +20,6 @@ COPY templates ./templates RUN mkdir /var/lib/phoenix ENV P_DBPATH="/var/lib/phoenix/db.sqlite3" -ENV P_PRODUCTION="true" EXPOSE 8080 From 07590d3891e294b62902953bc1c237122bed110d Mon Sep 17 00:00:00 2001 From: Ivan Date: Tue, 2 Apr 2024 18:53:31 +0500 Subject: [PATCH 24/24] fix(main-page): improve message style --- templates/index.html.tmpl | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/templates/index.html.tmpl b/templates/index.html.tmpl index 403d109..87728b0 100644 --- a/templates/index.html.tmpl +++ b/templates/index.html.tmpl @@ -8,18 +8,18 @@

{{ .title }}

+ + {{ if not .groups }} +

+ You don't have any links.
+ Go to settings and create one. +

+ {{ end }}
- {{if not .groups}} -

- You don't have any links. - Go to settings and create one. -

- {{else}} Settings - {{end}} {{ if ne .style "list" }}