diff --git a/middleware/option.go b/middleware/option.go index 9c95432..3aaf619 100644 --- a/middleware/option.go +++ b/middleware/option.go @@ -47,6 +47,10 @@ func InjectSiteInfo() gin.HandlerFunc { ignoreFunc := func(c *gin.Context) { c.Next() } + if bootstrap.StaticFS == nil { + return ignoreFunc + } + // 读取index.html file, err := bootstrap.StaticFS.Open("/index.html") if err != nil { diff --git a/middleware/option_test.go b/middleware/option_test.go index b5a4852..1a75f61 100644 --- a/middleware/option_test.go +++ b/middleware/option_test.go @@ -99,6 +99,17 @@ func TestInjectSiteInfo(t *testing.T) { asserts := assert.New(t) rec := httptest.NewRecorder() + // 静态资源未加载 + { + TestFunc := InjectSiteInfo() + + c, _ := gin.CreateTestContext(rec) + c.Params = []gin.Param{} + c.Request, _ = http.NewRequest("GET", "/", nil) + TestFunc(c) + asserts.False(c.IsAborted()) + } + // index.html 不存在 { testStatic := &StaticMock{}