Fixed some minor issues.
This commit is contained in:
parent
7bcb3539e8
commit
c206736195
7
cache.go
7
cache.go
@ -20,11 +20,14 @@ type Cache struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewCache creates a new cache in the specified directory.
|
// NewCache creates a new cache in the specified directory.
|
||||||
func NewCache(directory string) *Cache {
|
func NewCache(directory string) (*Cache, error) {
|
||||||
|
if err := os.MkdirAll(directory, 0775); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
return &Cache{
|
return &Cache{
|
||||||
directory: directory,
|
directory: directory,
|
||||||
downloaders: make(map[string]*Downloader),
|
downloaders: make(map[string]*Downloader),
|
||||||
}
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetReader obtains an io.Reader for the specified rawurl. If a downloader
|
// GetReader obtains an io.Reader for the specified rawurl. If a downloader
|
||||||
|
@ -72,17 +72,18 @@ loop:
|
|||||||
l.err = err
|
l.err = err
|
||||||
break loop
|
break loop
|
||||||
}
|
}
|
||||||
|
}
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case e := <-watcher.Events:
|
case e := <-watcher.Events:
|
||||||
if e.Op&fsnotify.Write == fsnotify.Write {
|
if e.Op&fsnotify.Write != fsnotify.Write {
|
||||||
continue loop
|
continue
|
||||||
}
|
}
|
||||||
case err = <-l.done:
|
case err = <-l.done:
|
||||||
l.err = err
|
l.err = err
|
||||||
l.eof = true
|
l.eof = true
|
||||||
}
|
}
|
||||||
}
|
continue loop
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -91,6 +92,8 @@ loop:
|
|||||||
|
|
||||||
// Close frees resources associated with the reader.
|
// Close frees resources associated with the reader.
|
||||||
func (l *LiveReader) Close() error {
|
func (l *LiveReader) Close() error {
|
||||||
|
if l.file != nil {
|
||||||
l.file.Close()
|
l.file.Close()
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
16
main.go
16
main.go
@ -1,19 +1,29 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"flag"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"syscall"
|
"syscall"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s, err := NewServer(":8000", "/tmp/proxy")
|
var (
|
||||||
|
addr = flag.String("addr", ":8000", "`host:port` to listen on")
|
||||||
|
directory = flag.String("directory", "/var/lib/go-aptproxy", "`directory` used for storing packages")
|
||||||
|
)
|
||||||
|
flag.Parse()
|
||||||
|
s, err := NewServer(*addr, *directory)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
log.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
s.Start()
|
s.Start()
|
||||||
|
defer s.Stop()
|
||||||
|
log.Println("APT proxy started")
|
||||||
c := make(chan os.Signal)
|
c := make(chan os.Signal)
|
||||||
signal.Notify(c, syscall.SIGINT)
|
signal.Notify(c, syscall.SIGINT)
|
||||||
<-c
|
<-c
|
||||||
s.Stop()
|
log.Println("APT proxy shut down by signal")
|
||||||
}
|
}
|
||||||
|
@ -79,9 +79,13 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
|||||||
|
|
||||||
// NewServer creates a new server.
|
// NewServer creates a new server.
|
||||||
func NewServer(addr, directory string) (*Server, error) {
|
func NewServer(addr, directory string) (*Server, error) {
|
||||||
|
c, err := NewCache(directory)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
s := &Server{
|
s := &Server{
|
||||||
server: server.New(addr),
|
server: server.New(addr),
|
||||||
cache: NewCache(directory),
|
cache: c,
|
||||||
}
|
}
|
||||||
s.server.Handler = s
|
s.server.Handler = s
|
||||||
return s, nil
|
return s, nil
|
||||||
@ -95,4 +99,5 @@ func (s *Server) Start() error {
|
|||||||
// Stop shuts down the server.
|
// Stop shuts down the server.
|
||||||
func (s *Server) Stop() {
|
func (s *Server) Stop() {
|
||||||
s.server.Stop()
|
s.server.Stop()
|
||||||
|
s.cache.Close()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user