Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Major refactor, added fileopener and caching
- Loading branch information
Showing
12 changed files
with
518 additions
and
338 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package push | ||
|
||
import "sync" | ||
|
||
// Cache is an interface that allows Middleware and ResponseWriter to cache the results of the list of resources to improve performance. | ||
type Cache interface { | ||
Get(string) ([]string, bool) | ||
Add(string, string) | ||
Del(string) | ||
} | ||
|
||
//////////////// | ||
|
||
type DefaultCache struct { | ||
uris map[string][]string | ||
mutex sync.RWMutex | ||
} | ||
|
||
func NewDefaultCache() *DefaultCache { | ||
return &DefaultCache{make(map[string][]string), sync.RWMutex{}} | ||
} | ||
|
||
func (c *DefaultCache) Get(uri string) ([]string, bool) { | ||
c.mutex.RLock() | ||
defer c.mutex.RUnlock() | ||
|
||
resources, ok := c.uris[uri] | ||
return resources, ok | ||
} | ||
|
||
func (c *DefaultCache) Add(uri string, resource string) { | ||
c.mutex.Lock() | ||
defer c.mutex.Unlock() | ||
|
||
if _, ok := c.uris[uri]; !ok { | ||
c.uris[uri] = []string{resource} | ||
return | ||
} | ||
c.uris[uri] = append(c.uris[uri], resource) | ||
} | ||
|
||
func (c *DefaultCache) Del(uri string) { | ||
c.mutex.Lock() | ||
defer c.mutex.Unlock() | ||
|
||
delete(c.uris, uri) | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package push | ||
|
||
import ( | ||
"io" | ||
"os" | ||
"path" | ||
) | ||
|
||
// FileOpener is an interface that allows the parser to load embedded resources recursively. | ||
type FileOpener interface { | ||
Open(string) (io.Reader, string, error) | ||
} | ||
|
||
type FileOpenerFunc func(string) (io.Reader, string, error) | ||
|
||
func (f FileOpenerFunc) Open(uri string) (io.Reader, string, error) { | ||
return f(uri) | ||
} | ||
|
||
//////////////// | ||
|
||
type DefaultFileOpener struct { | ||
basePath string | ||
} | ||
|
||
func NewDefaultFileOpener(basePath string) *DefaultFileOpener { | ||
return &DefaultFileOpener{basePath} | ||
} | ||
|
||
func (o *DefaultFileOpener) Open(uri string) (io.Reader, string, error) { | ||
r, err := os.Open(path.Join(o.basePath, uri)) | ||
if err != nil { | ||
return nil, "", err | ||
} | ||
return r, ExtToMimetype[path.Ext(uri)], nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package push | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
"sync" | ||
) | ||
|
||
// ErrNoPusher is returned when the ResponseWriter does not implement the Pusher interface. | ||
var ErrNoPusher = errors.New("ResponseWriter is not a Pusher") | ||
|
||
// URIHandler is a callback definition that is called when a resource URI is found. | ||
type URIHandler interface { | ||
URI(string) error | ||
} | ||
|
||
type URIHandlerFunc func(string) error | ||
|
||
func (f URIHandlerFunc) URI(uri string) error { | ||
return f(uri) | ||
} | ||
|
||
//////////////// | ||
|
||
// PushHandler is a URIHandler that pushes resources to the client. | ||
type PushHandler struct { | ||
pusher http.Pusher | ||
opts *http.PushOptions | ||
} | ||
|
||
func NewPushHandler(pusher http.Pusher, opts *http.PushOptions) *PushHandler { | ||
if opts == nil { | ||
opts = &http.PushOptions{"", http.Header{}} | ||
} | ||
opts.Header.Set("X-Pushed", "1") | ||
return &PushHandler{pusher, opts} | ||
} | ||
|
||
func NewPushHandlerFromResponseWriter(w http.ResponseWriter) (*PushHandler, error) { | ||
pusher, ok := w.(http.Pusher) | ||
if !ok { | ||
return nil, ErrNoPusher | ||
} | ||
opts := &http.PushOptions{"", http.Header{}} | ||
opts.Header.Set("X-Pushed", "1") | ||
return &PushHandler{pusher, opts}, nil | ||
} | ||
|
||
func (p *PushHandler) URI(uri string) error { | ||
return p.pusher.Push(uri, p.opts) | ||
} | ||
|
||
//////////////// | ||
|
||
// ListHandler is a URIHandler that collects all resource URIs in a list. | ||
type ListHandler struct { | ||
URIs []string | ||
mutex sync.Mutex | ||
} | ||
|
||
func NewListHandler() *ListHandler { | ||
return &ListHandler{[]string{}, sync.Mutex{}} | ||
} | ||
|
||
func (h *ListHandler) URI(uri string) error { | ||
h.mutex.Lock() | ||
h.URIs = append(h.URIs, uri) | ||
h.mutex.Unlock() | ||
return nil | ||
} |
Oops, something went wrong.