forked from harness/gitness
/
example.go
70 lines (63 loc) · 1.87 KB
/
example.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package main
import (
"encoding/hex"
"fmt"
"github.com/GeertJohan/go.rice"
"github.com/davecgh/go-spew/spew"
"log"
"net/http"
"os"
"text/template"
)
func main() {
box, err := rice.FindBox("example-files")
if err != nil {
log.Fatalf("error opening rice.Box: %s\n", err)
}
// spew.Dump(box)
contentString, err := box.String("file.txt")
if err != nil {
log.Fatalf("could not read file contents as string: %s\n", err)
}
log.Printf("Read some file contents as string:\n%s\n", contentString)
contentBytes, err := box.Bytes("file.txt")
if err != nil {
log.Fatalf("could not read file contents as byteSlice: %s\n", err)
}
log.Printf("Read some file contents as byteSlice:\n%s\n", hex.Dump(contentBytes))
file, err := box.Open("file.txt")
if err != nil {
log.Fatalf("could not open file: %s\n", err)
}
spew.Dump(file)
// debianFile, err := box.Open("debian-7.3.0-amd64-i386-netinst.iso")
// if err != nil {
// log.Fatalf("error opening file debian-7.3.0-amd64-i386-netinst.iso: %v", err)
// }
// info, err := debianFile.Stat()
// if err != nil {
// log.Fatalf("error doing stat for debian file: %v", err)
// }
// log.Printf("debian file was last modified at %v\n", info.ModTime())
// log.Printf("debian file is %d bytes large\n", info.Size())
// find/create a rice.Box
templateBox, err := rice.FindBox("example-templates")
if err != nil {
log.Fatal(err)
}
// get file contents as string
templateString, err := templateBox.String("message.tmpl")
if err != nil {
log.Fatal(err)
}
// parse and execute the template
tmplMessage, err := template.New("message").Parse(templateString)
if err != nil {
log.Fatal(err)
}
tmplMessage.Execute(os.Stdout, map[string]string{"Message": "Hello, world!"})
http.Handle("/", http.FileServer(box.HTTPBox()))
go http.ListenAndServe(":8080", nil)
fmt.Printf("Serving files on :8080, press ctrl-C to exit")
select {}
}