| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- package main
- import (
- "demo/configs"
- "demo/router"
- "demo/util/templatefunc"
- "github.com/gin-gonic/gin"
- "net/http"
- "net/http/httputil"
- "net/url"
- )
- func init() {
- configs.ConfigInit()
- }
- func main() {
- runGin()
- //Test()
- }
- // 接口前缀
- func runGin() {
- Router := gin.Default()
- router.TestRouth(Router.Group("/"))
- // 重定向 /back 及其子路径到指定 IP 地址
- apiGroup := Router.Group(router.Prefix)
- Router.SetFuncMap(templatefunc.BaseTemplateFunc())
- //加载模板
- Router.LoadHTMLGlob("file/static/**/**")
- apiGroup.StaticFS("/static", http.Dir("file/resources"))
- apiGroup.StaticFS("/assets", http.Dir("file/assets"))
- //proxy(Router)
- router.InitRouter(apiGroup)
- router.InitAuthority(Router)
- Router.Run(":8182")
- }
- func proxy(Router *gin.Engine) {
- proxy := func(target string) gin.HandlerFunc {
- return func(c *gin.Context) {
- targetUrl, err := url.Parse(target)
- if err != nil {
- c.JSON(http.StatusInternalServerError, gin.H{"error": "Invalid target URL"})
- return
- }
- proxy := httputil.NewSingleHostReverseProxy(targetUrl)
- proxy.ServeHTTP(c.Writer, c.Request)
- }
- }
- // 将所有未定义的路径转发到其他 IP
- Router.NoRoute(proxy("http://127.0.0.1:12306"))
- }
|