예제 #1
0
파일: kuisp.go 프로젝트: gashcrumb/kuisp
func defaultPageHandler(defaultPage string, httpDir http.Dir, fsHandler http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if _, err := httpDir.Open(r.URL.Path); err != nil {
			if defaultFile, err := httpDir.Open(defaultPage); err == nil {
				if stat, err := defaultFile.Stat(); err == nil {
					http.ServeContent(w, r, stat.Name(), stat.ModTime(), defaultFile)
				}
			}
		} else {
			fsHandler.ServeHTTP(w, r)
		}
	})
}
예제 #2
0
func serveFile(c *routing.Context, dir http.Dir, path string) error {
	file, err := dir.Open(path)
	if err != nil {
		return routing.NewHTTPError(http.StatusNotFound, err.Error())
	}
	defer file.Close()
	fstat, err := file.Stat()
	if err != nil {
		return routing.NewHTTPError(http.StatusNotFound, err.Error())
	} else if fstat.IsDir() {
		return routing.NewHTTPError(http.StatusNotFound)
	}
	http.ServeContent(c.Response, c.Request, path, fstat.ModTime(), file)
	return nil
}
예제 #3
0
func assetHandler(rootDir http.Dir, w http.ResponseWriter, r *http.Request) {
	filePath := strings.TrimPrefix(r.URL.Path, fmt.Sprintf("/%s", rootDir))

	fileToServe, err := rootDir.Open(filePath)

	if err != nil {
		log.Print(err)
		http.NotFound(w, r)
		return
	}

	defer fileToServe.Close()

	var fileContent []byte

	for {
		buffer := make([]byte, 2048)
		_, err = fileToServe.Read(buffer)

		if err == io.EOF {
			break
		}
		if err != nil {
			log.Fatal(err)
		}

		for _, b := range buffer {
			if b == 0 {
				break
			}
			fileContent = append(fileContent, b)
		}
	}

	fmt.Fprintf(w, "%s", fileContent)
}
예제 #4
0
파일: kuisp.go 프로젝트: KurtStam/kuisp
func defaultPageHandler(defaultPage string, httpDir http.Dir, fsHandler http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if _, err := httpDir.Open(r.URL.Path); err != nil {
			splitPath := strings.Split(r.URL.Path, "/")
			for {
				p := append(splitPath, defaultPage)
				dp := path.Join(p...)
				if defaultFile, err := httpDir.Open(dp); err == nil {
					if stat, err := defaultFile.Stat(); err == nil {
						http.ServeContent(w, r, stat.Name(), stat.ModTime(), defaultFile)
						return
					}
				}
				if len(splitPath) == 0 {
					http.NotFound(w, r)
					return
				}
				splitPath = splitPath[:len(splitPath)-1]
			}
		} else {
			fsHandler.ServeHTTP(w, r)
		}
	})
}
예제 #5
0
//try get file
func get_file(root http.Dir, path string) (http.File, bool) {
	if path[len(path)-1] == '/' {
		path += "index"
	}
	if fd, err := root.Open(path); err == nil {
		return fd, false
	}
	if fd, err := root.Open(path + ".html"); err == nil {
		return fd, false
	}
	if fd, err := root.Open(path + ".md"); err == nil {
		return fd, true
	}
	return nil, false
}
예제 #6
0
//Проверка наличия файла в директории.
func checkDir(dir http.Dir, name string) {
	_, err := dir.Open(name)
	if err != nil {
		log.Fatal("Missing static directory or files in it.")
	}
}