Skip to content

Commit

Permalink
http test
Browse files Browse the repository at this point in the history
  • Loading branch information
=charlie committed Nov 11, 2021
0 parents commit 8d2d1d4
Show file tree
Hide file tree
Showing 9 changed files with 835 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/gomicro.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions Services/UserEndpoint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package Services

import (
"context"
"github.com/go-kit/kit/endpoint"
)

type UserRequest struct {
Uid int `json:"uid"`
}

type UserResponse struct {
Result string `json:"result"`
}

func GenUserEndpoint(userService IUserService) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (response interface{}, err error) {
r :=request.(UserRequest)
result := userService.GetName(r.Uid)
return UserResponse{Result: result},nil
}
}
15 changes: 15 additions & 0 deletions Services/UserService.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package Services

type IUserService interface {
GetName(userid int) string
}

type UserService struct {
}

func (this UserService) GetName(userid int) string {
if userid == 101 {
return "charlie"
}
return "guest"
}
27 changes: 27 additions & 0 deletions Services/UserTransport.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package Services

import (
"context"
"encoding/json"
"errors"
"net/http"
"strconv"
)

func DecodeUserRequest(c context.Context, r *http.Request) (interface{},error) {
// http://localhost:xxx/?uid=101
if r.URL.Query().Get("uid") != "" {
uid,_ := strconv.Atoi(r.URL.Query().Get("uid"))
return UserRequest{
Uid:uid,
},nil
}

return nil,errors.New("参数错误")
}


func EncodeUserResponse(ctx context.Context,w http.ResponseWriter,response interface{}) error {
w.Header().Set("Content-type","application/json")
return json.NewEncoder(w).Encode(response)
}
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module service.gomicro.test

go 1.16

require github.com/go-kit/kit v0.12.0
727 changes: 727 additions & 0 deletions go.sum

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

import (
httptransport "github.com/go-kit/kit/transport/http"
"net/http"
. "service.gomicro.test/Services"
)

func main() {
user := UserService{}
endp:=GenUserEndpoint(user)
serverHandler := httptransport.NewServer(endp,DecodeUserRequest,EncodeUserResponse)
http.ListenAndServe(":8080",serverHandler)
}

0 comments on commit 8d2d1d4

Please sign in to comment.