Exemplo n.º 1
0
func (self *Web) HandleStatic(router *mux.Router, dir string) (err error) {
	children, err := templar.GetMatchingBlobNames(self.env == Development, "^static/.*")
	if err != nil {
		return
	}
	for _, fil := range children {
		cpy := fil
		self.Handle(router.MatcherFunc(func(r *http.Request, rm *mux.RouteMatch) bool {
			return strings.HasSuffix(r.URL.Path, filepath.Base(cpy))
		}), func(c *HTTPContext) (err error) {
			if strings.HasSuffix(c.Req().URL.Path, ".css") {
				c.SetContentType("text/css; charset=UTF-8", true)
			} else if strings.HasSuffix(c.Req().URL.Path, ".js") {
				c.SetContentType("application/javascript; charset=UTF-8", true)
			} else if strings.HasSuffix(c.Req().URL.Path, ".png") {
				c.SetContentType("image/png", true)
			} else if strings.HasSuffix(c.Req().URL.Path, ".gif") {
				c.SetContentType("image/gif", true)
			} else if strings.HasSuffix(c.Req().URL.Path, ".c.Resp()off") {
				c.SetContentType("application/font-c.Resp()off", true)
			} else if strings.HasSuffix(c.Req().URL.Path, ".ttf") {
				c.SetContentType("font/truetype", true)
			} else {
				c.SetContentType("application/octet-stream", true)
			}
			in, err := templar.GetBlob(self.env == Development, cpy)
			if err != nil {
				self.Errorf("%v", err)
				c.Resp().WriteHeader(500)
				fmt.Fprintln(c.Resp(), err)
			} else {
				defer in.Close()
				if _, err = io.Copy(c.Resp(), in); err != nil {
					return
				}
			}
			return
		})
	}
	return
}
Exemplo n.º 2
0
func handleStatic(router *mux.Router, dir string) {
	static, err := os.Open(dir)
	if err != nil {
		panic(err)
	}
	children, err := static.Readdirnames(-1)
	if err != nil {
		panic(err)
	}
	for _, fil := range children {
		cpy := fil
		router.MatcherFunc(func(r *http.Request, rm *mux.RouteMatch) bool {
			return strings.HasSuffix(r.URL.Path, cpy)
		}).HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			if strings.HasSuffix(r.URL.Path, ".css") {
				common.SetContentType(w, "text/css; charset=UTF-8", true)
			} else if strings.HasSuffix(r.URL.Path, ".js") {
				common.SetContentType(w, "application/javascript; charset=UTF-8", true)
			} else if strings.HasSuffix(r.URL.Path, ".png") {
				common.SetContentType(w, "image/png", true)
			} else if strings.HasSuffix(r.URL.Path, ".gif") {
				common.SetContentType(w, "image/gif", true)
			} else if strings.HasSuffix(r.URL.Path, ".woff") {
				common.SetContentType(w, "application/font-woff", true)
			} else if strings.HasSuffix(r.URL.Path, ".ttf") {
				common.SetContentType(w, "font/truetype", true)
			} else {
				common.SetContentType(w, "application/octet-stream", true)
			}
			if in, err := os.Open(filepath.Join("static", cpy)); err != nil {
				w.WriteHeader(500)
			} else {
				defer in.Close()
				if _, err := io.Copy(w, in); err != nil {
					w.WriteHeader(500)
				}
			}
		})
	}
}
Exemplo n.º 3
0
func AddRoutes(router *mux.Router) *mux.Router {
	router.Path("/")

	for acceptHeader, vApi := range acceptVersionMap {
		// Create a subrouter for the header/api version.
		subrouter := router.MatcherFunc(
			acceptOrQueryMatcherFactory(acceptHeader)).Subrouter()

		// Define the path/handler relationships.
		pathHandlerMap := map[string]func(http.ResponseWriter, *http.Request){
			"/json.json":  vApi.JsonHandler,
			"/json2.json": vApi.JsonHandler2,
			"/json3.json": vApi.JsonHandler3,
		}
		// Create a route in the subrouter for each path/handler.
		for path, handler := range pathHandlerMap {
			route := subrouter.HandleFunc(path, handler)
			route.Name(fmt.Sprintf("%s - %s", path, handler))
		}
	}
	return router
}
Exemplo n.º 4
0
func registerFastCGIHandler(r URLRoute, router *mux.Router) {
	_u1, err := url.Parse(r.Path)
	if err != nil {
		fmt.Printf("invalid path: %s\n", r.Path)
		os.Exit(-1)
	}
	_p := _u1.Path
	switch _u1.Scheme {
	case "unix":
	case "tcp":
		_p = _u1.Host
	default:
		fmt.Printf("invalid scheme: %s, only support unix, tcp", _u1.Scheme)
		os.Exit(-1)
	}
	if r.UseRegex {
		m1 := myURLMatch{regexp.MustCompile(r.URLPrefix)}
		_u, _ := NewFastCGI(_u1.Scheme, _p, r.DocRoot, "")
		router.MatcherFunc(m1.match).Handler(_u)
	} else {
		_u, _ := NewFastCGI(_u1.Scheme, _p, r.DocRoot, r.URLPrefix)
		router.PathPrefix(r.URLPrefix).Handler(_u)
	}
}