Skip to content

Commit 19ecfec

Browse files
Update go version & add verification/testing tools (#93)
<!-- For Work In Progress Pull Requests, please use the Draft PR feature, see https://github.blog/2019-02-14-introducing-draft-pull-requests/ for further details. For a timely review/response, please avoid force-pushing additional commits if your PR already received reviews or comments. Before submitting a Pull Request, please ensure that you have: - 📖 Read the Contributing guide: https://github.com/gorilla/.github/blob/main/CONTRIBUTING.md - 📖 Read the Code of Conduct: https://github.com/gorilla/.github/blob/main/CODE_OF_CONDUCT.md - Provide tests for your changes. - Use descriptive commit messages. - Comment your code where appropriate. - Squash your commits - Update any related documentation. - Add gorilla/pull-request-reviewers as a Reviewer --> ## What type of PR is this? (check all applicable) - [ ] Refactor - [ ] Feature - [ ] Bug Fix - [ ] Optimization - [ ] Documentation Update ## Description ## Related Tickets & Documents <!-- For pull requests that relate or close an issue, please include them below. We like to follow [Github's guidance on linking issues to pull requests](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue). For example having the text: "closes #1234" would connect the current pull request to issue 1234. And when we merge the pull request, Github will automatically close the issue. --> - Related Issue # - Closes # ## Added/updated tests? - [ ] Yes - [ ] No, and this is why: _please replace this line with details on why tests have not been included_ - [ ] I need help with writing tests ## Run verifications and test - [ ] `make verify` is passing - [ ] `make test` is passing
1 parent 9515cd2 commit 19ecfec

25 files changed

+266
-117
lines changed

.circleci/config.yml

-65
This file was deleted.

.editorconfig

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
; https://editorconfig.org/
2+
3+
root = true
4+
5+
[*]
6+
insert_final_newline = true
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
indent_style = space
10+
indent_size = 2
11+
12+
[{Makefile,go.mod,go.sum,*.go,.gitmodules}]
13+
indent_style = tab
14+
indent_size = 4
15+
16+
[*.md]
17+
indent_size = 4
18+
trim_trailing_whitespace = false
19+
20+
eclint_indent_style = unset

.github/workflows/issues.yml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Add issues or pull-requests created to the project.
2+
name: Add issue or pull request to Project
3+
4+
on:
5+
issues:
6+
types:
7+
- opened
8+
pull_request:
9+
types:
10+
- opened
11+
12+
jobs:
13+
add-to-project:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- name: Add issue to project
17+
uses: actions/[email protected]
18+
with:
19+
project-url: https://github.com/orgs/gorilla/projects/4
20+
github-token: ${{ secrets.ADD_TO_PROJECT_TOKEN }}

.github/workflows/test.yml

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
name: CI
2+
on:
3+
push:
4+
branches:
5+
- main
6+
pull_request:
7+
branches:
8+
- main
9+
10+
permissions:
11+
contents: read
12+
13+
jobs:
14+
verify-and-test:
15+
strategy:
16+
matrix:
17+
go: ['1.19','1.20']
18+
os: [ubuntu-latest, macos-latest, windows-latest]
19+
fail-fast: true
20+
runs-on: ${{ matrix.os }}
21+
steps:
22+
- name: Checkout Code
23+
uses: actions/checkout@v3
24+
25+
- name: Setup Go ${{ matrix.go }}
26+
uses: actions/setup-go@v4
27+
with:
28+
go-version: ${{ matrix.go }}
29+
cache: false
30+
31+
- name: Run GolangCI-Lint
32+
uses: golangci/golangci-lint-action@v3
33+
with:
34+
version: v1.53
35+
args: --timeout=5m
36+
37+
- name: Run GoSec
38+
if: matrix.os == 'ubuntu-latest'
39+
uses: securego/gosec@master
40+
with:
41+
args: ./...
42+
43+
- name: Run GoVulnCheck
44+
uses: golang/govulncheck-action@v1
45+
with:
46+
go-version-input: ${{ matrix.go }}
47+
go-package: ./...
48+
49+
- name: Run Tests
50+
run: go test -race -cover -coverprofile=coverage -covermode=atomic -v ./...
51+
52+
- name: Upload coverage to Codecov
53+
uses: codecov/codecov-action@v3
54+
with:
55+
files: ./coverage

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
*~
1+
coverage.coverprofile

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2012 Rodrigo Moraes. All rights reserved.
1+
Copyright (c) 2023 The Gorilla Authors. All rights reserved.
22

33
Redistribution and use in source and binary forms, with or without
44
modification, are permitted provided that the following conditions are

Makefile

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
GO_LINT=$(shell which golangci-lint 2> /dev/null || echo '')
2+
GO_LINT_URI=github.com/golangci/golangci-lint/cmd/golangci-lint@latest
3+
4+
GO_SEC=$(shell which gosec 2> /dev/null || echo '')
5+
GO_SEC_URI=github.com/securego/gosec/v2/cmd/gosec@latest
6+
7+
GO_VULNCHECK=$(shell which govulncheck 2> /dev/null || echo '')
8+
GO_VULNCHECK_URI=golang.org/x/vuln/cmd/govulncheck@latest
9+
10+
.PHONY: golangci-lint
11+
golangci-lint:
12+
$(if $(GO_LINT), ,go install $(GO_LINT_URI))
13+
@echo "##### Running golangci-lint"
14+
golangci-lint run -v
15+
16+
.PHONY: gosec
17+
gosec:
18+
$(if $(GO_SEC), ,go install $(GO_SEC_URI))
19+
@echo "##### Running gosec"
20+
gosec ./...
21+
22+
.PHONY: govulncheck
23+
govulncheck:
24+
$(if $(GO_VULNCHECK), ,go install $(GO_VULNCHECK_URI))
25+
@echo "##### Running govulncheck"
26+
govulncheck ./...
27+
28+
.PHONY: verify
29+
verify: golangci-lint gosec govulncheck
30+
31+
.PHONY: test
32+
test:
33+
@echo "##### Running tests"
34+
go test -race -cover -coverprofile=coverage.coverprofile -covermode=atomic -v ./...

README.md

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
rpc
2-
===
3-
[![Build Status](https://travis-ci.org/gorilla/rpc.png?branch=master)](https://travis-ci.org/gorilla/rpc)
1+
# gorilla/rpc
42

3+
![testing](https://github.com/gorilla/rpc/actions/workflows/test.yml/badge.svg)
4+
[![codecov](https://codecov.io/github/gorilla/rpc/branch/main/graph/badge.svg)](https://codecov.io/github/gorilla/rpc)
5+
[![godoc](https://godoc.org/github.com/gorilla/rpc?status.svg)](https://godoc.org/github.com/gorilla/rpc)
6+
[![sourcegraph](https://sourcegraph.com/github.com/gorilla/rpc/-/badge.svg)](https://sourcegraph.com/github.com/gorilla/rpc?badge)
7+
8+
9+
![Gorilla Logo](https://github.com/gorilla/.github/assets/53367916/d92caabf-98e0-473e-bfbf-ab554ba435e5)
510

611
gorilla/rpc is a foundation for RPC over HTTP services, providing access to the exported methods of an object through HTTP requests.
712

go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/gorilla/rpc
2+
3+
go 1.19

json/client.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
66
package json
77

88
import (
9+
"crypto/rand"
910
"encoding/json"
1011
"errors"
1112
"fmt"
1213
"io"
13-
"math/rand"
14+
"log"
15+
"math"
16+
"math/big"
1417
)
1518

1619
// ----------------------------------------------------------------------------
@@ -37,10 +40,15 @@ type clientResponse struct {
3740

3841
// EncodeClientRequest encodes parameters for a JSON-RPC client request.
3942
func EncodeClientRequest(method string, args interface{}) ([]byte, error) {
43+
val, err := rand.Int(rand.Reader, big.NewInt(int64(math.MaxInt64)))
44+
if err != nil {
45+
log.Fatal(err)
46+
}
47+
4048
c := &clientRequest{
4149
Method: method,
4250
Params: [1]interface{}{args},
43-
Id: uint64(rand.Int63()),
51+
Id: val.Uint64(),
4452
}
4553
return json.Marshal(c)
4654
}

json/json_test.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ func executeRaw(t *testing.T, s *rpc.Server, req interface{}, res interface{}) i
8383
func TestService(t *testing.T) {
8484
s := rpc.NewServer()
8585
s.RegisterCodec(NewCodec(), "application/json")
86-
s.RegisterService(new(Service1), "")
86+
if err := s.RegisterService(new(Service1), ""); err != nil {
87+
t.Fatal(err)
88+
}
8789

8890
var res Service1Response
8991
if err := execute(t, s, "Service1.Multiply", &Service1Request{4, 2}, &res); err != nil {
@@ -109,12 +111,14 @@ func TestServiceBeforeAfter(t *testing.T) {
109111
s.RegisterCodec(NewCodec(), "application/json")
110112
service := &Service1{}
111113
service.beforeAfterContext = map[string]string{}
112-
s.RegisterService(service, "")
114+
if err := s.RegisterService(service, ""); err != nil {
115+
t.Fatal(err)
116+
}
113117

114-
s.RegisterBeforeFunc(func(i *rpc.RequestInfo) {
118+
s.RegisterBeforeFunc(func(_ *rpc.RequestInfo) {
115119
service.beforeAfterContext["before"] = "Before is true"
116120
})
117-
s.RegisterAfterFunc(func(i *rpc.RequestInfo) {
121+
s.RegisterAfterFunc(func(_ *rpc.RequestInfo) {
118122
service.beforeAfterContext["after"] = "After is true"
119123
})
120124

json/server.go

-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ func newCodecRequest(r *http.Request) rpc.CodecRequest {
6969
// Decode the request body and check if RPC method is valid.
7070
req := new(serverRequest)
7171
err := json.NewDecoder(r.Body).Decode(req)
72-
r.Body.Close()
7372
return &CodecRequest{request: req, err: err}
7473
}
7574

protorpc/protorpc_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ func execute(t *testing.T, s *rpc.Server, method string, req, res interface{}) (
6363
func TestService(t *testing.T) {
6464
s := rpc.NewServer()
6565
s.RegisterCodec(NewCodec(), "application/json")
66-
s.RegisterService(new(Service1), "")
66+
if err := s.RegisterService(new(Service1), ""); err != nil {
67+
t.Fatal(err)
68+
}
6769

6870
var res Service1Response
6971
if _, err := execute(t, s, "Service1.Multiply", &Service1Request{4, 2}, &res); err != nil {

protorpc/server.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"errors"
1111
"fmt"
1212
"io"
13+
"log"
1314
"net/http"
1415
"strings"
1516

@@ -78,12 +79,11 @@ func newCodecRequest(r *http.Request) rpc.CodecRequest {
7879
}
7980
req.Method = path[index+1:]
8081
err := json.NewDecoder(r.Body).Decode(&req.Params)
81-
r.Body.Close()
82-
var errr error
82+
var codecErr error
8383
if err != io.EOF {
84-
errr = err
84+
codecErr = err
8585
}
86-
return &CodecRequest{request: req, err: errr}
86+
return &CodecRequest{request: req, err: codecErr}
8787
}
8888

8989
// CodecRequest decodes and encodes a single request.
@@ -138,6 +138,8 @@ func (c *CodecRequest) WriteResponse(w http.ResponseWriter, reply interface{}, m
138138
w.WriteHeader(500)
139139
}
140140
encoder := json.NewEncoder(w)
141-
encoder.Encode(res.Result)
141+
if err := encoder.Encode(res.Result); err != nil {
142+
log.Fatal(err)
143+
}
142144
return nil
143145
}

server_test.go

+15-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package rpc
77

88
import (
9+
"log"
910
"net/http"
1011
"strconv"
1112
"testing"
@@ -88,7 +89,7 @@ type MockCodec struct {
8889
}
8990

9091
func (c MockCodec) NewRequest(*http.Request) CodecRequest {
91-
return MockCodecRequest{c.A, c.B}
92+
return MockCodecRequest(c)
9293
}
9394

9495
type MockCodecRequest struct {
@@ -107,10 +108,14 @@ func (r MockCodecRequest) ReadRequest(args interface{}) error {
107108

108109
func (r MockCodecRequest) WriteResponse(w http.ResponseWriter, reply interface{}, methodErr error) error {
109110
if methodErr != nil {
110-
w.Write([]byte(methodErr.Error()))
111+
if _, err := w.Write([]byte(methodErr.Error())); err != nil {
112+
log.Fatal(err)
113+
}
111114
} else {
112115
res := reply.(*Service1Response)
113-
w.Write([]byte(strconv.Itoa(res.Result)))
116+
if _, err := w.Write([]byte(strconv.Itoa(res.Result))); err != nil {
117+
log.Fatal(err)
118+
}
114119
}
115120
return nil
116121
}
@@ -150,7 +155,9 @@ func TestServeHTTP(t *testing.T) {
150155
expected := A * B
151156

152157
s := NewServer()
153-
s.RegisterService(new(Service1), "")
158+
if err := s.RegisterService(new(Service1), ""); err != nil {
159+
log.Fatal(err)
160+
}
154161
s.RegisterCodec(MockCodec{A, B}, "mock")
155162

156163
r, err := http.NewRequest("POST", "", nil)
@@ -203,7 +210,10 @@ func TestInterception(t *testing.T) {
203210
}
204211

205212
s := NewServer()
206-
s.RegisterService(new(Service1), "")
213+
if err = s.RegisterService(new(Service1), ""); err != nil {
214+
t.Fatal(err)
215+
}
216+
207217
s.RegisterCodec(MockCodec{A, B}, "mock")
208218
s.RegisterInterceptFunc(func(i *RequestInfo) *http.Request {
209219
return r2

0 commit comments

Comments
 (0)