🎉 init
This commit is contained in:
69
app/auth/auth.api
Normal file
69
app/auth/auth.api
Normal file
@@ -0,0 +1,69 @@
|
||||
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: I18nMiddleware,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)
|
||||
}
|
||||
|
37
app/auth/auth.go
Normal file
37
app/auth/auth.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"schisandra-album-cloud-microservices/app/auth/internal/config"
|
||||
"schisandra-album-cloud-microservices/app/auth/internal/handler"
|
||||
"schisandra-album-cloud-microservices/app/auth/internal/svc"
|
||||
|
||||
"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(func(header http.Header) {
|
||||
header.Set("Access-Control-Allow-Origin", "*")
|
||||
header.Add("Access-Control-Allow-Headers", "UserHeader1, UserHeader2")
|
||||
header.Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, PATCH")
|
||||
header.Set("Access-Control-Expose-Headers", "Content-Length, Content-Type")
|
||||
}, nil, "*"))
|
||||
defer server.Stop()
|
||||
|
||||
ctx := svc.NewServiceContext(c)
|
||||
handler.RegisterHandlers(server, ctx)
|
||||
|
||||
fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
|
||||
server.Start()
|
||||
}
|
5
app/auth/etc/auth.yaml
Normal file
5
app/auth/etc/auth.yaml
Normal file
@@ -0,0 +1,5 @@
|
||||
Name: auth
|
||||
Host: 0.0.0.0
|
||||
Port: 8888
|
||||
Mysql:
|
||||
Dsn: root:1611@tcp(localhost:3306)/schisandra-cloud-album?charset=utf8mb4&parseTime=True&loc=Local
|
10
app/auth/internal/config/config.go
Normal file
10
app/auth/internal/config/config.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package config
|
||||
|
||||
import "github.com/zeromicro/go-zero/rest"
|
||||
|
||||
type Config struct {
|
||||
rest.RestConf
|
||||
Mysql struct {
|
||||
Dsn string
|
||||
}
|
||||
}
|
43
app/auth/internal/handler/routes.go
Normal file
43
app/auth/internal/handler/routes.go
Normal file
@@ -0,0 +1,43 @@
|
||||
// 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.I18nMiddleware, 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),
|
||||
)
|
||||
}
|
28
app/auth/internal/handler/user/account_login_handler.go
Normal file
28
app/auth/internal/handler/user/account_login_handler.go
Normal file
@@ -0,0 +1,28 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
28
app/auth/internal/handler/user/phone_login_handler.go
Normal file
28
app/auth/internal/handler/user/phone_login_handler.go
Normal file
@@ -0,0 +1,28 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
28
app/auth/internal/handler/user/reset_password_handler.go
Normal file
28
app/auth/internal/handler/user/reset_password_handler.go
Normal file
@@ -0,0 +1,28 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
30
app/auth/internal/logic/user/account_login_logic.go
Normal file
30
app/auth/internal/logic/user/account_login_logic.go
Normal file
@@ -0,0 +1,30 @@
|
||||
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 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
|
||||
|
||||
return
|
||||
}
|
30
app/auth/internal/logic/user/phone_login_logic.go
Normal file
30
app/auth/internal/logic/user/phone_login_logic.go
Normal file
@@ -0,0 +1,30 @@
|
||||
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
|
||||
}
|
30
app/auth/internal/logic/user/reset_password_logic.go
Normal file
30
app/auth/internal/logic/user/reset_password_logic.go
Normal file
@@ -0,0 +1,30 @@
|
||||
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
|
||||
}
|
19
app/auth/internal/middleware/i18n_middleware.go
Normal file
19
app/auth/internal/middleware/i18n_middleware.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package middleware
|
||||
|
||||
import "net/http"
|
||||
|
||||
type I18nMiddleware struct {
|
||||
}
|
||||
|
||||
func NewI18nMiddleware() *I18nMiddleware {
|
||||
return &I18nMiddleware{}
|
||||
}
|
||||
|
||||
func (m *I18nMiddleware) Handle(next http.HandlerFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
// TODO generate middleware implement function, delete after code implementation
|
||||
|
||||
// Passthrough to next handler if need
|
||||
next(w, r)
|
||||
}
|
||||
}
|
21
app/auth/internal/middleware/securityheaders_middleware.go
Normal file
21
app/auth/internal/middleware/securityheaders_middleware.go
Normal file
@@ -0,0 +1,21 @@
|
||||
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)
|
||||
}
|
||||
}
|
26
app/auth/internal/svc/service_context.go
Normal file
26
app/auth/internal/svc/service_context.go
Normal file
@@ -0,0 +1,26 @@
|
||||
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/gen/entschema"
|
||||
)
|
||||
|
||||
type ServiceContext struct {
|
||||
Config config.Config
|
||||
I18nMiddleware rest.Middleware
|
||||
SecurityHeadersMiddleware rest.Middleware
|
||||
DB *entschema.Client
|
||||
}
|
||||
|
||||
func NewServiceContext(c config.Config) *ServiceContext {
|
||||
return &ServiceContext{
|
||||
Config: c,
|
||||
I18nMiddleware: middleware.NewI18nMiddleware().Handle,
|
||||
SecurityHeadersMiddleware: middleware.NewSecurityHeadersMiddleware().Handle,
|
||||
DB: core.InitMySQL(c.Mysql.Dsn),
|
||||
}
|
||||
}
|
34
app/auth/internal/types/types.go
Normal file
34
app/auth/internal/types/types.go
Normal file
@@ -0,0 +1,34 @@
|
||||
// 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"`
|
||||
}
|
Reference in New Issue
Block a user