Jelajahi Sumber

A bit of cleanup

spsobole 3 tahun lalu
induk
melakukan
7842c7d5b8
6 mengubah file dengan 61 tambahan dan 3 penghapusan
  1. 42 0
      README.md
  2. 1 1
      bundle.go
  3. 3 2
      example/server.go
  4. 2 0
      http_fs.go
  5. 5 0
      log.go
  6. 8 0
      overlay.go

+ 42 - 0
README.md

@@ -1,2 +1,44 @@
 # vessel
+Implements a golang library and command line tool for embedding contents of a zip file into a golang binary for the 
+purpose of packing the binary and content into a single file for distribution.
+
+The zip file is appended to the back of the binary, and the provided API allows us to access the zipped 
+files directly.
+
+
+# bundling
+
+In the example below, vesselcmd will zip up the content of **site/** and attach it to the golang binary **server** 
+```
+./vesselcmd build --overwrite --bundle=./server.exe --src=site/
+```
+
+# Accessing the bundled resources
+
+Once bundled the golang program can acess the content via:
+```
+	b, err := vessel.Self()
+	if err != nil {
+		panic(err)
+	}
+    
+    fs := b.FileSystem() // this returns an htp.FileSyste 
+```
+
+We can then directly use the returned filesystem to serve http pages/content
+```
+    http.Handle("/", http.FileServer(fs))
+```
+
+Or we can access the files ourselves by using the http.FileSystem interfaces
+```
+		f, err := fs.Open("/templates/list.html")
+        if err != nil {
+            return err
+        }
+        defer f.Close()
+		data, err := ioutil.ReadAll(f)
+```
+
+
 

+ 1 - 1
bundle.go

@@ -69,7 +69,7 @@ func (b *Bundle) FileSystem() *FileSystem {
 	}
 }
 
-// tryLoadEmbededBundle loads the bundle from an embeded file
+// tryLoadEmbeddedBundle loads the bundle from an embedded file
 func (b *Bundle) tryLoadEmbeddedBundle() error {
 	// Check version 1
 	_, err := b.file.Seek(int64(-24), io.SeekEnd)

+ 3 - 2
example/server.go

@@ -34,8 +34,8 @@ func main() {
 	}
 
 	// We're using an overlay FS so that we can transparently prefer things
-	// localy over our built bundle in the source for testing.
-	// this lets us make uick changes to the site/ content without having to regenerate a bundle
+	// locally over our built bundle in the source for testing.
+	// this lets us make quick changes to the site/ content without having to regenerate a bundle
 	o := vessel.NewOverlayFS()
 	if *localContentFlag != "" {
 		o.Append(http.FileSystem(http.Dir(*localContentFlag)))
@@ -50,6 +50,7 @@ func main() {
 			w.Write([]byte(err.Error()))
 			return
 		}
+		defer f.Close()
 
 		data, err := ioutil.ReadAll(f)
 		if err != nil {

+ 2 - 0
http_fs.go

@@ -170,6 +170,7 @@ func (f *FileSystem) Open(name string) (http.File, error) {
 	return nil, os.ErrNotExist
 }
 
+// Stat returns file info for a file in the bundle filesystem
 func (f *FileSystem) Stat(name string) (os.FileInfo, error) {
 	z, err := f.get(name)
 	if err != nil {
@@ -178,6 +179,7 @@ func (f *FileSystem) Stat(name string) (os.FileInfo, error) {
 	return z.FileInfo(), nil
 }
 
+// Dir return a list of files contained in a subdirectory of our bundle
 func (f *FileSystem) Dir(path string) ([]os.FileInfo, error) {
 	zr, err := zip.NewReader(f.b, f.b.Size())
 	if err != nil {

+ 5 - 0
log.go

@@ -6,22 +6,27 @@ import (
 
 var log Logger = &NilLogger{}
 
+// Logger provides an interface to get at log data for vessel
 type Logger interface {
 	Log(format string, a ...interface{})
 }
 
+// NilLogger dumps all logs to nil
 type NilLogger struct {
 }
 
 func (l NilLogger) Log(format string, a ...interface{}) {}
 
+// DebugLogger defines s simple logger that dumps files to stdout
 type DebugLogger struct {
 }
 
+// Log implements Logger.Log interface
 func (l DebugLogger) Log(format string, a ...interface{}) {
 	golog.Printf(format, a...)
 }
 
+// SetLogger sets the logger to use for vessel
 func SetLogger(l Logger) {
 	if l != nil {
 		log = l

+ 8 - 0
overlay.go

@@ -5,18 +5,26 @@ import (
 	"os"
 )
 
+// OverlayFS allows us to stack multiple overlapping http.FileSystems and search for a file in them in order
 type OverlayFS struct {
 	search []http.FileSystem
 }
 
+// verify we implement the http.FileSystem interface
+var _ http.FileSystem = (*OverlayFS)(nil)
+
+// NewOverlayFS returns a new overlay FileSystem
 func NewOverlayFS() *OverlayFS {
 	return &OverlayFS{}
 }
 
+// Append appends a filesystem to our overlay group.
+//   Filesystems are searched in the order they are appended
 func (f *OverlayFS) Append(fs http.FileSystem) {
 	f.search = append(f.search, fs)
 }
 
+// Open implements http.FileSystem
 func (f *OverlayFS) Open(name string) (http.File, error) {
 	for _, source := range f.search {
 		file, err := source.Open(name)