github.com/yasker/longhorn-engine@v0.0.0-20160621014712-6ed6cfca0729/agent/controller/rest/router.go (about) 1 package rest 2 3 import ( 4 "net/http" 5 6 "github.com/gorilla/mux" 7 "github.com/rancher/go-rancher/api" 8 "github.com/rancher/go-rancher/client" 9 10 lclient "github.com/rancher/longhorn/controller/client" 11 ) 12 13 func HandleError(s *client.Schemas, t func(http.ResponseWriter, *http.Request) error) http.Handler { 14 return api.ApiHandler(s, http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { 15 if err := t(rw, req); err != nil { 16 apiContext := api.GetApiContext(req) 17 apiContext.WriteErr(err) 18 } 19 })) 20 } 21 22 func NewRouter(s *Server) *mux.Router { 23 schemas := newSchema() 24 router := mux.NewRouter().StrictSlash(true) 25 f := HandleError 26 27 // API framework routes 28 router.Methods("GET").Path("/").Handler(api.VersionsHandler(schemas, "v1")) 29 router.Methods("GET").Path("/v1/schemas").Handler(api.SchemasHandler(schemas)) 30 router.Methods("GET").Path("/v1/schemas/{id}").Handler(api.SchemaHandler(schemas)) 31 router.Methods("GET").Path("/v1").Handler(api.VersionHandler(schemas, "v1")) 32 33 // Volume(s) 34 router.Methods("GET").Path("/v1/volumes").Handler(f(schemas, s.ListVolumes)) 35 router.Methods("GET").Path("/v1/volumes/{id}").Handler(f(schemas, s.GetVolume)) 36 router.Methods("POST").Path("/v1/volumes/{id}").Queries("action", "reverttosnapshot").Handler(f(schemas, s.RevertToSnapshot)) 37 router.Methods("POST").Path("/v1/volumes/{id}").Queries("action", "restorefrombackup").Handler(f(schemas, s.RestoreFromBackup)) 38 39 // Snapshots 40 router.Methods("GET").Path("/v1/snapshots").Handler(f(schemas, s.ListSnapshots)) 41 router.Methods("GET").Path("/v1/snapshots/{id}").Handler(f(schemas, s.GetSnapshot)) 42 router.Methods("POST").Path("/v1/snapshots").Handler(f(schemas, s.CreateSnapshot)) 43 router.Methods("DELETE").Path("/v1/snapshots/{id}").Handler(f(schemas, s.DeleteSnapshot)) 44 router.Methods("POST").Path("/v1/snapshots/{id}").Queries("action", "backup").Handler(f(schemas, s.CreateBackup)) 45 router.Methods("POST").Path("/v1/snapshots/{id}").Queries("action", "removebackup").Handler(f(schemas, s.RemoveBackup)) 46 47 // Restore status 48 router.Methods("GET").Path("/v1/backupstatuses/{id}").Handler(f(schemas, s.GetBackupStatus)) 49 router.Methods("GET").Path("/v1/restorestatuses/{id}").Handler(f(schemas, s.GetRestoreStatus)) 50 51 return router 52 } 53 54 type Server struct { 55 controllerClient *lclient.ControllerClient 56 } 57 58 func NewServer() *Server { 59 contollerClient := lclient.NewControllerClient("http://localhost:9501") 60 return &Server{ 61 controllerClient: contollerClient, 62 } 63 }