-
Notifications
You must be signed in to change notification settings - Fork 3
/
no_route.go
62 lines (54 loc) · 1.57 KB
/
no_route.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package ginKit
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/richelieu-yang/chimera/v3/internal/resources"
"github.com/richelieu-yang/chimera/v3/src/core/pathKit"
"github.com/richelieu-yang/chimera/v3/src/core/strKit"
"github.com/richelieu-yang/chimera/v3/src/file/fileKit"
"net/http"
"sync"
)
var (
// noRouteHtmlPath 相对路径
noRouteHtmlPath = "_resources/html/404.min.html"
// noRouteHtmlName 文件名
noRouteHtmlName = fileKit.GetFileName(noRouteHtmlPath)
)
var noRouteOnce sync.Once
var noRouteErr error
// DefaultNoRouteHtml 使用自带的404页面.
func DefaultNoRouteHtml(engine *gin.Engine) error {
noRouteOnce.Do(func() {
tempDir, err := pathKit.GetExclusiveTempDir()
if err != nil {
noRouteErr = err
return
}
err = resources.RestoreAsset(tempDir, noRouteHtmlPath)
filePath := pathKit.Join(tempDir, noRouteHtmlPath)
if err := fileKit.AssertExistAndIsFile(filePath); err != nil {
noRouteErr = err
return
}
engine.LoadHTMLFiles(filePath)
})
if noRouteErr != nil {
return noRouteErr
}
var prefix string
if strKit.IsNotEmpty(serviceInfo) {
prefix = fmt.Sprintf("[%s] ", serviceInfo)
}
engine.NoRoute(func(ctx *gin.Context) {
ctx.HTML(http.StatusNotFound, noRouteHtmlName, gin.H{
"prefix": prefix,
"route": ctx.Request.URL.Path,
})
//ctx.Data(http.StatusNotFound, "text/html; charset=utf-8", noRouteData)
//// 此处不使用 ctx.FileFromFS ,原因: 这样的话,响应状态码就会是200,改不了
//fs := resources.AssetFile()
//ctx.FileFromFS("_resources/html/404.min.html", fs)
})
return nil
}