Fix health checker

This commit is contained in:
Asim 2015-05-21 23:06:01 +01:00
parent c9df1cf7d2
commit d67c1ba111
5 changed files with 61 additions and 17 deletions

View File

@ -9,6 +9,7 @@ import (
"github.com/codegangsta/cli" "github.com/codegangsta/cli"
"github.com/myodc/go-micro/broker" "github.com/myodc/go-micro/broker"
"github.com/myodc/go-micro/client"
"github.com/myodc/go-micro/registry" "github.com/myodc/go-micro/registry"
"github.com/myodc/go-micro/server" "github.com/myodc/go-micro/server"
"github.com/myodc/go-micro/store" "github.com/myodc/go-micro/store"
@ -113,6 +114,8 @@ func Setup(c *cli.Context) error {
transport.DefaultTransport = transport.NewNatsTransport(tAddrs) transport.DefaultTransport = transport.NewNatsTransport(tAddrs)
} }
client.DefaultClient = client.NewRpcClient()
return nil return nil
} }

38
proto/health/health.pb.go Normal file
View File

@ -0,0 +1,38 @@
// Code generated by protoc-gen-go.
// source: github.com/myodc/go-micro/proto/health/health.proto
// DO NOT EDIT!
/*
Package health is a generated protocol buffer package.
It is generated from these files:
github.com/myodc/go-micro/proto/health/health.proto
It has these top-level messages:
Request
Response
*/
package health
import proto "github.com/golang/protobuf/proto"
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
type Request struct {
}
func (m *Request) Reset() { *m = Request{} }
func (m *Request) String() string { return proto.CompactTextString(m) }
func (*Request) ProtoMessage() {}
type Response struct {
Status string `protobuf:"bytes,1,opt,name=status" json:"status,omitempty"`
}
func (m *Response) Reset() { *m = Response{} }
func (m *Response) String() string { return proto.CompactTextString(m) }
func (*Response) ProtoMessage() {}
func init() {
}

View File

@ -0,0 +1,8 @@
syntax = "proto3";
message Request {
}
message Response {
string status = 1;
}

View File

@ -1,21 +1,17 @@
package server package server
import ( import (
"io" "github.com/myodc/go-micro/proto/health"
"net/http" "golang.org/x/net/context"
"net/url"
) )
func registerHealthChecker(mux *http.ServeMux) { type Debug struct{}
req := &http.Request{
Method: "GET", func (d *Debug) Health(ctx context.Context, req *health.Request, rsp *health.Response) error {
URL: &url.URL{ rsp.Status = "ok"
Path: HealthPath, return nil
},
}
if _, path := mux.Handler(req); path != HealthPath {
mux.HandleFunc(HealthPath, func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "ok")
})
} }
func registerHealthChecker(r Server) {
r.Register(r.NewReceiver(&Debug{}))
} }

View File

@ -2,7 +2,6 @@ package server
import ( import (
"bytes" "bytes"
"net/http"
"sync" "sync"
log "github.com/golang/glog" log "github.com/golang/glog"
@ -97,7 +96,7 @@ func (s *RpcServer) Register(r Receiver) error {
} }
func (s *RpcServer) Start() error { func (s *RpcServer) Start() error {
registerHealthChecker(http.DefaultServeMux) registerHealthChecker(s)
ts, err := s.opts.transport.Listen(s.address) ts, err := s.opts.transport.Listen(s.address)
if err != nil { if err != nil {