diff --git a/middleware/option.go b/middleware/option.go index 9c95432105c9b24bb3d451bdcd3d556956baff8b..3aaf6191eb98e38e698226f001a22099e059a31e 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 b5a4852cd6c14c4019f98552d39ebb77302240f8..1a75f61caf4dd7664c6797f33b5629b2650c95c4 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{}