github.com/gramework/gramework@v1.8.1-0.20231027140105-82555c9057f5/app_router.go (about)

     1  // Copyright 2017-present Kirill Danshin and Gramework contributors
     2  // Copyright 2019-present Highload LTD (UK CN: 11893420)
     3  //
     4  // Licensed under the Apache License, Version 2.0 (the "License");
     5  // you may not use this file except in compliance with the License.
     6  // You may obtain a copy of the License at
     7  //
     8  //     http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  
    11  package gramework
    12  
    13  import (
    14  	"time"
    15  
    16  	"github.com/valyala/fasthttp"
    17  )
    18  
    19  // ServeDir from a given path
    20  func (app *App) ServeDir(path string) func(*Context) {
    21  	return app.ServeDirCustom(path, 0, true, false, []string{"index.html", "index.htm"})
    22  }
    23  
    24  // ServeDirCustom gives you ability to serve a dir with custom settings
    25  func (app *App) ServeDirCustom(path string, stripSlashes int, compress bool, generateIndexPages bool, indexNames []string) func(*Context) {
    26  	if indexNames == nil {
    27  		indexNames = []string{}
    28  	}
    29  	fs := &fasthttp.FS{
    30  		Root:                 path,
    31  		IndexNames:           indexNames,
    32  		GenerateIndexPages:   generateIndexPages,
    33  		Compress:             compress,
    34  		CacheDuration:        5 * time.Minute,
    35  		CompressedFileSuffix: ".gz",
    36  	}
    37  
    38  	if stripSlashes > 0 {
    39  		fs.PathRewrite = fasthttp.NewPathSlashesStripper(stripSlashes)
    40  	}
    41  
    42  	h := fs.NewRequestHandler()
    43  	return func(ctx *Context) {
    44  		h(ctx.RequestCtx)
    45  	}
    46  }
    47  
    48  // ServeDirNoCache gives you ability to serve a dir without caching
    49  func (app *App) ServeDirNoCache(path string) func(*Context) {
    50  	return app.ServeDirNoCacheCustom(path, 0, true, false, nil)
    51  }
    52  
    53  // ServeDirNoCacheCustom gives you ability to serve a dir with custom settings without caching
    54  func (app *App) ServeDirNoCacheCustom(path string, stripSlashes int, compress bool, generateIndexPages bool, indexNames []string) func(*Context) {
    55  	if indexNames == nil {
    56  		indexNames = []string{}
    57  	}
    58  	fs := &fasthttp.FS{
    59  		Root:                 path,
    60  		IndexNames:           indexNames,
    61  		GenerateIndexPages:   generateIndexPages,
    62  		Compress:             compress,
    63  		CacheDuration:        time.Millisecond,
    64  		CompressedFileSuffix: ".gz",
    65  	}
    66  
    67  	if stripSlashes > 0 {
    68  		fs.PathRewrite = fasthttp.NewPathSlashesStripper(stripSlashes)
    69  	}
    70  
    71  	h := fs.NewRequestHandler()
    72  	pragmaH := "Pragma"
    73  	pragmaV := "no-cache"
    74  	expiresH := "Expires"
    75  	expiresV := "0"
    76  	ccH := "Cache-Control"
    77  	ccV := "no-cache, no-store, must-revalidate"
    78  	return func(ctx *Context) {
    79  		ctx.Response.Header.Add(pragmaH, pragmaV)
    80  		ctx.Response.Header.Add(expiresH, expiresV)
    81  		ctx.Response.Header.Add(ccH, ccV)
    82  		h(ctx.RequestCtx)
    83  	}
    84  }