summaryrefslogtreecommitdiffstats
path: root/modules
diff options
context:
space:
mode:
authorUnknown <joe2010xtmf@163.com>2014-07-04 17:26:15 -0400
committerUnknown <joe2010xtmf@163.com>2014-07-04 17:26:15 -0400
commitaed4271c8dc60f34a59c548bbc1b8859b0105be0 (patch)
tree3d8521fa4397622c75249b2c5efc2468ea25484e /modules
parentbe8ac7fb75a389402929dd726f39bbbbdb9a6531 (diff)
downloadgitea-aed4271c8dc60f34a59c548bbc1b8859b0105be0.tar.gz
gitea-aed4271c8dc60f34a59c548bbc1b8859b0105be0.zip
Change martini.Static to custom Static function
Diffstat (limited to 'modules')
-rw-r--r--modules/middleware/static.go127
1 files changed, 127 insertions, 0 deletions
diff --git a/modules/middleware/static.go b/modules/middleware/static.go
new file mode 100644
index 0000000000..35f03f721a
--- /dev/null
+++ b/modules/middleware/static.go
@@ -0,0 +1,127 @@
+// Copyright 2013 The Martini Authors. All rights reserved.
+// Copyright 2014 The Gogs Authors. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
+package middleware
+
+import (
+ "log"
+ "net/http"
+ "path"
+ "runtime"
+ "strings"
+
+ "github.com/go-martini/martini"
+
+ "github.com/gogits/gogs/modules/setting"
+)
+
+// StaticOptions is a struct for specifying configuration options for the martini.Static middleware.
+type StaticOptions struct {
+ // Prefix is the optional prefix used to serve the static directory content
+ Prefix string
+ // SkipLogging will disable [Static] log messages when a static file is served.
+ SkipLogging bool
+ // IndexFile defines which file to serve as index if it exists.
+ IndexFile string
+ // Expires defines which user-defined function to use for producing a HTTP Expires Header
+ // https://developers.google.com/speed/docs/insights/LeverageBrowserCaching
+ Expires func() string
+}
+
+func prepareStaticOptions(options []StaticOptions) StaticOptions {
+ var opt StaticOptions
+ if len(options) > 0 {
+ opt = options[0]
+ }
+
+ // Defaults
+ if len(opt.IndexFile) == 0 {
+ opt.IndexFile = "index.html"
+ }
+ // Normalize the prefix if provided
+ if opt.Prefix != "" {
+ // Ensure we have a leading '/'
+ if opt.Prefix[0] != '/' {
+ opt.Prefix = "/" + opt.Prefix
+ }
+ // Remove any trailing '/'
+ opt.Prefix = strings.TrimRight(opt.Prefix, "/")
+ }
+ return opt
+}
+
+// Static returns a middleware handler that serves static files in the given directory.
+func Static(directory string, staticOpt ...StaticOptions) martini.Handler {
+ if runtime.GOOS == "windows" {
+ if len(directory) < 2 || directory[1] != ':' {
+ directory = path.Join(setting.StaticRootPath, directory)
+ }
+ } else if !path.IsAbs(directory) {
+ directory = path.Join(setting.StaticRootPath, directory)
+ }
+
+ dir := http.Dir(directory)
+ opt := prepareStaticOptions(staticOpt)
+
+ return func(res http.ResponseWriter, req *http.Request, log *log.Logger) {
+ if req.Method != "GET" && req.Method != "HEAD" {
+ return
+ }
+ file := req.URL.Path
+ // if we have a prefix, filter requests by stripping the prefix
+ if opt.Prefix != "" {
+ if !strings.HasPrefix(file, opt.Prefix) {
+ return
+ }
+ file = file[len(opt.Prefix):]
+ if file != "" && file[0] != '/' {
+ return
+ }
+ }
+ f, err := dir.Open(file)
+ if err != nil {
+ // discard the error?
+ return
+ }
+ defer f.Close()
+
+ fi, err := f.Stat()
+ if err != nil {
+ return
+ }
+
+ // try to serve index file
+ if fi.IsDir() {
+ // redirect if missing trailing slash
+ if !strings.HasSuffix(req.URL.Path, "/") {
+ http.Redirect(res, req, req.URL.Path+"/", http.StatusFound)
+ return
+ }
+
+ file = path.Join(file, opt.IndexFile)
+ f, err = dir.Open(file)
+ if err != nil {
+ return
+ }
+ defer f.Close()
+
+ fi, err = f.Stat()
+ if err != nil || fi.IsDir() {
+ return
+ }
+ }
+
+ if !opt.SkipLogging {
+ log.Println("[Static] Serving " + file)
+ }
+
+ // Add an Expires header to the static content
+ if opt.Expires != nil {
+ res.Header().Set("Expires", opt.Expires())
+ }
+
+ http.ServeContent(res, req, file, fi.ModTime(), f)
+ }
+}