Created
August 14, 2023 13:50
-
-
Save johnbedeir/25404708e352e4c292b5704de70b7828 to your computer and use it in GitHub Desktop.
How to use Go Kit?
This file contains 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
# Go kit Introduction | |
Go kit is a library for building microservices in Go. It offers tools that facilitate the implementation of microservices in a robust and scalable manner. | |
## Getting Started | |
### 1. Setup | |
Ensure Go is installed. Install the Go kit library: | |
```bash | |
go get github.com/go-kit/kit | |
``` | |
### 2. Create a Service | |
Organize your functionality around a "service". Here's a basic `StringService`: | |
```go | |
type StringService interface { | |
Uppercase(string) (string, error) | |
} | |
type stringService struct{} | |
func (stringService) Uppercase(s string) (string, error) { | |
if s == "" { | |
return "", errors.New("empty string") | |
} | |
return strings.ToUpper(s), nil | |
} | |
``` | |
### 3. Define Endpoints | |
For each method of our service, define an endpoint: | |
```go | |
type Endpoints struct { | |
UppercaseEndpoint endpoint.Endpoint | |
} | |
func MakeUppercaseEndpoint(s StringService) endpoint.Endpoint { | |
return func(ctx context.Context, request interface{}) (interface{}, error) { | |
req := request.(uppercaseRequest) | |
v, err := s.Uppercase(req.S) | |
return uppercaseResponse{v, err}, nil | |
} | |
} | |
type uppercaseRequest struct { | |
S string `json:"s"` | |
} | |
type uppercaseResponse struct { | |
V string `json:"v"` | |
Err error `json:"err,omitempty"` | |
} | |
``` | |
### 4. Transport | |
Separate the transport logic (HTTP, gRPC, etc.) from your business logic: | |
```go | |
func MakeHTTPHandler(s StringService) http.Handler { | |
r := mux.NewRouter() | |
r.Methods("POST").Path("/uppercase").Handler(httptransport.NewServer( | |
MakeUppercaseEndpoint(s), | |
decodeUppercaseRequest, | |
encodeResponse, | |
)) | |
return r | |
} | |
func decodeUppercaseRequest(_ context.Context, 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 encodeResponse(_ context.Context, w http.ResponseWriter, response interface{}) error { | |
return json.NewEncoder(w).Encode(response) | |
} | |
``` | |
### 5. Running the Service | |
Run your service with the following: | |
```go | |
func main() { | |
svc := stringService{} | |
handler := MakeHTTPHandler(svc) | |
http.ListenAndServe(":8080", handler) | |
} | |
``` | |
Visit `http://localhost:8080/uppercase` with a POST request containing JSON like `{"s": "hello"}` to test. | |
--- | |
For a more comprehensive dive into Go kit, refer to the [official documentation](https://gokit.io/). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment