use middlewares from CasaOS-Common (#7)

This commit is contained in:
Tiger Wang (王豫) 2022-08-25 17:36:24 -04:00 committed by GitHub
parent 8d9c3f987d
commit 5417c6b94b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 6 additions and 67 deletions

2
go.mod
View File

@ -3,7 +3,7 @@ module github.com/IceWhaleTech/CasaOS-UserService
go 1.18
require (
github.com/IceWhaleTech/CasaOS-Common v0.0.0-20220811230555-229797f76426
github.com/IceWhaleTech/CasaOS-Common v0.0.0-20220825191226-4519f1880f68
github.com/IceWhaleTech/CasaOS-Gateway v0.0.0-20220804231126-285796241a3b
github.com/gin-contrib/gzip v0.0.6
github.com/gin-gonic/gin v1.8.1

4
go.sum
View File

@ -1,6 +1,6 @@
github.com/BurntSushi/toml v1.2.0 h1:Rt8g24XnyGTyglgET/PRUNlrUeu9F5L+7FilkXfZgs0=
github.com/IceWhaleTech/CasaOS-Common v0.0.0-20220811230555-229797f76426 h1:+Yc9qJ6AehudNsIRkoyTe8dmHiHDWsPF/QL7qk8Va6o=
github.com/IceWhaleTech/CasaOS-Common v0.0.0-20220811230555-229797f76426/go.mod h1:XGqdbedN9UlF3/rylcXKJ2BW4ayugBmEMa4Z0tk2KbQ=
github.com/IceWhaleTech/CasaOS-Common v0.0.0-20220825191226-4519f1880f68 h1:KMFS0/08mKgZqsM8fd5If/1NOLwtIvi7dljFEkJCdSw=
github.com/IceWhaleTech/CasaOS-Common v0.0.0-20220825191226-4519f1880f68/go.mod h1:XGqdbedN9UlF3/rylcXKJ2BW4ayugBmEMa4Z0tk2KbQ=
github.com/IceWhaleTech/CasaOS-Gateway v0.0.0-20220804231126-285796241a3b h1:IiMCqvGelQLGTX151gqVwrzoPQVJy8Q2JAvkhjiQ6tY=
github.com/IceWhaleTech/CasaOS-Gateway v0.0.0-20220804231126-285796241a3b/go.mod h1:jcURlZtPPQJJvfIW4ZgDTtpFfak7bPTvKZUxWxf62M8=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=

View File

@ -3,7 +3,6 @@ package main
import (
"flag"
"fmt"
"log"
"net"
"net/http"
"os"
@ -16,6 +15,7 @@ import (
"github.com/IceWhaleTech/CasaOS-UserService/pkg/utils/random"
"github.com/IceWhaleTech/CasaOS-UserService/route"
"github.com/IceWhaleTech/CasaOS-UserService/service"
"go.uber.org/zap"
)
const (
@ -87,7 +87,7 @@ func main() {
panic(err)
}
log.Printf("user service listening on %s", listener.Addr().String())
logger.Info("User service is listening...", zap.Any("address", listener.Addr().String()))
err = http.Serve(listener, r)
if err != nil {
panic(err)

View File

@ -1,61 +0,0 @@
/*
* @Author: LinkLeong link@icewhale.com
* @Date: 2021-10-08 10:29:08
* @LastEditors: LinkLeong
* @LastEditTime: 2022-07-22 11:06:07
* @Description:
* @Website: https://www.casaos.io
* Copyright (c) 2022 by icewhale, All Rights Reserved.
*/
package middleware
import (
"fmt"
"net/http"
"strings"
"github.com/IceWhaleTech/CasaOS-Common/utils/logger"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
)
func Cors() gin.HandlerFunc {
return func(c *gin.Context) {
method := c.Request.Method
c.Header("Access-Control-Allow-Origin", "*")
c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE,UPDATE")
// 允许跨域设置可以返回其他子段,可以自定义字段
c.Header("Access-Control-Allow-Headers", "Authorization, Content-Length, X-CSRF-Token, Token,session,Language,Content-Type,Access-Control-Allow-Origin,Access-Control-Allow-Headers,Access-Control-Allow-Methods,Connection,Host,Origin,Referer,User-Agent,X-Requested-With")
// 允许浏览器(客户端)可以解析的头部 (重要)
c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers")
// c.Writer.Header().Set("Access-Control-Allow-Headers", "Accept, Authorization, Content-Type, Content-Length, X-CSRF-Token, Token, session, Origin, Host, Connection, Accept-Encoding, Accept-Language, X-Requested-With")
// 设置缓存时间
c.Header("Access-Control-Max-Age", "172800")
c.Header("Access-Control-Allow-Credentials", "true")
c.Set("Content-Type", "application/json")
//}
// 允许类型校验
if method == "OPTIONS" {
c.JSON(http.StatusOK, "ok!")
}
defer func() {
if err := recover(); err != nil {
fmt.Println(err)
}
}()
c.Next()
}
}
func WriteLog() gin.HandlerFunc {
return func(c *gin.Context) {
if !strings.Contains(c.Request.URL.String(), "password") {
logger.Info("request:", zap.Any("path", c.Request.URL.String()), zap.Any("param", c.Params), zap.Any("query", c.Request.URL.Query()), zap.Any("method", c.Request.Method))
c.Next()
}
}
}

View File

@ -3,8 +3,8 @@ package route
import (
"os"
"github.com/IceWhaleTech/CasaOS-Common/middleware"
"github.com/IceWhaleTech/CasaOS-Common/utils/jwt"
"github.com/IceWhaleTech/CasaOS-UserService/middleware"
v1 "github.com/IceWhaleTech/CasaOS-UserService/route/v1"
"github.com/gin-contrib/gzip"
"github.com/gin-gonic/gin"