fonchain-fiee/pkg/router/gitea.go
2025-02-19 14:24:15 +08:00

146 lines
4.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package router
import (
"fmt"
"github.com/dorlolo/simpleRequest"
"github.com/fonchain_enterprise/fonchain-main/pkg/config"
"github.com/fonchain_enterprise/fonchain-main/pkg/e"
"github.com/fonchain_enterprise/fonchain-main/pkg/service"
"github.com/fonchain_enterprise/fonchain-main/pkg/service/auth/sso"
"github.com/gin-gonic/gin"
"net/http"
"net/http/httputil"
"net/url"
"time"
)
func GiteaRoute(router *gin.RouterGroup) {
proxyDev(router)
r := router.Group("sso")
// OpenID发现文档端点
r.GET("/.well-known/openid-configuration", sso.Configuration)
r.GET("/login", sso.LoginHtml)
r.POST("/login", sso.Login)
// 授权端点
r.GET("/auth", sso.Auth)
r.GET("/auth/pass", sso.AuthSuccess)
// 令牌端点
r.POST("/token", sso.Token)
// 用户信息端点
r.GET("/userinfo", sso.SsoUserInfo)
//通过微信绑定手机号
r.GET("/wechat/bind", sso.WechatLoginHtml)
r.POST("/wechat/bind", sso.WechatBind)
r.POST("/wechat/phone/now", sso.WechatNowPhone)
}
func proxyDev(r *gin.RouterGroup) {
fmt.Println("1------", config.AppMode)
if config.AppMode == "debug" {
target := "http://172.16.100.93:8046"
targetUrl, err := url.Parse(target)
if err != nil {
return
}
proxy := httputil.NewSingleHostReverseProxy(targetUrl)
tempTarget := "http://172.16.100.93:9100"
tempTargetUrl, err := url.Parse(tempTarget)
if err != nil {
return
}
proxy1 := httputil.NewSingleHostReverseProxy(tempTargetUrl)
// 使用Gin的Any方法来匹配所有请求方法
r.Any("/apiv2/*action", func(c *gin.Context) {
// 修改请求的主机为目标主机这是因为在反向代理中请求的Host头信息不会自动更新
c.Request.Host = targetUrl.Host
c.Request.URL.Host = targetUrl.Host
c.Request.URL.Scheme = targetUrl.Scheme
// 反向代理转发请求
proxy.ServeHTTP(c.Writer, c.Request)
})
r.Any("/containeradm/*action", func(c *gin.Context) {
// 修改请求的主机为目标主机这是因为在反向代理中请求的Host头信息不会自动更新
c.Request.Host = tempTargetUrl.Host
c.Request.URL.Host = tempTargetUrl.Host
c.Request.URL.Scheme = tempTargetUrl.Scheme
// 反向代理转发请求
proxy1.ServeHTTP(c.Writer, c.Request)
})
}
ArtistInfoProxy(r)
}
// 使用simpleRequest实现的画家宝聊天模块简单代理
func ArtistInfoProxy(r *gin.RouterGroup) {
var artistinfoTarget string
if config.Env == "dev" {
artistinfoTarget = "http://127.0.0.1:8084"
} else if config.Env == "test" {
artistinfoTarget = "http://114.218.158.24:9048"
} else {
artistinfoTarget = "https://artist.fontree.cn"
}
artistinfotargetUrl, err := url.Parse(artistinfoTarget)
if err != nil {
return
}
fmt.Println("artistinfotargetUrl:", artistinfotargetUrl)
//画家宝客服聊天代理转发
r.Any("/api/aschat/*action", func(c *gin.Context) {
c.Request.Host = artistinfotargetUrl.Host
c.Request.URL.Host = artistinfotargetUrl.Host
c.Request.URL.Scheme = artistinfotargetUrl.Scheme
authorization := c.GetHeader(e.Authorization)
if authorization == "" {
service.NotLoginRes(c, e.ErrNotLogin)
return
}
fmt.Println("c.Request.URL.String()", c.Request.URL.String())
req := simpleRequest.NewRequest()
req.TimeOut(15 * time.Second)
req.Headers().Set(e.Authorization, authorization).Set("domain", "fontree")
params := c.Params
action := params[0].Value
fmt.Println("action:", action)
if file, errs := c.FormFile("file"); file != nil && errs == nil {
fmt.Println("上传文件")
req.Headers()
req.Body().
SetFromDataMultipartFile("file", file)
formData, _ := c.MultipartForm()
for key, value := range formData.Value {
req.Body().Set(key, value[0])
}
} else {
fmt.Println("普通请求")
bodybuf, _ := c.GetRawData()
req.Body().SetBytes(bodybuf)
req.Headers().ConentType_json()
fmt.Printf("请求参数:%+v\n", string(bodybuf))
}
resp, requestError := req.LaunchTo(c.Request.URL.String(), c.Request.Method)
if requestError != nil {
fmt.Println("req.POST Err", requestError.Error())
service.Error(c, e.Failed, requestError, "请求失败")
return
}
c.String(http.StatusOK, string(resp))
})
}