Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve code #20

Merged
merged 20 commits into from
Aug 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (

_ "github.com/houseme/url-shortenter/app/api/internal/logic"

"github.com/houseme/url-shortenter/internal/tracing"
"github.com/houseme/url-shortenter/utility/env"
"github.com/houseme/url-shortenter/utility/tracing"

"github.com/houseme/url-shortenter/app/api/internal/cmd"
)
Expand Down
29 changes: 29 additions & 0 deletions app/console/api/v1/account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package v1

import (
"github.com/gogf/gf/v2/frame/g"

"github.com/houseme/url-shortenter/app/console/internal/model"
)

// CreateAccountReq is the request struct for the CreateAccount endpoint.
type CreateAccountReq struct {
g.Meta `path:"/account" tags:"Account Service" method:"Post" summary:"create an account"`
*model.CreateAccountInput
}

// CreateAccountRes is the response struct for the CreateAccount endpoint.
type CreateAccountRes struct {
*model.CreateAccountOutput
}

// ModifyAccountReq is the request struct for the ModifyAccount endpoint.
type ModifyAccountReq struct {
g.Meta `path:"/account/:account/update" tags:"Account Service" method:"Post" summary:"You say, I modify an account"`
*model.ModifyAccountInput
}

// ModifyAccountRes is the response struct for the ModifyAccount endpoint.
type ModifyAccountRes struct {
*model.ModifyAccountOutput
}
29 changes: 29 additions & 0 deletions app/console/api/v1/auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package v1

import (
"github.com/gogf/gf/v2/frame/g"

"github.com/houseme/url-shortenter/app/console/internal/model"
)

// CreateAccessTokenReq is the request struct for the CreateAccessToken endpoint.
type CreateAccessTokenReq struct {
g.Meta `path:"/auth/access_token" tags:"Account Service" method:"Get" summary:"create an access token"`
*model.CreateAccessTokenInput
}

// CreateAccessTokenRes is the response struct for the CreateAccessToken endpoint.
type CreateAccessTokenRes struct {
*model.CreateAccessTokenOutput
}

// AuthReq is the request struct for the Auth endpoint.
type AuthReq struct {
g.Meta `path:"/auth/authorization" tags:"Account Service" method:"Post" summary:"authorization"`
*model.AuthInput
}

// AuthRes is the response struct for the Auth endpoint.
type AuthRes struct {
*model.AuthOutput
}
73 changes: 73 additions & 0 deletions app/console/api/v1/domain.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package v1

import (
"github.com/gogf/gf/v2/frame/g"

"github.com/houseme/url-shortenter/app/console/internal/model"
)

// DomainAuthorizationReq is the request for DomainAuthorization
type DomainAuthorizationReq struct {
g.Meta `path:"/domain/create" tags:"Domain Service" method:"Post" summary:"create an domain authorization"`
*model.DomainAuthorizationInput
}

// DomainAuthorizationRes is the response for DomainAuthorization
type DomainAuthorizationRes struct {
*model.DomainAuthorizationOutput
}

// DomainAuthorizationListReq is the request for DomainAuthorizationList
type DomainAuthorizationListReq struct {
g.Meta `path:"/domain/list" tags:"Domain Service" method:"Get" summary:"list domain authorization"`
*model.DomainAuthorizationListInput
}

// DomainAuthorizationListRes is the response for DomainAuthorizationList
type DomainAuthorizationListRes struct {
*model.DomainAuthorizationListOutput
}

// DomainAuthorizationDeleteReq is the request for DomainAuthorizationDelete
type DomainAuthorizationDeleteReq struct {
g.Meta `path:"/domain/delete" tags:"Domain Service" method:"Post" summary:"delete domain authorization"`
*model.DomainAuthorizationDeleteInput
}

// DomainAuthorizationDeleteRes is the response for DomainAuthorizationDelete
type DomainAuthorizationDeleteRes struct {
*model.DomainAuthorizationDeleteOutput
}

// DomainAuthorizationUpdateReq is the request for DomainAuthorizationUpdate
type DomainAuthorizationUpdateReq struct {
g.Meta `path:"/domain/update" tags:"Domain Service" method:"Post" summary:"update domain authorization"`
*model.DomainAuthorizationUpdateInput
}

// DomainAuthorizationUpdateRes is the response for DomainAuthorizationUpdate
type DomainAuthorizationUpdateRes struct {
*model.DomainAuthorizationUpdateOutput
}

// QueryDomainAuthorizationReq is the request for QueryDomainAuthorization
type QueryDomainAuthorizationReq struct {
g.Meta `path:"/domain/query" tags:"Domain Service" method:"Get" summary:"query domain authorization"`
*model.QueryDomainAuthorizationInput
}

// QueryDomainAuthorizationRes is the response for QueryDomainAuthorization
type QueryDomainAuthorizationRes struct {
*model.QueryDomainAuthorizationOutput
}

// DomainAuthorizationDetailReq is the request for DomainAuthorizationDetail
type DomainAuthorizationDetailReq struct {
g.Meta `path:"/domain/detail" tags:"Domain Service" method:"Get" summary:"detail domain authorization"`
*model.DomainAuthorizationDetailInput
}

// DomainAuthorizationDetailRes is the response for DomainAuthorizationDetail
type DomainAuthorizationDetailRes struct {
*model.DomainAuthorizationDetailOutput
}
2 changes: 2 additions & 0 deletions app/console/api/v1/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import (
"github.com/gogf/gf/v2/frame/g"
)

// EchoSayReq is the request struct for the EchoSay endpoint.
type EchoSayReq struct {
g.Meta `path:"/echo/say" tags:"Echo Service" method:"get" summary:"You say, I echo"`
Content string `v:"required" dc:"Say something?"`
}

// EchoSayRes is the response struct for the EchoSay endpoint.
type EchoSayRes struct {
Content string `dc:"Reply content"`
}
51 changes: 51 additions & 0 deletions app/console/api/v1/short.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package v1

import (
"github.com/gogf/gf/v2/frame/g"

"github.com/houseme/url-shortenter/app/console/internal/model"
)

// CreateShortReq is the request struct for the CreateShort endpoint.
type CreateShortReq struct {
g.Meta `path:"/url" tags:"Account Service" method:"Post" summary:"create a short url"`
*model.CreateShortInput
}

// CreateShortRes is the response struct for the CreateShort endpoint.
type CreateShortRes struct {
*model.CreateShortOutput
}

// QueryShortReq is the request struct for the QueryShort endpoint.
type QueryShortReq struct {
g.Meta `path:"/url/:shortUrl" tags:"Account Service" method:"Get" summary:"query a short url"`
*model.QueryShortInput
}

// QueryShortRes is the response struct for the QueryShort endpoint.
type QueryShortRes struct {
*model.QueryShortOutput
}

// ModifyShortReq is the request struct for the ModifyShort endpoint.
type ModifyShortReq struct {
g.Meta `path:"/url/:shortUrl/change_state" tags:"Account Service" method:"Post" summary:"modify a short url"`
*model.ModifyShortInput
}

// ModifyShortRes is the response struct for the ModifyShort endpoint.
type ModifyShortRes struct {
*model.ModifyShortOutput
}

// QueryStatReq is the request struct for the QueryStat endpoint.
type QueryStatReq struct {
g.Meta `path:"/url/:shortUrl/stat" tags:"Account Service" method:"Get" summary:"query a short url stat"`
*model.QueryStatInput
}

// QueryStatRes is the response struct for the QueryStat endpoint.
type QueryStatRes struct {
*model.QueryStatOutput
}
52 changes: 51 additions & 1 deletion app/console/internal/consts/consts.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,55 @@
package consts

const (
AppId = `svr-template`
// AppID 应用ID
AppID = `console`

// AppDefaultLoggerName 应用默认日志名称
AppDefaultLoggerName = `console`

// AuthorizationHeaderKey 授权头
AuthorizationHeaderKey = "authorization"
// AuthorizationTypeBearer 授权类型
AuthorizationTypeBearer = "Bearer"

// AccountLevelPlatform 超级管理员 权限级别 0默认平台超级管理员;1000商家管理员;10000商家员工
AccountLevelPlatform = 0
// AccountLevelBusiness 商家管理员
AccountLevelBusiness = 1000
// AccountLevelBusinessEmployee 商家员工
AccountLevelBusinessEmployee = 10000

// UserStateDefault 用户状态 0默认,100正常,200失效
UserStateDefault = 0
// UserStateNormal 正常
UserStateNormal = 100
// UserStateInvalid 失效
UserStateInvalid = 200

// AuthSecretStateDefault 授权密钥状态 0 默认 100正常 200禁用
AuthSecretStateDefault = 0
// AuthSecretStateNormal 正常
AuthSecretStateNormal = 100
// AuthSecretStateInvalid 失效
AuthSecretStateInvalid = 200

// AuthTypeAPIKey 授权类型 ApiKey授权类型 账户密码授权类型
AuthTypeAPIKey = "api_key"
// AuthTypePassword 授权类型 账户密码授权类型
AuthTypePassword = "password"

// APIKeyExpireTime ApiKey授权有效期时间 单位秒
APIKeyExpireTime = 7200

// PasswordExpireTime 账号密码授权有效期时间 单位秒
PasswordExpireTime = 7200

// AccessTokenExpireTime 访问令牌有效期时间 单位秒
AccessTokenExpireTime = 7200

// RefreshTokenExpireTime 刷新令牌有效期时间 单位秒
RefreshTokenExpireTime = 864000

// TokenExpireTime 有效期时间 单位秒
TokenExpireTime = 864000
)
61 changes: 61 additions & 0 deletions app/console/internal/controller/account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package controller

import (
"context"

"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/gtrace"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"

v1 "github.com/houseme/url-shortenter/app/console/api/v1"
"github.com/houseme/url-shortenter/app/console/internal/service"
"github.com/houseme/url-shortenter/utility"
)

type cAccount struct {
}

// Account is the handler for Account
var Account = cAccount{}

// CreateAccount is the handler for CreateAccount
func (c *cAccount) CreateAccount(ctx context.Context, req *v1.CreateAccountReq) (res *v1.CreateAccountRes, err error) {
ctx, span := gtrace.NewSpan(ctx, "tracing-controller-account-CreateAccount")
defer span.End()

var logger = utility.Helper().Logger(ctx)

defer func() {
if err != nil {
g.Log(logger).Error(ctx, "account-CreateAccount err:", err)
span.RecordError(err, trace.WithAttributes(attribute.String("account-CreateAccount-err", err.Error())))
}
}()
res = &v1.CreateAccountRes{}
if res.CreateAccountOutput, err = service.Account().CreateAccount(ctx, req.CreateAccountInput); err != nil {
err = gerror.Wrap(err, "account-CreateAccount err:")
}
return
}

// ModifyAccount is the handler for ModifyAccount
func (c *cAccount) ModifyAccount(ctx context.Context, req *v1.ModifyAccountReq) (res *v1.ModifyAccountRes, err error) {
ctx, span := gtrace.NewSpan(ctx, "tracing-controller-account-ModifyAccount")
defer span.End()

var logger = utility.Helper().Logger(ctx)

defer func() {
if err != nil {
g.Log(logger).Error(ctx, "account-ModifyAccount err:", err)
span.RecordError(err, trace.WithAttributes(attribute.String("account-ModifyAccount-err", err.Error())))
}
}()
res = &v1.ModifyAccountRes{}
if res.ModifyAccountOutput, err = service.Account().ModifyAccount(ctx, req.ModifyAccountInput); err != nil {
err = gerror.Wrap(err, "account-ModifyAccount err:")
}
return
}
61 changes: 61 additions & 0 deletions app/console/internal/controller/auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package controller

import (
"context"

"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/gtrace"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"

v1 "github.com/houseme/url-shortenter/app/console/api/v1"
"github.com/houseme/url-shortenter/app/console/internal/service"
"github.com/houseme/url-shortenter/utility"
)

type cAuth struct {
}

// Auth is the handler for Auth
var Auth = cAuth{}

// CreateAccessToken is the handler for CreateAccessToken
func (c *cAuth) CreateAccessToken(ctx context.Context, req *v1.CreateAccessTokenReq) (res *v1.CreateAccessTokenRes, err error) {
ctx, span := gtrace.NewSpan(ctx, "tracing-controller-auth-CreateAccessToken")
defer span.End()

var logger = utility.Helper().Logger(ctx)

defer func() {
if err != nil {
g.Log(logger).Error(ctx, "auth-CreateAccessToken err:", err)
span.RecordError(err, trace.WithAttributes(attribute.String("auth-CreateAccessToken-err", err.Error())))
}
}()
res = &v1.CreateAccessTokenRes{}
if res.CreateAccessTokenOutput, err = service.Auth().CreateAccessToken(ctx, req.CreateAccessTokenInput); err != nil {
err = gerror.Wrap(err, "auth-CreateAccessToken failed")
}
return
}

// Authorization is the handler for Authorization
func (c *cAuth) Authorization(ctx context.Context, req *v1.AuthReq) (res *v1.AuthRes, err error) {
ctx, span := gtrace.NewSpan(ctx, "tracing-controller-auth-authorization")
defer span.End()

var logger = utility.Helper().Logger(ctx)

defer func() {
if err != nil {
g.Log(logger).Error(ctx, "auth-authorization err:", err)
span.RecordError(err, trace.WithAttributes(attribute.String("auth-authorization-err", err.Error())))
}
}()
res = &v1.AuthRes{}
if res.AuthOutput, err = service.Auth().Authorization(ctx, req.AuthInput); err != nil {
err = gerror.Wrap(err, "auth-authorization failed")
}
return
}
Loading