2019-10-14 23:39:25 +03:00
|
|
|
// Package server implements the sync http server
|
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2020-01-30 14:39:00 +03:00
|
|
|
"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
|
|
|
|
}
|