github.com/IRelaxxx/servefiles/v3@v3.4.6/example_test.go (about)

     1  package servefiles_test
     2  
     3  import (
     4  	"log"
     5  	"net/http"
     6  	"time"
     7  
     8  	"github.com/spf13/afero"
     9  
    10  	"github.com/IRelaxxx/servefiles/v3"
    11  )
    12  
    13  func ExampleNewAssetHandler() {
    14  	// A simple webserver
    15  
    16  	// where the assets are stored (replace as required)
    17  	localPath := "./assets"
    18  
    19  	// how long we allow user agents to cache assets
    20  	// (this is in addition to conditional requests, see
    21  	// RFC7234 https://tools.ietf.org/html/rfc7234#section-5.2.2.8)
    22  	maxAge := time.Hour
    23  
    24  	h := servefiles.NewAssetHandler(localPath).WithMaxAge(maxAge)
    25  
    26  	log.Fatal(http.ListenAndServe(":8080", h))
    27  }
    28  
    29  func ExampleNewAssetHandlerFS() {
    30  	// A simple webserver
    31  
    32  	// where the assets are stored (replace as required)
    33  	fs := afero.NewOsFs()
    34  
    35  	// how long we allow user agents to cache assets
    36  	// (this is in addition to conditional requests, see
    37  	// RFC7234 https://tools.ietf.org/html/rfc7234#section-5.2.2.8)
    38  	maxAge := time.Hour
    39  
    40  	h := servefiles.NewAssetHandlerFS(fs).WithMaxAge(maxAge)
    41  
    42  	log.Fatal(http.ListenAndServe(":8080", h))
    43  }