Fixed some minor issues.

This commit is contained in:
Nathan Osman 2016-04-26 15:15:31 -07:00
parent 7bcb3539e8
commit c206736195
4 changed files with 37 additions and 16 deletions

View File

@ -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

View File

@ -72,17 +72,18 @@ loop:
l.err = err l.err = err
break loop break loop
} }
for { }
select { for {
case e := <-watcher.Events: select {
if e.Op&fsnotify.Write == fsnotify.Write { case e := <-watcher.Events:
continue loop if e.Op&fsnotify.Write != fsnotify.Write {
} continue
case err = <-l.done:
l.err = err
l.eof = true
} }
case err = <-l.done:
l.err = err
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 {
l.file.Close() if l.file != nil {
l.file.Close()
}
return nil return nil
} }

16
main.go
View File

@ -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")
} }

View File

@ -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()
} }