Skip to content

Instantly share code, notes, and snippets.

@hectorgool
Created July 31, 2017 15:33
Show Gist options
  • Select an option

  • Save hectorgool/53babaf5bb95c2c62204e324fd5168bb to your computer and use it in GitHub Desktop.

Select an option

Save hectorgool/53babaf5bb95c2c62204e324fd5168bb to your computer and use it in GitHub Desktop.
go-kit + pat sinatra
package main
import (
"context"
"encoding/json"
"errors"
"log"
"net/http"
"strings"
"github.com/bmizerany/pat"
//"github.com/gorilla/mux"
"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) (string, string, error)
Count(string) int
}
type stringService struct{}
func (stringService) Uppercase(id string, s string) (string, string, error) {
if s == "" {
return id, "", ErrEmpty
}
if id == "" {
return "", s, ErrEmpty
}
return id, strings.ToUpper(s), nil
}
func (stringService) Count(s string) int {
return len(s)
}
func main() {
r := pat.New()
//r := mux.NewRouter()
svc := stringService{}
uppercaseHandler := httptransport.NewServer(
makeUppercaseEndpoint(svc),
decodeUppercaseRequest,
encodeResponse,
)
countHandler := httptransport.NewServer(
makeCountEndpoint(svc),
decodeCountRequest,
encodeResponse,
)
//http.Handle("/uppercase", uppercaseHandler)
r.Put("/uppercase/:name", http.Handler(uppercaseHandler))
//r.Methods("POST").Path("/uppercase").Handler(uppercaseHandler)
//http.Handle("/count", countHandler)
r.Post("/count", http.Handler(countHandler))
//r.Methods("POST").Path("/count").Handler(countHandler)
log.Fatal(http.ListenAndServe(":8080", r))
}
func makeUppercaseEndpoint(svc StringService) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
//log.Println(request)
//req := request.(uppercaseRequest)
//log.Println(request.URL.Query().Get(":name"))
req := request.(putUppercaseRequest)
log.Println("++++++++", req.ID)
log.Println("++++++++", req.uppercaseRequest.S)
id, v, err := svc.Uppercase(req.ID, req.uppercaseRequest.S)
if err != nil {
return uppercaseResponse{id, v, err.Error()}, nil
}
return uppercaseResponse{id, v, ""}, 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(_ context.Context, r *http.Request) (interface{}, error) {
var request uppercaseRequest
//log.Println(r.URL.Query().Get(":name"))
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
return nil, err
}
log.Println(request)
//return request, nil
return putUppercaseRequest{
ID: r.URL.Query().Get(":name"),
uppercaseRequest: request,
}, nil
}
func decodeCountRequest(_ context.Context, 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(_ context.Context, w http.ResponseWriter, response interface{}) error {
return json.NewEncoder(w).Encode(response)
}
type uppercaseRequest struct {
S string `json:"s"`
}
type uppercaseResponse struct {
ID string `json:"id"`
V string `json:"v"`
Err string `json:"err,omitempty"` // errors don't define JSON marshaling
}
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")
type putUppercaseRequest struct {
ID string
uppercaseRequest uppercaseRequest
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment