Renamed functions from context

This commit is contained in:
perp 2024-06-06 19:18:46 +01:00
parent 132ad2b9cf
commit 846bbbcb29
4 changed files with 7 additions and 123 deletions

View File

@ -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

View File

@ -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

View File

@ -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))
}

View File

@ -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))
}
}