-
Notifications
You must be signed in to change notification settings - Fork 1
/
fileServer.go
85 lines (72 loc) · 1.97 KB
/
fileServer.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"html/template"
"log"
"net/http"
"path"
"github.com/gorilla/mux"
)
type fileServer interface {
fileHandler(http.ResponseWriter, *http.Request)
}
type FileServer struct {
nfs fileBackend
piInventory piManager
diskManager *diskManager
}
type templatevars struct {
PiId string
NfsServer string
NfsRoot string
}
func newFileServer(nfs fileBackend, inventory piManager, dm *diskManager) (fileServer, error) {
return &FileServer{
nfs: nfs,
piInventory: inventory,
diskManager: dm,
}, nil
}
func (f *FileServer) fileHandler(w http.ResponseWriter, r *http.Request) {
urlvars := mux.Vars(r)
filename := urlvars["filename"]
piId := urlvars["piId"]
//check if piId is allready registered. If not then register.
pi, err := f.piInventory.GetPi(piId)
if err != nil {
log.Println("Pi not found in inventory. Putting a new one in the fridge.")
pi = f.piInventory.NewPi(piId)
err = pi.Save()
if err != nil {
panic(err)
}
}
if pi.Status == NOTINUSE {
//Pi is not in inventory or not in use. Then don't serve files and power it off
log.Printf("Pi %v came online but it's not in use. Powering it off\n", pi.Id)
err = pi.PowerOff()
if err != nil {
log.Println("A Pi just came online but I can't control its power state. Error:" + err.Error())
}
w.WriteHeader(http.StatusNotFound)
return
}
//if filename == cmdline.txt then parse the template. else just serve the file
bootLocation := pi.SourceBakeform.bootLocation
//strings.Replace(bootLocation, "/", "", 1) //remove the first /
fullFilename := path.Join(bootLocation, filename)
if filename != "cmdline.txt" {
http.ServeFile(w, r, fullFilename)
return
} else {
c := templatevars{
NfsServer: f.nfs.GetNfsAddress(),
NfsRoot: pi.Disks[0].Location,
}
log.Printf("%v requested for: %v\n", filename, pi.Id)
t, err := template.New("templatefile").ParseFiles(fullFilename)
if err != nil {
panic(err)
}
t.ExecuteTemplate(w, filename, c)
}
}