From df83602e1718907ed97895d03d08d6751f639595 Mon Sep 17 00:00:00 2001 From: Yang Luo Date: Tue, 20 Oct 2020 23:24:07 +0800 Subject: [PATCH] Serve static files in Go. --- main.go | 7 +++++++ routers/filter.go | 43 +++++++++++++++++++++++++++++++++++++++++++ util/path.go | 24 ++++++++++++++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 routers/filter.go create mode 100644 util/path.go diff --git a/main.go b/main.go index 228094f4..d05e0919 100644 --- a/main.go +++ b/main.go @@ -18,6 +18,7 @@ import ( "github.com/astaxie/beego" "github.com/astaxie/beego/plugins/cors" "github.com/casdoor/casdoor/object" + "github.com/casdoor/casdoor/routers" _ "github.com/casdoor/casdoor/routers" ) @@ -33,6 +34,12 @@ func main() { AllowCredentials: true, })) + //beego.DelStaticPath("/static") + beego.SetStaticPath("/static", "web/build/static") + // https://studygolang.com/articles/2303 + beego.InsertFilter("/", beego.BeforeRouter, routers.TransparentStatic) // must has this for default page + beego.InsertFilter("/*", beego.BeforeRouter, routers.TransparentStatic) + beego.BConfig.WebConfig.Session.SessionProvider="file" beego.BConfig.WebConfig.Session.SessionProviderConfig = "./tmp" beego.BConfig.WebConfig.Session.SessionGCMaxLifetime = 3600 * 24 * 365 diff --git a/routers/filter.go b/routers/filter.go new file mode 100644 index 00000000..56afb3c7 --- /dev/null +++ b/routers/filter.go @@ -0,0 +1,43 @@ +// Copyright 2020 The casbin Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package routers + +import ( + "net/http" + "strings" + + "github.com/astaxie/beego/context" + "github.com/casdoor/casdoor/util" +) + +func TransparentStatic(ctx *context.Context) { + urlPath := ctx.Request.URL.Path + if strings.HasPrefix(urlPath, "/api/") { + return + } + + path := "web/build" + if urlPath == "/" { + path += "/index.html" + } else { + path += urlPath + } + + if util.FileExist(path) { + http.ServeFile(ctx.ResponseWriter, ctx.Request, path) + } else { + http.ServeFile(ctx.ResponseWriter, ctx.Request, "web/build/index.html") + } +} diff --git a/util/path.go b/util/path.go new file mode 100644 index 00000000..91b01083 --- /dev/null +++ b/util/path.go @@ -0,0 +1,24 @@ +// Copyright 2020 The casbin Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package util + +import "os" + +func FileExist(path string) bool { + if _, err := os.Stat(path); os.IsNotExist(err) { + return false + } + return true +}