github.com/Cloud-Foundations/Dominator@v0.3.4/lib/url/urlutil/api.go (about)

     1  package urlutil
     2  
     3  import (
     4  	"io"
     5  	"os"
     6  	"time"
     7  
     8  	"github.com/Cloud-Foundations/Dominator/lib/log"
     9  	"github.com/Cloud-Foundations/Dominator/lib/log/debuglogger"
    10  )
    11  
    12  // Open will open the URL given by url. A io.ReadCloser is returned, which must
    13  // be closed.
    14  func Open(url string) (io.ReadCloser, error) {
    15  	return open(url)
    16  }
    17  
    18  type CachedReadCloser struct {
    19  	cache         *os.File
    20  	cacheFilename string
    21  	rawReadCloser io.ReadCloser
    22  }
    23  
    24  func (rc *CachedReadCloser) Close() error {
    25  	return rc.close()
    26  }
    27  
    28  func (rc *CachedReadCloser) Read(p []byte) (int, error) {
    29  	return rc.read(p)
    30  }
    31  
    32  func (rc *CachedReadCloser) SaveCache() error {
    33  	return rc.saveCache()
    34  }
    35  
    36  // WatchUrl watches the URL given by url and yields a new io.ReadCloser
    37  // periodically. If the URL is a local file, a new io.ReadCloser is yielded
    38  // when a new inode is found and it is a regular file. If url is a HTTP/HTTPS
    39  // URL a new io.ReadCloser is yielded every checkInterval.
    40  // Each yielded io.ReadCloser must be closed after use.
    41  // Any errors are logged to the logger.
    42  func WatchUrl(url string, checkInterval time.Duration,
    43  	logger log.Logger) (<-chan io.ReadCloser, error) {
    44  	return watchUrl(url, checkInterval, debuglogger.Upgrade(logger))
    45  }
    46  
    47  // WatchUrlWithCache is similar to WatchUrl, except that data are cached.
    48  // A cached copy of the data is stored in the file named cacheFilename when the
    49  // SaveCache method is called. This file is read at startup if the URL is not
    50  // available before the initialTimeout.
    51  // Any errors are logged to the logger.
    52  func WatchUrlWithCache(url string, checkInterval time.Duration,
    53  	cacheFilename string, initialTimeout time.Duration,
    54  	logger log.Logger) (<-chan *CachedReadCloser, error) {
    55  	return watchUrlWithCache(url, checkInterval, cacheFilename, initialTimeout,
    56  		debuglogger.Upgrade(logger))
    57  }