Skip to content

Commit

Permalink
Merge pull request CocaineCong#14 from CocaineCong/v2
Browse files Browse the repository at this point in the history
feat:http
  • Loading branch information
CocaineCong authored Jun 7, 2023
2 parents f038310 + cf3b4ef commit 011aeba
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 53 deletions.
24 changes: 0 additions & 24 deletions app/gateway/internal/handler/pkg.go

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package handler
package http

import (
"net/http"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package handler
package http

import (
"net/http"
Expand Down
14 changes: 1 addition & 13 deletions app/gateway/middleware/cors.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,16 @@
package middleware

import (
"fmt"
"net/http"
"strings"

"github.com/gin-gonic/gin"
)

// 跨域
// Cors 跨域
func Cors() gin.HandlerFunc {
return func(c *gin.Context) {
method := c.Request.Method // 请求方法
origin := c.Request.Header.Get("Origin") // 请求头部
var headerKeys []string // 声明请求头keys
for k := range c.Request.Header {
headerKeys = append(headerKeys, k)
}
headerStr := strings.Join(headerKeys, ", ")
if headerStr != "" {
headerStr = fmt.Sprintf("access-control-allow-origin, access-control-allow-headers, %s", headerStr)
} else {
headerStr = "access-control-allow-origin, access-control-allow-headers"
}
if origin != "" {
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
c.Header("Access-Control-Allow-Origin", "*") // 这是允许访问所有域
Expand Down
5 changes: 3 additions & 2 deletions app/gateway/middleware/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/gin-gonic/gin"

"github.com/CocaineCong/grpc-todolist/consts"
"github.com/CocaineCong/grpc-todolist/pkg/ctl"
"github.com/CocaineCong/grpc-todolist/pkg/e"
"github.com/CocaineCong/grpc-todolist/pkg/util/jwt"
)
Expand Down Expand Up @@ -41,7 +41,8 @@ func JWT() gin.HandlerFunc {
c.Abort()
return
}
c.Set(consts.UserIdKey, claims.UserID)
c.Request = c.Request.WithContext(ctl.NewContext(c.Request.Context(), &ctl.UserInfo{Id: claims.UserID}))
ctl.InitUserInfo(c.Request.Context())
c.Next()
}
}
14 changes: 7 additions & 7 deletions app/gateway/routes/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie"

"github.com/CocaineCong/grpc-todolist/app/gateway/internal/handler"
"github.com/CocaineCong/grpc-todolist/app/gateway/internal/http"
"github.com/CocaineCong/grpc-todolist/app/gateway/middleware"
)

Expand All @@ -21,18 +21,18 @@ func NewRouter() *gin.Engine {
context.JSON(200, "success")
})
// 用户服务
v1.POST("/user/register", handler.UserRegister)
v1.POST("/user/login", handler.UserLogin)
v1.POST("/user/register", http.UserRegister)
v1.POST("/user/login", http.UserLogin)

// 需要登录保护
authed := v1.Group("/")
authed.Use(middleware.JWT())
{
// 任务模块
authed.GET("task", handler.GetTaskList)
authed.POST("task", handler.CreateTask)
authed.PUT("task", handler.UpdateTask)
authed.DELETE("task", handler.DeleteTask)
authed.GET("task", http.GetTaskList)
authed.POST("task", http.CreateTask)
authed.PUT("task", http.UpdateTask)
authed.DELETE("task", http.DeleteTask)
}
}
return ginRouter
Expand Down
File renamed without changes.
File renamed without changes.
24 changes: 19 additions & 5 deletions pkg/ctl/user_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,32 @@ package ctl

import (
"context"
"errors"
)

"github.com/gin-gonic/gin"
type key int

"github.com/CocaineCong/grpc-todolist/consts"
)
var userKey key

type UserInfo struct {
Id int64 `json:"id"`
}

func GetUserInfo(ctx *gin.Context) (*UserInfo, error) {
return &UserInfo{Id: ctx.GetInt64(consts.UserIdKey)}, nil
func GetUserInfo(ctx context.Context) (*UserInfo, error) {
user, ok := FromContext(ctx)
if !ok {
return nil, errors.New("获取用户信息错误")
}
return user, nil
}

func NewContext(ctx context.Context, u *UserInfo) context.Context {
return context.WithValue(ctx, userKey, u)
}

func FromContext(ctx context.Context) (*UserInfo, bool) {
u, ok := ctx.Value(userKey).(*UserInfo)
return u, ok
}

func InitUserInfo(ctx context.Context) {
Expand Down

0 comments on commit 011aeba

Please sign in to comment.