micro/sync/lock/http/server/server.go

46 lines
807 B
Go
Raw Normal View History

2019-10-14 23:39:25 +03:00
// Package server implements the sync http server
package server
import (
"net/http"
"github.com/micro/go-micro/v2/sync/lock"
lkhttp "github.com/micro/go-micro/v2/sync/lock/http"
2019-10-14 23:39:25 +03:00
)
func Handler(lk lock.Lock) http.Handler {
mux := http.NewServeMux()
mux.HandleFunc(lkhttp.DefaultPath, func(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
id := r.Form.Get("id")
if len(id) == 0 {
return
}
switch r.Method {
case "POST":
err := lk.Acquire(id)
if err != nil {
http.Error(w, err.Error(), 500)
}
case "DELETE":
err := lk.Release(id)
if err != nil {
http.Error(w, err.Error(), 500)
}
}
})
return mux
}
func Server(lk lock.Lock) *http.Server {
server := &http.Server{
Addr: lkhttp.DefaultAddress,
Handler: Handler(lk),
}
return server
}