file

package
v2.1.4+incompatible Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2018 License: MIT Imports: 6 Imported by: 4

Documentation

Overview

Package file provides handlers that serve static files for the ozzo routing package.

Index

Constants

This section is empty.

Variables

View Source
var RootPath string

RootPath stores the current working path

Functions

func Content

func Content(path string) routing.Handler

Content returns a handler that serves the content of the specified file as the response. The file to be served can be specified as an absolute file path or a path relative to RootPath (which defaults to the current working path). If the specified file does not exist, the handler will pass the control to the next available handler.

func Server

func Server(pathMap PathMap, opts ...ServerOptions) routing.Handler

Server returns a handler that serves the files as the response content. The files being served are determined using the current URL path and the specified path map. For example, if the path map is {"/css": "/www/css", "/js": "/www/js"} and the current URL path "/css/main.css", the file "<working dir>/www/css/main.css" will be served. If a URL path matches multiple prefixes in the path map, the most specific prefix will take precedence. For example, if the path map contains both "/css" and "/css/img", and the URL path is "/css/img/logo.gif", then the path mapped by "/css/img" will be used.

import (
    "log"
    "github.com/go-ozzo/ozzo-routing"
    "github.com/go-ozzo/ozzo-routing/file"
)

r := routing.New()
r.Get("/*", file.Server(file.PathMap{
     "/css": "/ui/dist/css",
     "/js": "/ui/dist/js",
}))

Types

type PathMap

type PathMap map[string]string

PathMap specifies the mapping between URL paths (keys) and file paths (keys). The file paths are relative to Options.RootPath

type ServerOptions

type ServerOptions struct {
	// The path that all files to be served should be located within. The path map passed to the Server method
	// are all relative to this path. This property can be specified as an absolute file path or a path relative
	// to the current working path. If not set, this property defaults to the current working path.
	RootPath string
	// The file (e.g. index.html) to be served when the current request corresponds to a directory.
	// If not set, the handler will return a 404 HTTP error when the request corresponds to a directory.
	// This should only be a file name without the directory part.
	IndexFile string
	// The file to be served when no file or directory matches the current request.
	// If not set, the handler will return a 404 HTTP error when no file/directory matches the request.
	// The path of this file is relative to RootPath
	CatchAllFile string
	// A function that checks if the requested file path is allowed. If allowed, the function
	// may do additional work such as setting Expires HTTP header.
	// The function should return a boolean indicating whether the file should be served or not.
	// If false, a 404 HTTP error will be returned by the handler.
	Allow func(*routing.Context, string) bool
}

ServerOptions defines the possible options for the Server handler.

Jump to

Keyboard shortcuts

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