项目作者: vicanso

项目描述 :
Static serve middleware for elton.
高级语言: Go
项目地址: git://github.com/vicanso/elton-static-serve.git
创建时间: 2019-02-22T13:24:03Z
项目社区:https://github.com/vicanso/elton-static-serve

开源协议:Apache License 2.0

下载


elton-static-serve

The middleware has been archived, please use the middleware of elton.

Build Status

Static serve for elton, it use to serve static file, such as html, image and etc.

  1. package main
  2. import (
  3. "bytes"
  4. "io"
  5. "os"
  6. packr "github.com/gobuffalo/packr/v2"
  7. "github.com/vicanso/elton"
  8. staticServe "github.com/vicanso/elton-static-serve"
  9. )
  10. var (
  11. box = packr.New("asset", "./")
  12. )
  13. type (
  14. staticFile struct {
  15. box *packr.Box
  16. }
  17. )
  18. func (sf *staticFile) Exists(file string) bool {
  19. return sf.box.Has(file)
  20. }
  21. func (sf *staticFile) Get(file string) ([]byte, error) {
  22. return sf.box.Find(file)
  23. }
  24. func (sf *staticFile) Stat(file string) os.FileInfo {
  25. return nil
  26. }
  27. func (sf *staticFile) NewReader(file string) (io.Reader, error) {
  28. buf, err := sf.Get(file)
  29. if err != nil {
  30. return nil, err
  31. }
  32. return bytes.NewReader(buf), nil
  33. }
  34. func main() {
  35. e := elton.New()
  36. sf := &staticFile{
  37. box: box,
  38. }
  39. // static file route
  40. e.GET("/static/*file", staticServe.New(sf, staticServe.Config{
  41. // 客户端缓存一年
  42. MaxAge: 365 * 24 * 3600,
  43. // 缓存服务器缓存一个小时
  44. SMaxAge: 60 * 60,
  45. DenyQueryString: true,
  46. DisableLastModified: true,
  47. }))
  48. err := e.ListenAndServe(":7001")
  49. if err != nil {
  50. panic(err)
  51. }
  52. }
  1. package main
  2. import (
  3. "github.com/vicanso/elton"
  4. staticServe "github.com/vicanso/elton-static-serve"
  5. )
  6. func main() {
  7. e := elton.New()
  8. sf := new(staticServe.FS)
  9. // static file route
  10. e.GET("/*file", staticServe.New(sf, staticServe.Config{
  11. Path: "/tmp",
  12. // 客户端缓存一年
  13. MaxAge: 365 * 24 * 3600,
  14. // 缓存服务器缓存一个小时
  15. SMaxAge: 60 * 60,
  16. DenyQueryString: true,
  17. DisableLastModified: true,
  18. // packr不支持Stat,因此需要用强ETag
  19. EnableStrongETag: true,
  20. }))
  21. err := e.ListenAndServe(":3000")
  22. if err != nil {
  23. panic(err)
  24. }
  25. }