~stephen-stewart/snapweb/client-side-filtering-and-ordering

« back to all changes in this revision

Viewing changes to snappy/handlers.go

  • Committer: Snappy Tarmac
  • Author(s): Sergio Schvezov
  • Date: 2015-04-29 15:10:48 UTC
  • mfrom: (84.8.25 packageInstall)
  • Revision ID: snappy_tarmac-20150429151048-377311dov0usk4tx
Install progress. by sergiusens approved by sergiusens,stephen-stewart

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
        "fmt"
6
6
        "net/http"
7
7
 
 
8
        "log"
 
9
 
 
10
        "launchpad.net/snappy/snappy"
 
11
        "launchpad.net/webdm/webprogress"
 
12
 
8
13
        "github.com/gorilla/mux"
9
14
)
10
15
 
11
 
type handler struct{}
 
16
type handler struct {
 
17
        installStatus *webprogress.Status
 
18
}
12
19
 
13
20
func NewHandler() *handler {
14
 
        return &handler{}
 
21
        return &handler{
 
22
                installStatus: webprogress.New(),
 
23
        }
15
24
}
16
25
 
17
26
func (h *handler) getAll(w http.ResponseWriter, r *http.Request) {
18
 
        payload, err := allPackages()
 
27
        payload, err := h.allPackages()
19
28
        if err != nil {
20
29
                w.WriteHeader(http.StatusInternalServerError)
21
30
                fmt.Fprint(w, fmt.Sprintf("Error: %s", err))
35
44
        vars := mux.Vars(r)
36
45
        pkgName := vars["pkg"]
37
46
 
38
 
        payload, err := packagePayload(pkgName)
 
47
        payload, err := h.packagePayload(pkgName)
39
48
        if err != nil {
40
49
                http.NotFound(w, r)
41
50
                return
47
56
        }
48
57
}
49
58
 
 
59
func (h *handler) add(w http.ResponseWriter, r *http.Request) {
 
60
        // Get the Key.
 
61
        vars := mux.Vars(r)
 
62
        pkgName := vars["pkg"]
 
63
 
 
64
        enc := json.NewEncoder(w)
 
65
        var msg string
 
66
 
 
67
        err := h.installPackage(pkgName)
 
68
 
 
69
        switch err {
 
70
        case snappy.ErrAlreadyInstalled:
 
71
                w.WriteHeader(http.StatusOK)
 
72
                msg = "Installed"
 
73
        case webprogress.ErrPackageInstallInProgress:
 
74
                w.WriteHeader(http.StatusBadRequest)
 
75
                msg = "Installation in progress"
 
76
        case snappy.ErrPackageNotFound:
 
77
                w.WriteHeader(http.StatusNotFound)
 
78
                msg = "Package not found"
 
79
        case nil:
 
80
                w.WriteHeader(http.StatusAccepted)
 
81
                msg = "Accepted"
 
82
        default:
 
83
                w.WriteHeader(http.StatusInternalServerError)
 
84
                msg = "Processing error"
 
85
        }
 
86
 
 
87
        response := Response{Message: msg, Package: pkgName}
 
88
        if err := enc.Encode(response); err != nil {
 
89
                log.Print(err)
 
90
        }
 
91
}
 
92
 
50
93
func (h *handler) MakeMuxer(prefix string) http.Handler {
51
94
        var m *mux.Router
52
95
 
62
105
        // get specific package
63
106
        m.HandleFunc("/{pkg}", h.get).Methods("GET")
64
107
 
 
108
        // Add a package
 
109
        m.HandleFunc("/{pkg}", h.add).Methods("PUT")
 
110
 
65
111
        return m
66
112
}