From 846bbbcb291b97e26f7f7e1b02ca9ef5bb2c3a4d Mon Sep 17 00:00:00 2001 From: perp Date: Thu, 6 Jun 2024 19:18:46 +0100 Subject: [PATCH] Renamed functions from context --- internal/router/api/v1/account/login.go | 25 +------------- internal/router/api/v1/account/register.go | 31 +++-------------- internal/router/api/v1/spec.go | 40 ---------------------- internal/router/api/v1/v1.go | 34 ++---------------- 4 files changed, 7 insertions(+), 123 deletions(-) delete mode 100644 internal/router/api/v1/spec.go diff --git a/internal/router/api/v1/account/login.go b/internal/router/api/v1/account/login.go index 9243740..cd7399e 100644 --- a/internal/router/api/v1/account/login.go +++ b/internal/router/api/v1/account/login.go @@ -1,17 +1,3 @@ -// Copyright 2024 perp (supernets) -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - package account import ( @@ -21,15 +7,6 @@ import ( "golang.org/x/crypto/bcrypt" ) -// @summary Account login -// @description Login to an account -// @tags account -// @accept json -// @produce json -// @param register body v1.Register true "alice" "supersecretpassword" -// @success 200 {object} v1.Token -// @response default {object} v1.Error "There was an error" -// @router /v1/account/login [post] func Login(ctx *context.Context) { // Store body var body *v1.Register @@ -42,7 +19,7 @@ func Login(ctx *context.Context) { } // Select account by username - account, err := ctx.Db.Account.SelectByUsername(body.Username) + account, err := ctx.Database.Account.SelectByUsername(body.Username) if err != nil { ctx.Error(500, "DatabaseError") return diff --git a/internal/router/api/v1/account/register.go b/internal/router/api/v1/account/register.go index a526351..0adee89 100644 --- a/internal/router/api/v1/account/register.go +++ b/internal/router/api/v1/account/register.go @@ -1,17 +1,3 @@ -// Copyright 2024 perp (supernets) -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - package account import ( @@ -21,18 +7,9 @@ import ( "golang.org/x/crypto/bcrypt" ) -// @summary Account registration -// @description Register an account -// @tags account -// @accept json -// @produce json -// @param register body v1.Register true "alice" "supersecretpassword" -// @success 200 {object} v1.Token -// @response default {object} v1.Error "There was an error" -// @router /v1/account/register [post] func Register(ctx *context.Context) { // Check if registration is disabled - if ctx.Config.Auth.Disabled { + if ctx.Config.Auth.Register { ctx.Error(403, "RegistrationDisabled") return } @@ -48,7 +25,7 @@ func Register(ctx *context.Context) { } // Select account by username - account, err := ctx.Db.Account.SelectByUsername(body.Username) + account, err := ctx.Database.Account.SelectByUsername(body.Username) if err != nil { ctx.Error(500, "DatabaseError") return @@ -68,14 +45,14 @@ func Register(ctx *context.Context) { } // Insert account - err = ctx.Db.Account.Insert(body.Username, string(password)) + err = ctx.Database.Account.Insert(body.Username, string(password)) if err != nil { ctx.Error(500, "DatabaseError") return } // Select account by username - account, err = ctx.Db.Account.SelectByUsername(body.Username) + account, err = ctx.Database.Account.SelectByUsername(body.Username) if err != nil { ctx.Error(500, "DatabaseError") return diff --git a/internal/router/api/v1/spec.go b/internal/router/api/v1/spec.go deleted file mode 100644 index 72e0d30..0000000 --- a/internal/router/api/v1/spec.go +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright 2024 perp (supernets) -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package v1 - -import ( - "git.supernets.org/perp/gopay/internal/context" - "github.com/MarceloPetrucio/go-scalar-api-reference" - "github.com/rs/zerolog/log" -) - -// Handle spec reference -func Spec(ctx *context.Context) { - // Parse documentation - content, err := scalar.ApiReferenceHTML(&scalar.Options{ - SpecURL: "docs/v1_swagger.json", - CustomOptions: scalar.CustomOptions{ - PageTitle: "GoPay API documentation", - }, - HideDownloadButton: true, - DarkMode: true, - }) - if err != nil { - log.Err(err).Msg("Could not parse swagger with scalar") - return - } - - ctx.Data(200, "text/html; charset=utf-8", []byte(content)) -} diff --git a/internal/router/api/v1/v1.go b/internal/router/api/v1/v1.go index 15011c4..28aab24 100644 --- a/internal/router/api/v1/v1.go +++ b/internal/router/api/v1/v1.go @@ -1,17 +1,3 @@ -// Copyright 2024 perp (supernets) -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - package v1 import ( @@ -20,28 +6,12 @@ import ( "github.com/gin-gonic/gin" ) -// @title GoPay API v1 -// @version 1.0 -// @description GoPay is a cryptocurrency payment processor made in Go - -// @contact.name repository -// @contact.url https://git.supernets.org/perp/gopay - -// @license.name Apache 2.0 -// @license.url http://www.apache.org/licenses/LICENSE-2.0.html - // Register v1 routes func Register(ctx *context.Context, engine *gin.Engine) { v1 := engine.Group("v1") - - { - v1.GET("scalar", ctx.Route(Spec)) - v1.GET("docs", ctx.Route(Spec)) - } - { a := v1.Group("account") - a.POST("login", ctx.Route(account.Login)) - a.POST("register", ctx.Route(account.Register)) + a.POST("login", ctx.Register(account.Login)) + a.POST("register", ctx.Register(account.Register)) } }