Skip to content

Commit

Permalink
day23 RPC go-kit microservices
Browse files Browse the repository at this point in the history
  • Loading branch information
gunjan5 committed Sep 4, 2015
1 parent 293f494 commit 210654b
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 5 deletions.
8 changes: 3 additions & 5 deletions day22-slices-errors/slices.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@ import (
func check(e error) {
if e != nil {
panic(e)
os.Exit(1)
}
}

func main() {
f, err := os.Open("test.txt")
if err != nil {
fmt.Printf("%s\n", err)
os.Exit(1)
}
check(err)
defer f.Close()

b := make([]byte, 100)
Expand All @@ -37,7 +35,7 @@ func main() {
// os.Exit(1)
// }
//defer w.Close()
blob := "hi this is golang program, I'm trying to write to you Miss writeMe.txt, hope you like it!\n\n"
blob := "hi this is golang program slices.go, I'm trying to write to you Miss writeMe.txt, hope you like it!\n\n"

err = ioutil.WriteFile("writeMe.txt", []byte(blob), 0777)
check(err)
Expand Down
4 changes: 4 additions & 0 deletions day23-RPC-go-kit/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
go-kit to make microservices architecture app
okay
bye
g2g
112 changes: 112 additions & 0 deletions day23-RPC-go-kit/rpc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package main

import (
"encoding/json"
"errors"
"log"
"net/http"
"strings"

"golang.org/x/net/context"

"github.com/go-kit/kit/endpoint"
httptransport "github.com/go-kit/kit/transport/http"
)

// StringService provides operations on strings.
type StringService interface {
Uppercase(string) (string, error)
Count(string) int
}

type stringService struct{}

func (stringService) Uppercase(s string) (string, error) {
if s == "" {
return "", ErrEmpty
}
return strings.ToUpper(s), nil
}

func (stringService) Count(s string) int {
return len(s)
}

func main() {
ctx := context.Background()
svc := stringService{}

uppercaseHandler := httptransport.Server{
Context: ctx,
Endpoint: makeUppercaseEndpoint(svc),
DecodeRequestFunc: decodeUppercaseRequest,
EncodeResponseFunc: encodeResponse,
}

countHandler := httptransport.Server{
Context: ctx,
Endpoint: makeCountEndpoint(svc),
DecodeRequestFunc: decodeCountRequest,
EncodeResponseFunc: encodeResponse,
}

http.Handle("/uppercase", uppercaseHandler)
http.Handle("/count", countHandler)
log.Fatal(http.ListenAndServe(":8080", nil))
}

func makeUppercaseEndpoint(svc StringService) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(uppercaseRequest)
v, err := svc.Uppercase(req.S)
return uppercaseResponse{v, err}, nil
}
}

func makeCountEndpoint(svc StringService) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(countRequest)
v := svc.Count(req.S)
return countResponse{v}, nil
}
}

func decodeUppercaseRequest(r *http.Request) (interface{}, error) {
var request uppercaseRequest
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
return nil, err
}
return request, nil
}

func decodeCountRequest(r *http.Request) (interface{}, error) {
var request countRequest
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
return nil, err
}
return request, nil
}

func encodeResponse(w http.ResponseWriter, response interface{}) error {
return json.NewEncoder(w).Encode(response)
}

type uppercaseRequest struct {
S string `json:"s"`
}

type uppercaseResponse struct {
V string `json:"v"`
Err error `json:"err"`
}

type countRequest struct {
S string `json:"s"`
}

type countResponse struct {
V int `json:"v"`
}

// ErrEmpty is returned when an input string is empty.
var ErrEmpty = errors.New("empty string")

0 comments on commit 210654b

Please sign in to comment.