Compare commits

..

10 Commits

Author SHA1 Message Date
4f10933caa Merge branch 'develop'
All checks were successful
continuous-integration/drone/push Build is passing
2021-11-08 00:57:56 +01:00
b0e492c163 Fixes after code linters suggestions
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone Build is passing
2021-08-03 00:13:14 +02:00
e070fc402a Added go vet, staticheck and lint to .drone.yml
Some checks failed
continuous-integration/drone/push Build is failing
2021-08-02 23:44:25 +02:00
268cab5f79 Merge pull request 'develop-issue-4' (#8) from develop-issue-4 into develop
All checks were successful
continuous-integration/drone/push Build is passing
Reviewed-on: https://git.pbiernat.dev/golang/vegvisir/pulls/8
2021-07-25 20:10:28 +01:00
8c0f8d4151 [fix] Added support for headers cache
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing
2021-07-25 20:54:42 +02:00
1c2c0bb467 [fix] Added GC to memory cache
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing
2021-07-25 01:44:50 +02:00
58eaaef0b3 [refactor] Simplified selecting cache datasource and fail-safe mechanism
All checks were successful
continuous-integration/drone/push Build is passing
2021-07-25 01:03:58 +02:00
80a0cba90f Merge pull request 'Rebuild of cache module' (#3) from develop-issue-2 into develop
All checks were successful
continuous-integration/drone Build is passing
continuous-integration/drone/push Build is passing
Reviewed-on: https://git.pbiernat.dev/golang/vegvisir/pulls/3
2021-07-24 17:23:07 +01:00
a0f162b91c Rebuild of cache module:
All checks were successful
continuous-integration/drone/tag Build is passing
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing
- Added new "fail-safe" memory-datasource
 - Moved some logic from Server to Router
2021-07-24 17:46:22 +02:00
a37e605291 Updated server version const
All checks were successful
continuous-integration/drone/push Build is passing
2021-07-23 18:42:18 +02:00
20 changed files with 593 additions and 371 deletions

View File

@ -3,7 +3,32 @@ type: docker
name: default name: default
steps: steps:
- name: test - name: static_check
image: golang image: golang:1.16
commands: commands:
- ls -lah - go get honnef.co/go/tools/cmd/staticcheck
- staticcheck ./pkg/...
volumes:
- name: gopath
path: /go
- name: lint
image: golang:1.16
commands:
- go get golang.org/x/lint/golint
- golint -set_exit_status ./pkg/...
volumes:
- name: gopath
path: /go
- name: vet
image: golang:1.16
commands:
- go vet ./pkg/...
volumes:
- name: gopath
path: /go
volumes:
- name: gopath
temp: {}

6
go.mod
View File

@ -1,13 +1,15 @@
module vegvisir module vegvisir
go 1.16 go 1.13
require ( require (
github.com/andybalholm/brotli v1.0.3 // indirect github.com/andybalholm/brotli v1.0.3 // indirect
github.com/go-redis/redis v6.15.9+incompatible github.com/go-redis/redis v6.15.9+incompatible
github.com/google/go-cmp v0.5.6 // indirect
github.com/klauspost/compress v1.13.1 // indirect
github.com/onsi/ginkgo v1.15.0 // indirect github.com/onsi/ginkgo v1.15.0 // indirect
github.com/onsi/gomega v1.10.5 // indirect github.com/onsi/gomega v1.10.5 // indirect
github.com/valyala/fasthttp v1.31.0 github.com/valyala/fasthttp v1.28.0
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect
) )

24
go.sum
View File

@ -2,6 +2,7 @@ github.com/andybalholm/brotli v1.0.2/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu
github.com/andybalholm/brotli v1.0.3 h1:fpcw+r1N1h0Poc1F/pHbW40cUm/lMEQslZtCkBQ0UnM= github.com/andybalholm/brotli v1.0.3 h1:fpcw+r1N1h0Poc1F/pHbW40cUm/lMEQslZtCkBQ0UnM=
github.com/andybalholm/brotli v1.0.3/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= github.com/andybalholm/brotli v1.0.3/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/go-redis/redis v6.15.9+incompatible h1:K0pv1D7EQUjfyoMql+r/jZqCLizCGKFlFgcHWWmHQjg= github.com/go-redis/redis v6.15.9+incompatible h1:K0pv1D7EQUjfyoMql+r/jZqCLizCGKFlFgcHWWmHQjg=
github.com/go-redis/redis v6.15.9+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA= github.com/go-redis/redis v6.15.9+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA=
@ -11,25 +12,32 @@ github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:x
github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs=
github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w=
github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0=
github.com/golang/protobuf v1.4.2 h1:+Z5KGCizgyZCbGh1KZqA0fcLLkwbsjIzS4aV2v7wJX0=
github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/klauspost/compress v1.13.4 h1:0zhec2I8zGnjWcKyLl6i3gPqKANCCn5e9xmviEEeX6s= github.com/klauspost/compress v1.12.2/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg=
github.com/klauspost/compress v1.13.4/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg= github.com/klauspost/compress v1.13.1 h1:wXr2uRxZTJXHLly6qhJabee5JqIhTRoLBhDOA74hDEQ=
github.com/klauspost/compress v1.13.1/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg=
github.com/nxadm/tail v1.4.4 h1:DQuhQpB1tVlglWS2hLQ5OV6B5r8aGxSrPc5Qo6uTN78=
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk= github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk=
github.com/onsi/ginkgo v1.15.0 h1:1V1NfVQR87RtWAgp1lv9JZJ5Jap+XFGKPi00andXGi4=
github.com/onsi/ginkgo v1.15.0/go.mod h1:hF8qUzuuC8DJGygJH3726JnCZX4MYbRB8yFfISqnKUg= github.com/onsi/ginkgo v1.15.0/go.mod h1:hF8qUzuuC8DJGygJH3726JnCZX4MYbRB8yFfISqnKUg=
github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY= github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY=
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
github.com/onsi/gomega v1.10.5 h1:7n6FEkpFmfCoo2t+YYqXH0evK+a9ICQz0xcAy9dYcaQ=
github.com/onsi/gomega v1.10.5/go.mod h1:gza4q3jKQJijlu05nKWRCW/GavJumGt8aNRxWg7mt48= github.com/onsi/gomega v1.10.5/go.mod h1:gza4q3jKQJijlu05nKWRCW/GavJumGt8aNRxWg7mt48=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.31.0 h1:lrauRLII19afgCs2fnWRJ4M5IkV0lo2FqA61uGkNBfE= github.com/valyala/fasthttp v1.28.0 h1:ruVmTmZaBR5i67NqnjvvH5gEv0zwHfWtbjoyW98iho4=
github.com/valyala/fasthttp v1.31.0/go.mod h1:2rsYD01CKFrjjsvFxx75KlEUNpWNBY9JWD3K/7o2Cus= github.com/valyala/fasthttp v1.28.0/go.mod h1:cmWIqlu99AO/RKcp1HWaViTqc57FswJOfYYdPJBl8BA=
github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc= github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
@ -44,6 +52,7 @@ golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20201202161906-c7110b5ffcbb/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/net v0.0.0-20201202161906-c7110b5ffcbb/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210510120150-4163338589ed h1:p9UgmWI9wKpfYmgaV/IZKGdXc5qEK45tDwwwDyjS26I=
golang.org/x/net v0.0.0-20210510120150-4163338589ed/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20210510120150-4163338589ed/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
@ -60,10 +69,12 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210514084401-e8d321eab015/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210514084401-e8d321eab015/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
@ -71,16 +82,21 @@ golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4f
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE=
google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
google.golang.org/protobuf v1.23.0 h1:4MY060fB1DLGMB/7MBTLnwQUY6+F09GEiz6SsrNqyzM=
google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=

48
pkg/cache/cache.go vendored Normal file
View File

@ -0,0 +1,48 @@
// ___ ____ ___ ___
// \ \ / / | _ | __| \ \ / / || | __ || || _ |
// \ \/ / |___ | |__ \ \/ / || |___ || ||___|
// \ / | _ | _ | \ / || __ | || ||\\
// \/ |___ |___ | \/ || ____| || || \\
//
// Copyright (c) 2021 Piotr Biernat. https://pbiernat.dev. MIT License
// Repo: https://git.pbiernat.dev/golang/vegvisir
package cache
import (
"log"
"vegvisir/pkg/config"
)
const (
typeRedis = "redis"
typeMemory = "memory"
)
// Datastore interface
type Datastore interface {
SetKey(string, interface{}, int) error
GetKey(string) (interface{}, error)
IsConnected() bool
}
// GetCacheDatastore function
func GetCacheDatastore(config config.Cache) *Datastore {
var datastore Datastore
if config.Type == typeRedis {
datastore = NewRedisDatastore(config.Host, config.Port)
} else {
datastore = NewMemoryDatastore()
}
// fail-safe switch to memory datasource
if !datastore.IsConnected() {
log.Println("Cache server is not responding, switching to memory cache.")
datastore = NewMemoryDatastore()
}
return &datastore
}

View File

@ -1,16 +0,0 @@
// ___ ____ ___ ___
// \ \ / / | _ | __| \ \ / / || | __ || || _ |
// \ \/ / |___ | |__ \ \/ / || |___ || ||___|
// \ / | _ | _ | \ / || __ | || ||\\
// \/ |___ |___ | \/ || ____| || || \\
//
// Copyright (c) 2021 Piotr Biernat. https://pbiernat.dev. MIT License
// Repo: https://git.pbiernat.dev/golang/vegvisir
package cache
type CacheDatastore interface {
SetKey(string, interface{}, int) error
GetKey(string) (interface{}, error)
}

87
pkg/cache/manager.go vendored
View File

@ -1,87 +0,0 @@
// ___ ____ ___ ___
// \ \ / / | _ | __| \ \ / / || | __ || || _ |
// \ \/ / |___ | |__ \ \/ / || |___ || ||___|
// \ / | _ | _ | \ / || __ | || ||\\
// \/ |___ |___ | \/ || ____| || || \\
//
// Copyright (c) 2021 Piotr Biernat. https://pbiernat.dev. MIT License
// Repo: https://git.pbiernat.dev/golang/vegvisir
package cache
import (
"encoding/json"
"log"
)
type Manager struct {
datastore CacheDatastore
prefix string
ttl int
}
func NewManager(datastore CacheDatastore, prefix string, ttl int) Manager {
return Manager{
datastore: datastore,
prefix: prefix,
ttl: ttl,
}
}
func (rm *Manager) Save(name string, r interface{}) bool { // FIXME second argument type( interface{} )
data, err := json.Marshal(r)
if err != nil {
log.Println("JSON:", err) // FIXME
return false
}
name = rm.prefix + name
err = rm.datastore.SetKey(name, string(data), rm.ttl)
if err != nil {
log.Println("Error saving cache item:", name) // FIXME
return false
}
return true
}
func (rm *Manager) Load(name string, output interface{}) (bool, interface{}) { // FIXME second return type( interface{} )
name = rm.prefix + name
data, err := rm.datastore.GetKey(name)
if err != nil {
log.Println("Cache item:", name, "not found")
return false, nil
}
var res interface{}
switch t := output.(type) {
default:
log.Printf("Unsupported cache type: %T", t)
return false, nil
case *ResponseCache:
res = &ResponseCache{}
rm.convertType(data, res)
return true, res.(*ResponseCache)
case *RouteCache:
res = &RouteCache{}
rm.convertType(data, res)
return true, res.(*RouteCache)
}
}
func (rm *Manager) convertType(data interface{}, output interface{}) interface{} {
err := json.Unmarshal([]byte(data.(string)), &output)
if err != nil {
log.Println("Converting error: ", err) // FIXME
return nil
}
return output
}

84
pkg/cache/memo.go vendored
View File

@ -1,84 +0,0 @@
// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
// See page 278.
// Package memo provides a concurrency-safe non-blocking memoization
// of a function. Requests for different keys proceed in parallel.
// Concurrent requests for the same url block until the first completes.
// This implementation uses a monitor goroutine.
package cache
// Func is the type of the function to memoize.
type Func func(url, method string, route *RouteCache) (error, *ResponseCache)
// A result is the result of calling a Func.
type result struct {
value *ResponseCache
err error
}
type entry struct {
res result
ready chan struct{} // closed when res is ready
}
// A request is a message requesting that the Func be applied to url.
type request struct {
url string
method string
route *RouteCache
response chan<- result // the client wants a single result
}
type Memo struct {
requests chan request
}
// New returns a memoization of f. Clients must subsequently call Close.
func New(f Func) *Memo {
memo := &Memo{requests: make(chan request)}
go memo.server(f)
return memo
}
func (memo *Memo) Read(url, method string, route *RouteCache) (*ResponseCache, error) {
response := make(chan result)
memo.requests <- request{url, method, route, response}
res := <-response
return res.value, res.err
}
func (memo *Memo) Close() { close(memo.requests) }
func (memo *Memo) server(f Func) {
cache := make(map[string]*entry)
for req := range memo.requests {
key := req.method + "_" + req.url
e := cache[key]
if e == nil {
// This is the first request for this url.
e = &entry{ready: make(chan struct{})}
cache[key] = e
//log.Println("e.call:", req.url, req.method, req.route)
go e.call(f, req.url, req.method, req.route)
}
//log.Println("e.deliver:", req.response)
go e.deliver(req.response)
}
}
func (e *entry) call(f Func, url, method string, route *RouteCache) {
// Evaluate the function.
e.res.err, e.res.value = f(url, method, route)
// Broadcast the ready condition.
close(e.ready)
}
func (e *entry) deliver(response chan<- result) {
// Wait for the ready condition.
<-e.ready
// Send the result to the client.
response <- e.res
}

68
pkg/cache/memory_datastore.go vendored Normal file
View File

@ -0,0 +1,68 @@
// ___ ____ ___ ___
// \ \ / / | _ | __| \ \ / / || | __ || || _ |
// \ \/ / |___ | |__ \ \/ / || |___ || ||___|
// \ / | _ | _ | \ / || __ | || ||\\
// \/ |___ |___ | \/ || ____| || || \\
//
// Copyright (c) 2021 Piotr Biernat. https://pbiernat.dev. MIT License
// Repo: https://git.pbiernat.dev/golang/vegvisir
package cache
import (
"errors"
"time"
)
// TTLItem struct
type TTLItem struct {
ts int // timestamp
ttl int // ttl in seconds
}
// NewMemoryDatastore function
func NewMemoryDatastore() *MemoryDatastore {
return &MemoryDatastore{
cache: make(map[string]interface{}),
ts: make(map[string]TTLItem),
}
}
// MemoryDatastore struct
type MemoryDatastore struct {
cache map[string]interface{}
ts map[string]TTLItem
}
// SetKey function
func (ds *MemoryDatastore) SetKey(key string, data interface{}, ttl int) error {
ds.cache[key] = data
ds.ts[key] = TTLItem{
ts: time.Now().Second(),
ttl: ttl,
}
return nil
}
// GetKey function
func (ds *MemoryDatastore) GetKey(key string) (interface{}, error) {
ds.gc(key) // remove key is time of creation is outdated
if data, ok := ds.cache[key]; ok {
return data, nil
}
return nil, errors.New("Key not found" + key)
}
// IsConnected function
func (ds *MemoryDatastore) IsConnected() bool {
return true
}
func (ds *MemoryDatastore) gc(key string) {
if item, ok := ds.ts[key]; ok && item.ts < time.Now().Second()-item.ttl {
delete(ds.cache, key)
}
}

View File

@ -16,6 +16,7 @@ import (
"github.com/go-redis/redis" "github.com/go-redis/redis"
) )
// NewRedisDatastore function
func NewRedisDatastore(host string, port int) *RedisDatastore { func NewRedisDatastore(host string, port int) *RedisDatastore {
return &RedisDatastore{ return &RedisDatastore{
client: redis.NewClient(&redis.Options{ client: redis.NewClient(&redis.Options{
@ -23,31 +24,26 @@ func NewRedisDatastore(host string, port int) *RedisDatastore {
Password: "", // FIXME: use env or param Password: "", // FIXME: use env or param
DB: 0, // FIXME: use env or param DB: 0, // FIXME: use env or param
}), }),
cache: make(map[string]interface{}),
} }
} }
// RedisDatastore class
type RedisDatastore struct { type RedisDatastore struct {
client *redis.Client client *redis.Client
cache map[string]interface{}
} }
// SetKey function
func (ds *RedisDatastore) SetKey(key string, data interface{}, ttl int) error { func (ds *RedisDatastore) SetKey(key string, data interface{}, ttl int) error {
err := ds.client.Set(key, data, time.Duration(ttl)*time.Second).Err() err := ds.client.Set(key, data, time.Duration(ttl)*time.Second).Err()
if err != nil { if err != nil {
return err return err
} }
// ds.cache[key] = data
return nil return nil
} }
// GetKey function
func (ds *RedisDatastore) GetKey(key string) (interface{}, error) { func (ds *RedisDatastore) GetKey(key string) (interface{}, error) {
// if data, ok := ds.cache[key]; ok {
// return data, nil
// }
data, err := ds.client.Get(key).Result() data, err := ds.client.Get(key).Result()
if err != nil { if err != nil {
return nil, err return nil, err
@ -55,3 +51,10 @@ func (ds *RedisDatastore) GetKey(key string) (interface{}, error) {
return data, nil return data, nil
} }
// IsConnected function
func (ds *RedisDatastore) IsConnected() bool {
_, err := ds.client.Ping().Result()
return err == nil
}

83
pkg/cache/response.go vendored
View File

@ -9,24 +9,79 @@
package cache package cache
import "github.com/valyala/fasthttp" import (
"encoding/json"
"log"
)
// Headers type
type Headers map[string]string
// ResponseCache struct
type ResponseCache struct { type ResponseCache struct {
URL string URL string
Method string Body string
Body []byte Headers Headers
Code int
Headers *fasthttp.ResponseHeader
} }
func NewResponseCache(url, method string, body []byte, code int, headers *fasthttp.ResponseHeader) *ResponseCache { // ResponseCacheManager class
return &ResponseCache{url, method, body, code, headers} type ResponseCacheManager struct {
datastore Datastore
prefix string
ttl int
} }
//func (r *ResponseCache) StatusCode() int { // NewResponseCacheManager function
// return r.Code func NewResponseCacheManager(datastore Datastore, ttl int) ResponseCacheManager {
//} return ResponseCacheManager{
// datastore: datastore,
//func (r *ResponseCache) Body() []byte { prefix: "response_",
// return r.Body ttl: ttl,
//} }
}
// Save function
func (rm *ResponseCacheManager) Save(name string, r ResponseCache) bool {
if !rm.datastore.IsConnected() {
log.Println("Response-cache:", "Not connected to server")
return false
}
data, err := json.Marshal(r)
if err != nil {
log.Println("Response-cache", "JSON encode:", err)
return false
}
name = rm.prefix + name
err = rm.datastore.SetKey(name, string(data), rm.ttl)
if err != nil {
log.Println("Response-cache:", err, name)
return false
}
return true
}
// Load function
func (rm *ResponseCacheManager) Load(name string) (bool, *ResponseCache) {
if !rm.datastore.IsConnected() {
log.Println("Response-cache:", "Not connected to server")
return false, &ResponseCache{}
}
name = rm.prefix + name
data, err := rm.datastore.GetKey(name)
if err != nil {
return false, &ResponseCache{}
}
rc := &ResponseCache{}
err = json.Unmarshal([]byte(data.(string)), &rc)
if err != nil {
log.Println("Response-cache:", "JSON docode:", err)
return false, &ResponseCache{}
}
return true, rc
}

74
pkg/cache/route.go vendored
View File

@ -9,14 +9,76 @@
package cache package cache
import (
"encoding/json"
"log"
)
// RouteCache struct
type RouteCache struct { type RouteCache struct {
SourceUrl string SourceURL string
TargetUrl string TargetURL string
} }
func NewRouteCache(source, target string) *RouteCache { // RouteCacheManager class
return &RouteCache{ type RouteCacheManager struct {
SourceUrl: source, datastore Datastore
TargetUrl: target, prefix string
ttl int
}
// NewRouteCacheManager function
func NewRouteCacheManager(datastore Datastore, ttl int) RouteCacheManager {
return RouteCacheManager{
datastore: datastore,
prefix: "route_",
ttl: ttl,
} }
} }
// Save function
func (rm *RouteCacheManager) Save(name string, r RouteCache) bool {
if !rm.datastore.IsConnected() {
log.Println("Route-cache:", "Not connected to server")
return false
}
data, err := json.Marshal(r)
if err != nil {
log.Println("Route-cache", "JSON encode:", err)
return false
}
name = rm.prefix + name
err = rm.datastore.SetKey(name, data, rm.ttl)
if err != nil {
log.Println("Route-cache:", "Unable to save", name, err)
return false
}
return true
}
// Load function
func (rm *RouteCacheManager) Load(name string) (bool, *RouteCache) {
if !rm.datastore.IsConnected() {
log.Println("Route-cache:", "Not connected to server")
return false, &RouteCache{}
}
name = rm.prefix + name
data, err := rm.datastore.GetKey(name)
if err != nil {
log.Println("Route-cache:", "Unable to load", name, err)
return false, &RouteCache{}
}
rc := &RouteCache{}
err = json.Unmarshal([]byte(data.(string)), &rc)
if err != nil {
log.Println("Route-cache:", "JSON decode:", err)
return false, &RouteCache{}
}
return true, rc
}

View File

@ -8,3 +8,19 @@
// Repo: https://git.pbiernat.dev/golang/vegvisir // Repo: https://git.pbiernat.dev/golang/vegvisir
package client package client
import "github.com/valyala/fasthttp"
// HTTPClient struct
type HTTPClient struct {
context *fasthttp.RequestCtx
}
// NewHTTPClient function
func NewHTTPClient(ctx *fasthttp.RequestCtx) *HTTPClient {
return &HTTPClient{
context: ctx,
}
}
// func (c *HTTPClient)

View File

@ -1,10 +0,0 @@
// ___ ____ ___ ___
// \ \ / / | _ | __| \ \ / / || | __ || || _ |
// \ \/ / |___ | |__ \ \/ / || |___ || ||___|
// \ / | _ | _ | \ / || __ | || ||\\
// \/ |___ |___ | \/ || ____| || || \\
// Copyright (c) 2021 Piotr Biernat. https://pbiernat.dev. MIT License
// Repo: https://git.pbiernat.dev/golang/vegvisir
package client

View File

@ -15,39 +15,67 @@ import (
"os" "os"
) )
// Config struct
type Config struct { type Config struct {
Server Server Server Server
Cache Cache
Backends map[string]Backend Backends map[string]Backend
confPath string
} }
// Server struct
type Server struct { type Server struct {
Address string Address string
Port int Port int
} }
// Cache struct
type Cache struct {
Type string
Host string
Port int
Username string
Password string
Database string
RouteTTL int
ResponseTTL int
}
// Backend struct
type Backend struct { type Backend struct {
PrefixUrl string PrefixURL string
BackendAddress string BackendAddress string
Protocol string Protocol string
Routes []Route Routes []Route
} }
// Route struct
type Route struct { type Route struct {
Pattern string Pattern string
Target string Target string
} }
// DefaultRoute shorthand
var DefaultRoute = Route{ var DefaultRoute = Route{
Pattern: "(.*)", Pattern: "(.*)",
Target: "", Target: "",
} }
func (c *Config) Load(cPath string) error { // New function
if _, err := os.Stat(cPath); err != nil { func New(confPath string) *Config {
return &Config{
confPath: confPath,
}
}
// Load function
func (c *Config) Load() error {
if _, err := os.Stat(c.confPath); err != nil {
return err return err
} }
data, err := ioutil.ReadFile(cPath) data, err := ioutil.ReadFile(c.confPath)
if err != nil { if err != nil {
return err return err
} }

View File

@ -12,27 +12,20 @@ package main
import ( import (
"flag" "flag"
"log" "log"
"net/http"
"os" "os"
"runtime" "runtime"
"runtime/pprof" "runtime/pprof"
"vegvisir/pkg/server" "vegvisir/pkg/server"
) )
import _ "net/http/pprof"
var ( var (
cFile = flag.String("c", "vegvisir.json", "Path to config file") cPath = flag.String("c", "vegvisir.json", "Path to config file")
// for profiling... // for profiling...
cpuprofile = flag.String("cpuprofile", "", "write cpu profile to `file`") cpuprofile = flag.String("cpuprofile", "", "write cpu profile to `file`")
memprofile = flag.String("memprofile", "", "write memory profile to `file`") memprofile = flag.String("memprofile", "", "write memory profile to `file`")
) )
func main() { func main() {
go func() {
log.Println(http.ListenAndServe("localhost:6060", nil))
}()
flag.Parse() flag.Parse()
// cpu profiling // cpu profiling
if *cpuprofile != "" { if *cpuprofile != "" {
@ -47,7 +40,7 @@ func main() {
defer pprof.StopCPUProfile() defer pprof.StopCPUProfile()
} }
server.NewServer(*cFile).Run() server.NewServer(*cPath).Run()
// memory profiling // memory profiling
if *memprofile != "" { if *memprofile != "" {

87
pkg/server/router.go Normal file
View File

@ -0,0 +1,87 @@
// ___ ____ ___ ___
// \ \ / / | _ | __| \ \ / / || | __ || || _ |
// \ \/ / |___ | |__ \ \/ / || |___ || ||___|
// \ / | _ | _ | \ / || __ | || ||\\
// \/ |___ |___ | \/ || ____| || || \\
//
// Copyright (c) 2021 Piotr Biernat. https://pbiernat.dev. MIT License
// Repo: https://git.pbiernat.dev/golang/vegvisir
package server
import (
"fmt"
"regexp"
"strings"
"vegvisir/pkg/cache"
"vegvisir/pkg/config"
)
// Router struct
type Router struct {
config *config.Config
rcm cache.RouteCacheManager // Redis cache
rCache map[string]cache.RouteCache // Internal route cache - TMP?
}
// Route struct
type Route struct {
SourceURL string
TargetURL string
}
// NewRouter function
func NewRouter(conf *config.Config, cacheDS cache.Datastore, ttl int) *Router {
return &Router{
config: conf,
rcm: cache.NewRouteCacheManager(cacheDS, ttl),
rCache: make(map[string]cache.RouteCache),
}
}
// FindByRequestURL function
func (r *Router) FindByRequestURL(url []byte) (bool, Route) {
var sURL string = string(url)
for bID := range r.config.Backends {
bck := r.config.Backends[bID]
if !strings.Contains(sURL, bck.PrefixURL) {
continue
}
for rID := range bck.Routes {
routeCfg := &bck.Routes[rID]
// if ok, cRoute := s.rCacheManager.Load(sUri); ok {
// return true, cRoute
// }
if cRoute, ok := r.rCache[sURL]; ok {
route := Route{
SourceURL: cRoute.SourceURL,
TargetURL: cRoute.TargetURL,
}
return true, route
}
rgxp := regexp.MustCompile(fmt.Sprintf("%s%s", bck.PrefixURL, routeCfg.Pattern))
if rgxp.Match(url) {
targetURL := bck.BackendAddress + rgxp.ReplaceAllString(sURL, routeCfg.Target)
route := Route{ // FIXME: duplicated #1
SourceURL: sURL,
TargetURL: targetURL,
}
// s.rCacheManager.Save(sUri, cRoute)
r.rCache[sURL] = cache.RouteCache{ // FIXME: duplicated #1
SourceURL: route.SourceURL,
TargetURL: route.TargetURL,
}
return true, route
}
}
}
return false, Route{}
}

View File

@ -15,8 +15,7 @@ import (
"log" "log"
"os" "os"
"os/signal" "os/signal"
"regexp" "syscall"
"strings"
"time" "time"
"vegvisir/pkg/cache" "vegvisir/pkg/cache"
"vegvisir/pkg/config" "vegvisir/pkg/config"
@ -25,37 +24,40 @@ import (
) )
const ( const (
Version = "0.1" // ServerVersion static
Name = "Vegvisir/" + Version ServerVersion = "0.1-dev"
// ServerName static
ServerName = "Vegvisir/" + ServerVersion
) )
// Server struct
type Server struct { type Server struct {
Config config.Config config *config.Config
router *Router
cFilePath string // Path to config file respCM cache.ResponseCacheManager // Redis response cache
rCache map[string]*cache.RouteCache // Internal route cache
routeCM cache.Manager // Redis route cache
respCM cache.Manager // Redis response cache
} }
// NewServer function
func NewServer(cPath string) *Server { func NewServer(cPath string) *Server {
datastore := cache.NewRedisDatastore("127.0.0.7", 6379) // FIXME use config or env... config := config.New(cPath)
if err := config.Load(); err != nil {
log.Fatalln("Unable to find config file: ", cPath, err)
}
datastore := cache.GetCacheDatastore(config.Cache)
return &Server{ return &Server{
cFilePath: cPath, config: config,
rCache: make(map[string]*cache.RouteCache), router: NewRouter(config, *datastore, config.Cache.RouteTTL),
routeCM: cache.NewManager(datastore, "prefix_", 5), //FIXME use ttl(seconds) from config or env... respCM: cache.NewResponseCacheManager(*datastore, config.Cache.ResponseTTL),
respCM: cache.NewManager(datastore, "response_", 5), //FIXME use ttl(seconds) from config or env...
} }
} }
// Run function
func (s *Server) Run() { func (s *Server) Run() {
if err := s.Config.Load(s.cFilePath); err != nil {
log.Fatalln("Unable to find config file: ", s.cFilePath, err)
}
go func() { go func() {
serverAddress := s.Config.Server.Address + ":" + fmt.Sprint(s.Config.Server.Port) serverAddress := s.config.Server.Address + ":" + fmt.Sprint(s.config.Server.Port)
if err := fasthttp.ListenAndServe(serverAddress, s.mainHandler); err != nil { if err := fasthttp.ListenAndServe(serverAddress, s.mainHandler); err != nil {
log.Fatalf("Server panic! Error message: %s", err) log.Fatalf("Server panic! Error message: %s", err)
} }
@ -65,7 +67,7 @@ func (s *Server) Run() {
// Wait for an interrupt // Wait for an interrupt
interrupt := make(chan os.Signal, 1) interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt, os.Kill) signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM)
<-interrupt <-interrupt
log.Println("SIGKILL or SIGINT caught, shutting down...") log.Println("SIGKILL or SIGINT caught, shutting down...")
@ -78,103 +80,74 @@ func (s *Server) Run() {
log.Println("Server shutdown successfully.") log.Println("Server shutdown successfully.")
} }
// Shutdown function
func (s *Server) Shutdown(ctx context.Context) { // TODO: wait for all connections to finish func (s *Server) Shutdown(ctx context.Context) { // TODO: wait for all connections to finish
log.Println("Shuting down finished") log.Println("Shuting down...")
} }
func (s *Server) mainHandler(ctx *fasthttp.RequestCtx) { func (s *Server) mainHandler(ctx *fasthttp.RequestCtx) {
ctx.Response.Header.Add(fasthttp.HeaderServer, Name) // http := client.NewHttpClient(ctx)
// move all below logic to concrete handler or sth.... // move all below logic to concrete handler or sth....
reqUrl, sReqUrl, sReqMethod := ctx.RequestURI(), string(ctx.RequestURI()), string(ctx.Method()) reqURL, sReqURL, sReqMethod := ctx.RequestURI(), string(ctx.RequestURI()), string(ctx.Method())
// log.Println("Incoming request:", sReqMethod, sReqUrl) // log.Println("Incoming request:", sReqMethod, sReqURL)
found, route := s.findRouteByRequestUrl(reqUrl) found, route := s.router.FindByRequestURL(reqURL)
if !found { if !found {
// FIXME: return 404 or 5xx error in response? Maybe define it in concrete Backend config? // FIXME: return 404/5xx error in repsonse, maybe define it in Backend config?
ctx.SetStatusCode(fasthttp.StatusNotFound) ctx.SetStatusCode(fasthttp.StatusNotFound)
return return
} }
memo := cache.New(s.processUrl) go func() {
defer memo.Close() cacheFound, data := s.respCM.Load(sReqURL)
response, err := memo.Read(sReqUrl, sReqMethod, route) if cacheFound { // get response from cache
//err, response := s.processUrl(sReqUrl, sReqMethod, route) // log.Println("Read resp from cache: ", route.TargetUrl)
if err != nil {
// FIXME: Response read error(sending 500 error response)
ctx.SetStatusCode(fasthttp.StatusInternalServerError)
log.Println("Response read error(sending 500 error response)", err)
return
}
ctx.SetStatusCode(response.Code) // copy headers and body from cache
ctx.SetBody(response.Body) ctx.Response.Header.DisableNormalizing()
ctx.Response.Header.SetBytesV(fasthttp.HeaderContentType, response.Headers.ContentType()) for key, value := range data.Headers {
} ctx.Response.Header.Set(key, value)
}
ctx.SetBody([]byte(data.Body))
} else { // send request to backend and save to cache
log.Println("Send req to backend url: ", route.TargetURL)
func (s *Server) findRouteByRequestUrl(url []byte) (bool, *cache.RouteCache) { // prepare to send request to backend - separate
var sUrl = string(url) bckReq := fasthttp.AcquireRequest()
bckResp := fasthttp.AcquireResponse()
defer fasthttp.ReleaseRequest(bckReq)
defer fasthttp.ReleaseResponse(bckResp)
for bId := range s.Config.Backends { // copy headers from backend response and prepare request for backend - separate
bck := s.Config.Backends[bId] bckReq.SetRequestURI(route.TargetURL)
if !strings.Contains(sUrl, bck.PrefixUrl) { bckReq.Header.SetMethod(sReqMethod)
continue
}
for rId := range bck.Routes { err := fasthttp.Do(bckReq, bckResp)
route := &bck.Routes[rId] if err != nil {
ctx.SetStatusCode(fasthttp.StatusInternalServerError)
if cRoute, ok := s.rCache[sUrl]; ok { return
return true, cRoute
} }
rgxp := regexp.MustCompile(fmt.Sprintf("%s%s", bck.PrefixUrl, route.Pattern)) headers := make(cache.Headers)
if rgxp.Match(url) { // rewrite headers from backend to gateway response
targetUrl := bck.BackendAddress + rgxp.ReplaceAllString(sUrl, route.Target) bckResp.Header.Set(fasthttp.HeaderServer, ServerName)
bckResp.Header.Del(fasthttp.HeaderXPoweredBy)
ctx.Response.Header.DisableNormalizing()
bckResp.Header.VisitAll(func(key, value []byte) {
headers[string(key)] = string(value)
ctx.Response.Header.SetBytesKV(key, value)
})
// ctx.SetStatusCode(bckResp.StatusCode())
ctx.SetBody(bckResp.Body())
cRoute := cache.NewRouteCache(sUrl, targetUrl) // save response to cache
respCache := cache.ResponseCache{
// s.rCacheManager.Save(sUrl, cRoute) URL: sReqURL,
s.rCache[sUrl] = cRoute Body: string(bckResp.Body()),
Headers: headers,
return true, cRoute } // FIXME: prepare resp cache struct in respCM.Save method or other service...
} s.respCM.Save(sReqURL, respCache)
} }
} }()
return false, &cache.RouteCache{}
}
func (s *Server) processUrl(url, method string, route *cache.RouteCache) (error, *cache.ResponseCache) {
// handle response caching
cacheKey := method + "_" + url
if ok, data := s.respCM.Load(cacheKey, &cache.ResponseCache{}); ok {
//log.Println("Read resp from cache: ", route.TargetUrl, url)
return nil, data.(*cache.ResponseCache)
} else {
//log.Println("Send req to backend url: ", route.TargetUrl, url)
start := time.Now()
bckReq := fasthttp.AcquireRequest()
bckResp := fasthttp.AcquireResponse()
defer fasthttp.ReleaseRequest(bckReq)
defer fasthttp.ReleaseResponse(bckResp)
// copy headers from backend response and prepare request for backend - separate
bckReq.SetRequestURI(route.TargetUrl)
bckReq.Header.SetMethod(method)
err := fasthttp.Do(bckReq, bckResp)
if err != nil {
return err, nil
}
body, code := bckResp.Body(), bckResp.StatusCode()
log.Printf("%s, %s, %d bytes\n", url, time.Since(start), len(body))
// save response to cache
respCache := cache.NewResponseCache(url, method, body, code, &bckResp.Header)
s.respCM.Save(cacheKey, respCache)
return nil, respCache
}
} }

View File

@ -1,10 +0,0 @@
// ___ ____ ___ ___
// \ \ / / | _ | __| \ \ / / || | __ || || _ |
// \ \/ / |___ | |__ \ \/ / || |___ || ||___|
// \ / | _ | _ | \ / || __ | || ||\\
// \/ |___ |___ | \/ || ____| || || \\
//
// Copyright (c) 2021 Piotr Biernat. https://pbiernat.dev. MIT License
// Repo: https://git.pbiernat.dev/golang/vegvisir
package server

43
vegvisir.json Normal file
View File

@ -0,0 +1,43 @@
{
"server": {
"address": "127.0.0.1",
"port": 8080
},
"backends": {
"news-app": {
"prefixURL": "news/",
"backendAddress": "http://172.17.0.1:3030",
"routes": [{
"pattern": "new/(.*)/([0-9]*)",
"target": "new-url/$1/$2"
},{
"pattern": "new/(.*)",
"target": "$1"
},{
"pattern": "(.+)",
"target": "url"
}]
},
"article-app": {
"prefixURL": "art/",
"backendAddress": "http://172.17.0.1:3030",
"routes": [{
"pattern": "art1/(.*)",
"target": "art-url/$1"
},{
"pattern": "([a-zA-Z0-9]*)",
"target": "art-global/$1"
}]
}
},
"cache": {
"type": "redis",
"host": "localhost",
"port": 6379,
"username": "",
"password": "",
"database": "0",
"routeTTL": 10,
"responseTTL": 10
}
}

View File

@ -5,29 +5,39 @@
}, },
"backends": { "backends": {
"news-app": { "news-app": {
"prefixUrl": "news/", "prefixURL": "news/",
"backendAddress": "http://172.17.0.1:3030/api/news/", "backendAddress": "http://127.0.0.1:3030",
"routes": [{ "routes": [{
"pattern": "archive/([0-9]*)", "pattern": "new/(.*)",
"target": "archive/$1" "target": "$1"
},{ },{
"pattern": "(.*)", "pattern": "new1/(.*)/([0-9]*)",
"target": "new/$1" "target": "new-url1/$1/$2"
},{
"pattern": "(.+)",
"target": "url"
}] }]
}, },
"article-app": { "article-app": {
"prefixUrl": "article/", "prefixURL": "art/",
"backendAddress": "http://172.17.0.1:3030/api/articles/", "backendAddress": "http://127.0.0.1:3030",
"routes": [{ "routes": [{
"pattern": "(.*)", "pattern": "art1/(.*)",
"target": "$1" "target": "art-url1/$1"
},{
"pattern": "([a-zA-Z0-9]*)",
"target": "article-global/$1"
}] }]
} }
}, },
"cache": { "cache": {
"ttl": 300, "type": "redis",
"route_ttl": 300, "host": "localhost",
"response_ttl": 300, "port": 6379,
"headers_ttl": 300 "username": "",
"password": "",
"database": "0",
"routeTTL": 300,
"responseTTL": 300
} }
} }