Last active
April 8, 2017 11:31
-
-
Save microo8/53684deb27f06738073177968648b89c to your computer and use it in GitHub Desktop.
helloservice
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package helloservice | |
//HelloService is the Service with its public API | |
type HelloService struct { | |
HelloEndpoint chan *HelloReq //Endpoint on which the service listens | |
} | |
//NewHelloService returns a new Service | |
func NewHelloService() *HelloService { | |
return &HelloService{ | |
HelloEndpoint1: make(chan *HelloReq), | |
} | |
} | |
//Run spins up an go routine in which the Service processes its requests | |
func (s *HelloService) Run() { | |
go func() { | |
for { | |
select { | |
case req := <-s.HelloEndpoint: | |
go s.ProcessHelloReq(req) | |
} | |
} | |
}() | |
} | |
//ProcessHelloReq processes request HelloReq and returns an response | |
func (s *HelloService) ProcessHelloReq(req *HelloReq) { | |
req.Resp <- &HelloResp{ | |
Result: "Hello " + req.Query, | |
} | |
} | |
//HelloReq used to send request to the HelloEndpoint of Service | |
type HelloReq struct { | |
Query string | |
Resp chan *HelloResp | |
} | |
//HelloResp used as an response from the HelloEndpoint of Service | |
type HelloResp struct { | |
Result string | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment