handler.go 725 B

123456789101112131415161718192021222324252627282930
  1. package v2
  2. import (
  3. "net/http"
  4. "github.com/gorilla/mux"
  5. "github.com/coreos/go-etcd/etcd"
  6. )
  7. const prefix = "/_etcd/mod/lock"
  8. // handler manages the lock HTTP request.
  9. type handler struct {
  10. *mux.Router
  11. client *etcd.Client
  12. }
  13. // NewHandler creates an HTTP handler that can be registered on a router.
  14. func NewHandler(addr string) (http.Handler) {
  15. h := &handler{
  16. Router: mux.NewRouter(),
  17. client: etcd.NewClient([]string{addr}),
  18. }
  19. h.StrictSlash(false)
  20. h.HandleFunc("/{key:.*}", h.getIndexHandler).Methods("GET")
  21. h.HandleFunc("/{key:.*}", h.acquireHandler).Methods("POST")
  22. h.HandleFunc("/{key:.*}", h.renewLockHandler).Methods("PUT")
  23. h.HandleFunc("/{key:.*}", h.releaseLockHandler).Methods("DELETE")
  24. return h
  25. }