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.
|
||||
func NewCache(directory string) *Cache {
|
||||
func NewCache(directory string) (*Cache, error) {
|
||||
if err := os.MkdirAll(directory, 0775); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Cache{
|
||||
directory: directory,
|
||||
downloaders: make(map[string]*Downloader),
|
||||
}
|
||||
}, nil
|
||||
}
|
||||
|
||||
// GetReader obtains an io.Reader for the specified rawurl. If a downloader
|
||||
|
@ -72,17 +72,18 @@ loop:
|
||||
l.err = err
|
||||
break loop
|
||||
}
|
||||
for {
|
||||
select {
|
||||
case e := <-watcher.Events:
|
||||
if e.Op&fsnotify.Write == fsnotify.Write {
|
||||
continue loop
|
||||
}
|
||||
case err = <-l.done:
|
||||
l.err = err
|
||||
l.eof = true
|
||||
}
|
||||
for {
|
||||
select {
|
||||
case e := <-watcher.Events:
|
||||
if e.Op&fsnotify.Write != fsnotify.Write {
|
||||
continue
|
||||
}
|
||||
case err = <-l.done:
|
||||
l.err = err
|
||||
l.eof = true
|
||||
}
|
||||
continue loop
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -91,6 +92,8 @@ loop:
|
||||
|
||||
// Close frees resources associated with the reader.
|
||||
func (l *LiveReader) Close() error {
|
||||
l.file.Close()
|
||||
if l.file != nil {
|
||||
l.file.Close()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
16
main.go
16
main.go
@ -1,19 +1,29 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
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 {
|
||||
panic(err)
|
||||
log.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
s.Start()
|
||||
defer s.Stop()
|
||||
log.Println("APT proxy started")
|
||||
c := make(chan os.Signal)
|
||||
signal.Notify(c, syscall.SIGINT)
|
||||
<-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.
|
||||
func NewServer(addr, directory string) (*Server, error) {
|
||||
c, err := NewCache(directory)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s := &Server{
|
||||
server: server.New(addr),
|
||||
cache: NewCache(directory),
|
||||
cache: c,
|
||||
}
|
||||
s.server.Handler = s
|
||||
return s, nil
|
||||
@ -95,4 +99,5 @@ func (s *Server) Start() error {
|
||||
// Stop shuts down the server.
|
||||
func (s *Server) Stop() {
|
||||
s.server.Stop()
|
||||
s.cache.Close()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user