add mongodb/jwt

This commit is contained in:
landaiqing
2024-11-13 13:37:47 +08:00
parent ae743ba8a6
commit c95d5fc041
85 changed files with 3370 additions and 239 deletions

View File

@@ -1,69 +0,0 @@
syntax = "v1"
info (
title: "鉴权微服务"
desc: "鉴权微服务"
author: "landaiqing"
email: "landaiqing@126.com"
version: "v1.0.0"
)
// 登录请求参数
type (
// 账户登录请求参数
AccountLoginRequest {
Account string `json:"account"`
Password string `json:"password"`
AutoLogin bool `json:"auto_login"`
Angle int64 `json:"angle"`
Key string `json:"key"`
}
// 手机号登录请求参数
PhoneLoginRequest {
Phone string `json:"phone"`
Captcha string `json:"captcha"`
AutoLogin bool `json:"auto_login"`
}
// 重置密码请求参数
ResetPasswordRequest {
Phone string `json:"phone"`
Captcha string `json:"captcha"`
Password string `json:"password"`
Repassword string `json:"repassword"`
}
// 登录响应参数
LoginResponse {
AccessToken string `json:"access_token"`
UID string `json:"uid"`
Username string `json:"username,optional"`
Nickname string `json:"nickname"`
Avatar string `json:"avatar"`
Status int64 `json:"status"`
}
)
// 用户服务
@server (
group: user // 微服务分组
prefix: /api/auth/user // 微服务前缀
timeout: 10s // 超时时间
maxBytes: 1048576 // 最大请求大小
signature: true // 是否开启签名验证
middleware: SecurityHeadersMiddleware // 注册中间件
MaxConns: true // 是否开启最大连接数限制
Recover: true // 是否开启自动恢复
)
service auth {
// 账户登录
@handler accountLogin
post /login (AccountLoginRequest) returns (LoginResponse)
// 手机号登录
@handler phoneLogin
post /phone_login (PhoneLoginRequest) returns (LoginResponse)
// 重置密码
@handler resetPassword
post /reset_password (ResetPasswordRequest) returns (string)
}

View File

@@ -1,33 +0,0 @@
package main
import (
"flag"
"fmt"
"schisandra-album-cloud-microservices/app/auth/internal/config"
"schisandra-album-cloud-microservices/app/auth/internal/handler"
"schisandra-album-cloud-microservices/app/auth/internal/svc"
"schisandra-album-cloud-microservices/common/middleware"
"github.com/zeromicro/go-zero/core/conf"
"github.com/zeromicro/go-zero/rest"
)
var configFile = flag.String("f", "etc/auth.yaml", "the config file")
func main() {
flag.Parse()
var c config.Config
conf.MustLoad(*configFile, &c)
server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(middleware.CORSMiddleware(), nil, "*"))
defer server.Stop()
// i18n middleware
server.Use(middleware.I18nMiddleware)
ctx := svc.NewServiceContext(c)
handler.RegisterHandlers(server, ctx)
fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
server.Start()
}

View File

@@ -1,8 +0,0 @@
Name: auth
Host: 0.0.0.0
Port: 8888
Mysql:
DataSource: root:1611@tcp(localhost:3306)/schisandra-cloud-album?charset=utf8mb4&parseTime=True&loc=Local
Auth:
AccessSecret: uOvKLmVfztaXGpNYd4Z0I1SiT7MweJhl
AccessExpire: 86400

View File

@@ -1,14 +0,0 @@
package config
import "github.com/zeromicro/go-zero/rest"
type Config struct {
rest.RestConf
Auth struct {
AccessSecret string
AccessExpire int64
}
Mysql struct {
DataSource string
}
}

View File

@@ -1,43 +0,0 @@
// Code generated by goctl. DO NOT EDIT.
// goctl 1.7.3
package handler
import (
"net/http"
"time"
user "schisandra-album-cloud-microservices/app/auth/internal/handler/user"
"schisandra-album-cloud-microservices/app/auth/internal/svc"
"github.com/zeromicro/go-zero/rest"
)
func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
server.AddRoutes(
rest.WithMiddlewares(
[]rest.Middleware{serverCtx.SecurityHeadersMiddleware},
[]rest.Route{
{
Method: http.MethodPost,
Path: "/login",
Handler: user.AccountLoginHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/phone_login",
Handler: user.PhoneLoginHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/reset_password",
Handler: user.ResetPasswordHandler(serverCtx),
},
}...,
),
rest.WithSignature(serverCtx.Config.Signature),
rest.WithPrefix("/api/auth/user"),
rest.WithTimeout(10000*time.Millisecond),
rest.WithMaxBytes(1048576),
)
}

View File

@@ -1,28 +0,0 @@
package user
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"schisandra-album-cloud-microservices/app/auth/internal/logic/user"
"schisandra-album-cloud-microservices/app/auth/internal/svc"
"schisandra-album-cloud-microservices/app/auth/internal/types"
)
func AccountLoginHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.AccountLoginRequest
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := user.NewAccountLoginLogic(r.Context(), svcCtx)
resp, err := l.AccountLogin(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}

View File

@@ -1,28 +0,0 @@
package user
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"schisandra-album-cloud-microservices/app/auth/internal/logic/user"
"schisandra-album-cloud-microservices/app/auth/internal/svc"
"schisandra-album-cloud-microservices/app/auth/internal/types"
)
func PhoneLoginHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.PhoneLoginRequest
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := user.NewPhoneLoginLogic(r.Context(), svcCtx)
resp, err := l.PhoneLogin(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}

View File

@@ -1,28 +0,0 @@
package user
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"schisandra-album-cloud-microservices/app/auth/internal/logic/user"
"schisandra-album-cloud-microservices/app/auth/internal/svc"
"schisandra-album-cloud-microservices/app/auth/internal/types"
)
func ResetPasswordHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.ResetPasswordRequest
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := user.NewResetPasswordLogic(r.Context(), svcCtx)
resp, err := l.ResetPassword(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}

View File

@@ -1,35 +0,0 @@
package user
import (
"context"
"schisandra-album-cloud-microservices/app/auth/internal/svc"
"schisandra-album-cloud-microservices/app/auth/internal/types"
"schisandra-album-cloud-microservices/common/i18n"
"github.com/zeromicro/go-zero/core/logx"
)
type AccountLoginLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewAccountLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AccountLoginLogic {
return &AccountLoginLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *AccountLoginLogic) AccountLogin(req *types.AccountLoginRequest) (resp *types.LoginResponse, err error) {
// todo: add your logic here and delete this line
i18n.IsHasI18n(l.ctx)
text := i18n.FormatText(l.ctx, "user.name", "landaiqing")
return &types.LoginResponse{
AccessToken: text,
}, nil
}

View File

@@ -1,30 +0,0 @@
package user
import (
"context"
"schisandra-album-cloud-microservices/app/auth/internal/svc"
"schisandra-album-cloud-microservices/app/auth/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type PhoneLoginLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewPhoneLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PhoneLoginLogic {
return &PhoneLoginLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *PhoneLoginLogic) PhoneLogin(req *types.PhoneLoginRequest) (resp *types.LoginResponse, err error) {
// todo: add your logic here and delete this line
return
}

View File

@@ -1,30 +0,0 @@
package user
import (
"context"
"schisandra-album-cloud-microservices/app/auth/internal/svc"
"schisandra-album-cloud-microservices/app/auth/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type ResetPasswordLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewResetPasswordLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ResetPasswordLogic {
return &ResetPasswordLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *ResetPasswordLogic) ResetPassword(req *types.ResetPasswordRequest) (resp string, err error) {
// todo: add your logic here and delete this line
return
}

View File

@@ -1,21 +0,0 @@
package middleware
import (
"net/http"
"schisandra-album-cloud-microservices/common/middleware"
)
type SecurityHeadersMiddleware struct {
}
func NewSecurityHeadersMiddleware() *SecurityHeadersMiddleware {
return &SecurityHeadersMiddleware{}
}
func (m *SecurityHeadersMiddleware) Handle(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
middleware.SecurityHeadersMiddleware(w, r)
next(w, r)
}
}

View File

@@ -1,26 +0,0 @@
package svc
import (
"github.com/zeromicro/go-zero/rest"
"schisandra-album-cloud-microservices/app/auth/internal/config"
"schisandra-album-cloud-microservices/app/auth/internal/middleware"
"schisandra-album-cloud-microservices/common/core"
"schisandra-album-cloud-microservices/common/ent"
)
type ServiceContext struct {
Config config.Config
I18nMiddleware rest.Middleware
SecurityHeadersMiddleware rest.Middleware
DB *ent.Client
}
func NewServiceContext(c config.Config) *ServiceContext {
return &ServiceContext{
Config: c,
I18nMiddleware: middleware.NewI18nMiddleware().Handle,
SecurityHeadersMiddleware: middleware.NewSecurityHeadersMiddleware().Handle,
DB: core.InitMySQL(c.Mysql.DataSource),
}
}

View File

@@ -1,34 +0,0 @@
// Code generated by goctl. DO NOT EDIT.
// goctl 1.7.3
package types
type AccountLoginRequest struct {
Account string `json:"account"`
Password string `json:"password"`
AutoLogin bool `json:"auto_login"`
Angle int64 `json:"angle"`
Key string `json:"key"`
}
type LoginResponse struct {
AccessToken string `json:"access_token"`
UID string `json:"uid"`
Username string `json:"username,optional"`
Nickname string `json:"nickname"`
Avatar string `json:"avatar"`
Status int64 `json:"status"`
}
type PhoneLoginRequest struct {
Phone string `json:"phone"`
Captcha string `json:"captcha"`
AutoLogin bool `json:"auto_login"`
}
type ResetPasswordRequest struct {
Phone string `json:"phone"`
Captcha string `json:"captcha"`
Password string `json:"password"`
Repassword string `json:"repassword"`
}