Skip to content

Commit

Permalink
Feat/upgrade go version and all dependencies (#67)
Browse files Browse the repository at this point in the history
* upgrade go and refactor deprecated functions

* fix broken updates

* adjust consul structure

* adjust ci's golang version

* fix broken tests

* gofmt codebase
  • Loading branch information
sillygod authored Apr 12, 2023
1 parent 64780f3 commit a8055c8
Show file tree
Hide file tree
Showing 26 changed files with 683 additions and 1,450 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: set up go 1.x
uses: actions/setup-go@v2
with:
go-version: '1.16.4'
go-version: '1.20'
id: go

# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: set up go 1.x
uses: actions/setup-go@v2
with:
go-version: '1.16.4'
go-version: '1.20'
id: go

# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- name: set up go 1.x
uses: actions/setup-go@v2
with:
go-version: '1.16.4'
go-version: '1.20'
id: go

- name: checkout
Expand Down
6 changes: 5 additions & 1 deletion backends/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (b *Base) Close() error {

// GetReader no reader for base backend
func (b *Base) GetReader() (io.ReadCloser, error) {
return nil, errors.New("Private responses are not readable")
return nil, errors.New("private responses are not readable")
}

// WrapResponseWriterToBackend wrap the responseWriter to match the backend's interface
Expand All @@ -61,3 +61,7 @@ func WrapResponseWriterToBackend(w http.ResponseWriter) Backend {
w: w,
}
}

var (
_ Backend = (*Base)(nil)
)
7 changes: 0 additions & 7 deletions backends/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,6 @@ func (b *BackendTestSuite) TestFlush() {
b.Nil(err)
}

func (b *BackendTestSuite) TestWrapResponseWriterToBackend() {
w := httptest.NewRecorder()
back := WrapResponseWriterToBackend(w)
_, ok := back.(Backend)
b.True(ok, "WrapResponseWriterToBackend should make the responseWriter to match the interface of Backend")
}

func TestBackendTestSuite(t *testing.T) {
suite.Run(t, new(BackendTestSuite))
}
3 changes: 1 addition & 2 deletions backends/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package backends

import (
"io"
"io/ioutil"
"os"
"sync"
)
Expand All @@ -20,7 +19,7 @@ func NewFileBackend(path string) (Backend, error) {
return nil, err
}

file, err := ioutil.TempFile(path, "caddy-cache-")
file, err := os.CreateTemp(path, "caddy-cache-")
if err != nil {
return nil, err
}
Expand Down
8 changes: 1 addition & 7 deletions backends/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import (
"bytes"
"context"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"sync"
"time"
Expand Down Expand Up @@ -149,10 +147,6 @@ func NewInMemoryBackend(ctx context.Context, key string, expiration time.Time) (
return i, nil
}

func (i *InMemoryBackend) composeKey(key string, expiration time.Time) string {
return fmt.Sprintf("%s:%d", key, expiration.Unix())
}

// Write adds the response content in the context for the groupcache's
// setter function.
func (i *InMemoryBackend) Write(p []byte) (n int, err error) {
Expand Down Expand Up @@ -209,6 +203,6 @@ func (i *InMemoryBackend) GetReader() (io.ReadCloser, error) {

}

rc := ioutil.NopCloser(bytes.NewReader(i.cachedBytes))
rc := io.NopCloser(bytes.NewReader(i.cachedBytes))
return rc, nil
}
6 changes: 3 additions & 3 deletions backends/memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package backends

import (
"context"
"io/ioutil"
"io"
"testing"
"time"

Expand Down Expand Up @@ -40,7 +40,7 @@ func (suite *MemoryBackendTestSuite) TestWriteInCache() {
// test the content get from the reader will be consistent with the original one
reader, err := backend.GetReader()
suite.Nil(err)
result, err := ioutil.ReadAll(reader)
result, err := io.ReadAll(reader)
suite.Nil(err)
suite.Equal(result, content)
}
Expand All @@ -64,7 +64,7 @@ func (suite *MemoryBackendTestSuite) TestReadExistingCacheInGroupCache() {
reader, err := anotherBackend.GetReader()
suite.Assert().NoError(err)

result, err := ioutil.ReadAll(reader)
result, err := io.ReadAll(reader)
suite.Assert().NoError(err)
suite.Equal(result, content)

Expand Down
15 changes: 8 additions & 7 deletions backends/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import (
"bytes"
"context"
"io"
"io/ioutil"
"strconv"
"strings"
"time"

"github.com/go-redis/redis"
"github.com/redis/go-redis/v9"
)

var (
Expand Down Expand Up @@ -61,7 +60,9 @@ func InitRedisClient(addr, password string, db int) error {
DB: db,
})

if _, err := client.Ping().Result(); err != nil {
ctx := context.Background()

if _, err := client.Ping(ctx).Result(); err != nil {
return err
}

Expand Down Expand Up @@ -94,23 +95,23 @@ func (r *RedisBackend) Length() int {

// Close write the temp buffer's content to the groupcache
func (r *RedisBackend) Close() error {
_, err := client.Set(r.Key, r.content.Bytes(), r.expiration.Sub(time.Now())).Result()
_, err := client.Set(r.Ctx, r.Key, r.content.Bytes(), r.expiration.Sub(time.Now())).Result()
return err
}

// Clean performs the purge storage
func (r *RedisBackend) Clean() error {
_, err := client.Del(r.Key).Result()
_, err := client.Del(r.Ctx, r.Key).Result()
return err
}

// GetReader return a reader for the write public response
func (r *RedisBackend) GetReader() (io.ReadCloser, error) {
content, err := client.Get(r.Key).Result()
content, err := client.Get(r.Ctx, r.Key).Result()
if err != nil {
return nil, err
}

rc := ioutil.NopCloser(strings.NewReader(content))
rc := io.NopCloser(strings.NewReader(content))
return rc, nil
}
4 changes: 2 additions & 2 deletions backends/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package backends
import (
"context"
"fmt"
"io/ioutil"
"io"
"log"
"testing"
"time"
Expand Down Expand Up @@ -63,7 +63,7 @@ func (suite *RedisBackendTestSuite) TestWriteCacheInRedis() {

reader, err := backend.GetReader()
suite.Nil(err)
result, err := ioutil.ReadAll(reader)
result, err := io.ReadAll(reader)
suite.Nil(err)
suite.Equal(content, result)
}
Expand Down
2 changes: 1 addition & 1 deletion build/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.14.2 as builder
FROM golang:1.20 as builder
WORKDIR /app
COPY . /app
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" -o caddy cmd/main.go
Expand Down
5 changes: 2 additions & 3 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"fmt"
"hash/crc32"
"io"
"io/ioutil"
"math"
"net/http"
"net/url"
Expand Down Expand Up @@ -450,7 +449,7 @@ func getKey(cacheKeyTemplate string, r *http.Request) string {

// bodyHash calculates a hash value of the request body
func bodyHash(r *http.Request) string {
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
return ""
}
Expand All @@ -460,7 +459,7 @@ func bodyHash(r *http.Request) string {
bs := h.Sum(nil)
result := fmt.Sprintf("%x", bs)

r.Body = ioutil.NopCloser(bytes.NewBuffer(body))
r.Body = io.NopCloser(bytes.NewBuffer(body))

return result
}
Expand Down
28 changes: 14 additions & 14 deletions caddyfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,21 +124,21 @@ func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error)

// UnmarshalCaddyfile sets up the handler from Caddyfile
//
// :4000 {
// reverse_proxy yourserver:5000
// http_cache {
// match_path /assets
// match_header Content-Type image/jpg image/png
// status_header X-Cache-Status
// default_max_age 15m
// path /tmp/caddy-cache
// :4000 {
// reverse_proxy yourserver:5000
// http_cache {
// match_path /assets
// match_header Content-Type image/jpg image/png
// status_header X-Cache-Status
// default_max_age 15m
// path /tmp/caddy-cache
//
// distributed consul {
// service_name
// addr
// }
// }
// }
// distributed consul {
// service_name
// addr
// }
// }
// }
func (h *Handler) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
config := getDefaultConfig()

Expand Down
9 changes: 4 additions & 5 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@
// There is no need to modify the Caddy source code to customize your
// builds. You can easily build a custom Caddy with these simple steps:
//
// 1. Copy this file (main.go) into a new folder
// 2. Edit the imports below to include the modules you want plugged in
// 3. Run `go mod init caddy`
// 4. Run `go install` or `go build` - you now have a custom binary!
//
// 1. Copy this file (main.go) into a new folder
// 2. Edit the imports below to include the modules you want plugged in
// 3. Run `go mod init caddy`
// 4. Run `go install` or `go build` - you now have a custom binary!
package main

import (
Expand Down
10 changes: 5 additions & 5 deletions endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ type cachePurge struct{}
// PurgePayload holds the field which will be unmarshalled from the request's body
// NOTE: the format of URI can contains the query param.
// ex. when the client send a delete request with the body
// {
// "method": "GET",
// "hots": "example.com",
// "uri": "/static?ext=txt",
// }
//
// {
// "method": "GET",
// "hots": "example.com",
// "uri": "/static?ext=txt",
// }
type PurgePayload struct {
Method string `json:"method"`
Host string `json:"host"`
Expand Down
15 changes: 9 additions & 6 deletions endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package httpcache
import (
"bytes"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"strings"
Expand Down Expand Up @@ -47,7 +47,10 @@ func (suite *CacheEndpointTestSuite) assertKeyIn(key string, keys []string, msgA
func (suite *CacheEndpointTestSuite) SetupSuite() {
suite.caddyTester = caddytest.NewTester(suite.T())
suite.url = "http://localhost:9898/hello"
suite.admin_url = "http://localhost:2019"
caddytest.Default.AdminPort = 2019
// HACK: I don't know why caddytest will re-spwan a server which admin's port is 2019 different from the origin port in the caddytest
// This way, the test can run smoothly.
suite.admin_url = fmt.Sprintf("http://localhost:%d", caddytest.Default.AdminPort)
suite.caddyTester.InitServer(`
{
order http_cache before reverse_proxy
Expand Down Expand Up @@ -76,17 +79,17 @@ func (suite *CacheEndpointTestSuite) TestListCacheKeys() {
r, err := http.NewRequest("GET", suite.url, nil)
suite.Assert().NoError(err)

res, err := suite.caddyTester.Client.Do(r)
_, err = suite.caddyTester.Client.Do(r)
suite.Assert().NoError(err)
// create the cache first

r, err = http.NewRequest("GET", suite.admin_url+"/caches", nil)
suite.Assert().NoError(err)

res, err = suite.caddyTester.Client.Do(r)
res, err := suite.caddyTester.Client.Do(r)
suite.Assert().NoError(err)

result, err := ioutil.ReadAll(res.Body)
result, err := io.ReadAll(res.Body)
res.Body.Close()
suite.Assert().NoError(err)
suite.True(strings.Contains(string(result), "GET localhost/hello?"))
Expand Down Expand Up @@ -115,7 +118,7 @@ func (suite *CacheEndpointTestSuite) TestShowCache() {
res, err := suite.caddyTester.Client.Do(r)
suite.Assert().NoError(err)

result, err := ioutil.ReadAll(res.Body)
result, err := io.ReadAll(res.Body)
res.Body.Close()
suite.Assert().NoError(err)
suite.True(strings.Contains(string(result), "hope anything will be good"), fmt.Sprintf("result: %s", string(result)))
Expand Down
5 changes: 2 additions & 3 deletions example/distributed_cache/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ services:
- "8500:8500"

cdp: &cdp-source
image: golang:1.14.2
image: golang:1.20
container_name: cdp
volumes:
- cdp:/go/
Expand Down Expand Up @@ -56,8 +56,7 @@ services:
cd ${PROJECT_PATH} &&
go run $PROJECT_PATH/cmd/main.go run --adapter caddyfile --config example/distributed_cache/sourceCaddyfile"
volumes:
cdp:
go-build-cache:

13 changes: 7 additions & 6 deletions extends/distributed/caddyfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ func getDefaultConfig() *Config {
}

// UnmarshalCaddyfile deserializes Caddyfile tokens into consulservice's config
// distributed consul {
// service_name
// addr
// token
// health_url
// }
//
// distributed consul {
// service_name
// addr
// token
// health_url
// }
func (c *ConsulService) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
config := getDefaultConfig()

Expand Down
Loading

0 comments on commit a8055c8

Please sign in to comment.