コード例 #1
0
ファイル: favicon.go プロジェクト: nathanfaucett/middleware
func Favicon(options *FaviconOptions) func(*rest.Request, *rest.Response, func(error)) {
	dirname, err := os.Getwd()
	if err != nil {
		panic(err)
	}

	debug := debugger.Debug("Favicon")

	if options == nil {
		options = &FaviconOptions{}
	}
	if options.Pathname == "" {
		options.Pathname = "./app/assets/img/favicon.ico"
	}

	pathname := path.Join(dirname, options.Pathname)
	debug.Log("using Favicon " + options.Pathname)

	return func(req *rest.Request, res *rest.Response, next func(error)) {
		method := req.Method
		url := req.URL.Path

		if url != "/favicon.ico" || (method != "GET" && method != "HEAD") {
			next(nil)
			return
		}

		if method == "HEAD" {
			debug.Log("Serving HEAD request for " + options.Pathname)
		} else {
			debug.Log("Serving " + options.Pathname)
		}
		res.SendFile(pathname)
		next(nil)
	}
}
コード例 #2
0
ファイル: router.go プロジェクト: nathanfaucett/routes.go
	"errors"
	"github.com/nathanfaucett/debugger"
	"github.com/nathanfaucett/events"
)

const (
	GET     = "GET"
	POST    = "POST"
	PUT     = "PUT"
	PATCH   = "PATCH"
	HEAD    = "HEAD"
	OPTIONS = "OPTIONS"
	DELETE  = "DELETE"
)

var debug = debugger.Debug("Router")

type Router struct {
	*events.EventEmitter

	middleware []*Middleware
	routes     []*Route
}

// creates new router
func NewRouter() *Router {
	this := new(Router)
	this.EventEmitter = events.NewEventEmitter()

	return this
}
コード例 #3
0
func StaticServe(options *StaticServeOptions) func(*rest.Request, *rest.Response, func(error)) {
	dirname, err := os.Getwd()
	if err != nil {
		panic(err)
	}

	debug := debugger.Debug("StaticServe")

	if options == nil {
		options = &StaticServeOptions{
			Index: "index.html",
		}
	}
	if options.Root == "" {
		options.Root = "/assets/"
	}
	if string(options.Root[0]) != "/" {
		options.Root = "/" + options.Root
	}
	if string(options.Root[len(options.Root)-1]) != "/" {
		options.Root += "/"
	}
	if options.Directory == "" {
		options.Directory = "./app/assets/"
	}

	root := options.Root
	directory := path.Join(dirname, options.Directory)
	serveIndex := options.Index != ""
	index := path.Join(directory, options.Index)
	debug.Log("using StaticServe from " + options.Directory + " root " + root)

	return func(req *rest.Request, res *rest.Response, next func(error)) {
		method := req.Method

		if method != "GET" && method != "HEAD" {
			next(nil)
			return
		}

		url := req.URL.Path

		if url == "/" && serveIndex {
			debug.Log("Serving " + options.Index)
			res.SendFile(index)
			next(nil)
			return
		}
		if strings.Index(url, root) != 0 {
			next(nil)
			return
		}

		if method == "HEAD" {
			debug.Log("Serving HEAD request for " + url)
		} else {
			debug.Log("Serving GET request for " + url)
		}
		url = string(url[len(root):len(url)])
		fileName := path.Join(directory, url)

		res.SendFile(fileName)
		next(nil)
	}
}
コード例 #4
0
ファイル: cors.go プロジェクト: nathanfaucett/middleware
func Cors(options *CorsOptions) func(*rest.Request, *rest.Response, func(error)) {
	debug := debugger.Debug("Cors")

	if options == nil {
		options = &CorsOptions{}
	}
	if options.Origin == "" {
		options.Origin = "*"
	}
	if options.Methods == "" {
		options.Methods = "GET,POST,PUT,PATCH,HEAD,OPTIONS,DELETE"
	}
	if options.Credentials == "" {
		options.Credentials = "false"
	}
	if options.Headers == "" {
		options.Headers = ""
	}

	origin := options.Origin
	methods := options.Methods
	credentials := options.Credentials
	var maxAge string
	if options.MaxAge == 0 {
		maxAge = ""
	} else {
		maxAge = strconv.Itoa(options.MaxAge)
	}
	corsHeaders := options.Headers

	debug.Log(
		"using Cors with Options" +
			"\n\torigin: " + origin +
			"\n\tmethods: " + methods +
			"\n\tcredentials: " + credentials +
			"\n\tmaxAge: " + maxAge +
			"\n\tcorsHeaders: " + corsHeaders)

	return func(req *rest.Request, res *rest.Response, next func(error)) {
		var headers string
		if corsHeaders == "" {
			headers = req.GetHeader("Access-Control-Request-Headers")
		} else {
			headers = corsHeaders
		}

		if origin != "" {
			res.SetHeader("Access-Control-Allow-Origin", origin)
		}
		if methods != "" {
			res.SetHeader("Access-Control-Allow-Methods", methods)
		}
		if credentials != "" {
			res.SetHeader("Access-Control-Allow-Credentials", credentials)
		}
		if headers != "" {
			res.SetHeader("Access-Control-Allow-Headers", headers)
		}
		if maxAge != "" {
			res.SetHeader("Access-Control-Allow-Max-Age", maxAge)
		}

		next(nil)
	}
}
コード例 #5
0
ファイル: session.go プロジェクト: nathanfaucett/middleware
func Sessions(options *SessionsOptions) func(*rest.Request, *rest.Response, func(error)) {
	debug := debugger.Debug("Sessions")

	if options == nil {
		options = &SessionsOptions{}
	}
	if options.Name == "" {
		options.Name = "Rest.sid"
	}
	if options.Path == "" {
		options.Path = "/"
	}
	if options.MaxAge == 0 {
		options.MaxAge = 3600
	}
	if options.Secret == "" {
		options.Secret = util.Prng(24)
	}

	debug.Log(
		"using Sessions with Options" +
			"\n\tname: " + options.Name +
			"\n\tpath: " + options.Path +
			"\n\ttrustProxy: " + strconv.FormatBool(options.TrustProxy) +
			"\n\tsecret: " + options.Secret)

	return func(req *rest.Request, res *rest.Response, next func(error)) {
		if strings.Index(req.URL.Path, options.Path) != 0 {
			next(nil)
			return
		}

		var session *Session
		cookie, err := req.Cookie(options.Name)
		if err == nil {
			session = NewSession(false)
			session.id = util.Unsign(cookie.Value, options.Secret)
			session.cookie = cookie
		} else {
			cookie = &http.Cookie{
				Name:     options.Name,
				Path:     options.Path,
				Domain:   options.Domain,
				MaxAge:   options.MaxAge,
				Expires:  time.Now().Add(time.Duration(time.Duration(options.MaxAge) * time.Second)),
				Secure:   options.Secure,
				HttpOnly: options.HttpOnly,
			}
			session = NewSession(true)
			session.cookie = cookie
		}
		req.Session = session

		res.On("header", func() {
			if !session.isNew {
				debug.Log("Already set Cookie")
				return
			}

			cookie.Value = util.Sign(session.id, options.Secret)
			serialized := cookie.String()
			debug.Log("Setting new Cookie " + serialized)
			res.SetHeader("Set-Cookie", serialized)
		})

		next(nil)
	}
}