static

package module
v0.0.0-...-6334b0c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 1, 2016 License: MIT Imports: 3 Imported by: 0

README

static

Build Status Coverage Status Go Report Card

lu static file serving middleware, based on fasthttp.FS.

example

package main

import (
	"log"
	
	"github.com/valyala/fasthttp"
	"github.com/vincentLiuxiang/lu"
 	"github.com/lugolang/static"
)

func main() {
	app := lu.New()
	fs := static.DefaultFS
	// fs.Root = "/static/file/path/"
	Static := static.New(fs)
	app.Get("/static", Static)
	server := &fasthttp.Server{
		Handler:       app.Handler,
		Concurrency:   1024 * 1024,
		Name:          "lu",
		MaxConnsPerIP: 50,
	}
	if err := server.ListenAndServe(":8080"); err != nil {
		log.Fatalf("error in lu server: %s", err)
	}
}

the config of fs is totally same with fasthttp.FS

fs.Root string

  • The default value of fs.Root is ".", it means you should put your static file (html/css/js etc.) to the directory where your go program starts.

  • However you can custom it in your way.

fs.IndexNames []string

  • The default value is []string{"index.html"}
  • It specify the index file. For example, when you access to http://xxxxxx:xxx/home/ , static will search the directory fs.Root + /home/ to find the files in fs.IndexNames , and response to the client when find one.
  • if no file is found , static will call next(nil) to pass the request to next non-error-middleware

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultFS = &fasthttp.FS{
	Compress:        true,
	IndexNames:      []string{"index.html"},
	Root:            ".",
	AcceptByteRange: true,
}

DefaultFS the defalut config of fasthttp.FS

Functions

func New

func New(fs *fasthttp.FS) func(ctx *fasthttp.RequestCtx, next func(error))

New accepts fs which is a *fasthttp.FS pointer.

if the http request method is not 'GET' or 'HEAD', the static will call next(nil)

if the http request file does not exist or the directory does not have the IndexNames file. the static will call next(nil)

or, fasthttp.FS will handle the request file or the IndexNames file in the request directory

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL