-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
=charlie
committed
Nov 11, 2021
0 parents
commit 8d2d1d4
Showing
9 changed files
with
835 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 | ||
} | ||
} |
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
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" | ||
} |
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
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) | ||
} |
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
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 |
Large diffs are not rendered by default.
Oops, something went wrong.
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
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) | ||
} |