mirror of https://github.com/dnomd343/XProxy.git
Dnomd343
10 months ago
6 changed files with 93 additions and 16 deletions
@ -0,0 +1,36 @@ |
|||
package upstream |
|||
|
|||
import ( |
|||
"XProxy/logger" |
|||
"io" |
|||
"os" |
|||
"time" |
|||
) |
|||
|
|||
type localAsset struct { |
|||
tag string |
|||
path string |
|||
} |
|||
|
|||
func (l *localAsset) Tag() string { |
|||
return l.tag |
|||
} |
|||
|
|||
func (l *localAsset) lastModify() time.Time { |
|||
stat, err := os.Stat(l.path) |
|||
if err != nil { |
|||
logger.Warnf("Failed to get local file stat -> %v", err) |
|||
return time.Now() // unknown modify time
|
|||
} |
|||
return stat.ModTime() // using last modify time of src file
|
|||
} |
|||
|
|||
func (l *localAsset) Request() (io.ReadCloser, time.Time, error) { |
|||
logger.Debugf("Start extracting local asset `%s`", l.path) |
|||
stream, err := os.Open(l.path) |
|||
if err != nil { |
|||
logger.Errorf("Failed to read local asset -> %v", err) |
|||
return nil, l.lastModify(), err |
|||
} |
|||
return stream, l.lastModify(), nil |
|||
} |
@ -0,0 +1,19 @@ |
|||
package upstream |
|||
|
|||
import ( |
|||
"io" |
|||
"net/http" |
|||
"time" |
|||
) |
|||
|
|||
type remoteAsset struct { |
|||
client http.Client |
|||
} |
|||
|
|||
func (r *remoteAsset) GetTag() string { |
|||
return "" |
|||
} |
|||
|
|||
func (r *remoteAsset) Request() (io.ReadCloser, time.Time, error) { |
|||
return nil, time.Now(), nil |
|||
} |
@ -1,4 +1,4 @@ |
|||
package assets |
|||
package upstream |
|||
|
|||
import ( |
|||
"XProxy/logger" |
@ -0,0 +1,29 @@ |
|||
package upstream |
|||
|
|||
import ( |
|||
"io" |
|||
urlpkg "net/url" |
|||
"time" |
|||
) |
|||
|
|||
// Upstream interface is an abstraction that supports initiating request and
|
|||
// returning information such as data streams.
|
|||
type Upstream interface { |
|||
// Tag function get the description of current upstream.
|
|||
Tag() (tag string) |
|||
|
|||
// Request function initiates a request, returns the data stream and last
|
|||
// modification time, or an error information.
|
|||
Request() (stream io.ReadCloser, lastModify time.Time, err error) |
|||
} |
|||
|
|||
func NewLocalAsset(tag string, path string) Upstream { |
|||
return &localAsset{ |
|||
tag: tag, |
|||
path: path, |
|||
} |
|||
} |
|||
|
|||
func NewRemoteAsset(url string, proxy *urlpkg.URL) Upstream { |
|||
return &remoteAsset{} |
|||
} |
Loading…
Reference in new issue