Skip to content

Commit

Permalink
Feat/reverse proxy (#21)
Browse files Browse the repository at this point in the history
* doc: update config example

* chore: update go mod version

* chore: update ci

* doc: update doc

* feat: handle reverse proxy

* chore: update golang lint ci config

* chore: add cSpell words

* chore: update ci

* chore: force exec of ci

* chore: try to fix ginkgo

* chore: try new things for ci

* chore: update go.mod

* chore: revert back changed-file ci
  • Loading branch information
DblK authored Jun 25, 2023
1 parent caba4a7 commit 29ec5ff
Show file tree
Hide file tree
Showing 13 changed files with 214 additions and 412 deletions.
16 changes: 10 additions & 6 deletions .github/workflows/ginkgo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@ on:
- master
pull_request:

permissions:
contents: read

jobs:
mod:
runs-on: ubuntu-latest
name: Check modules
steps:

- uses: actions/setup-go@v3
- uses: actions/setup-go@v4
with:
go-version: 1.17
go-version: '1.20'
cache: false

- name: Checkout
uses: actions/checkout@v3
Expand All @@ -25,7 +29,7 @@ jobs:

- name: Get changed files
id: changed-files
uses: tj-actions/changed-files@v35.9.2
uses: tj-actions/changed-files@v37.0.2
with:
files: |
go.*
Expand All @@ -42,9 +46,9 @@ jobs:
runs-on: ubuntu-latest
steps:

- uses: actions/setup-go@v3
- uses: actions/setup-go@v4
with:
go-version: '1.19'
go-version: '1.20'

- name: Checkout
uses: actions/checkout@v3
Expand All @@ -53,7 +57,7 @@ jobs:

- name: Get changed files
id: changed-files
uses: tj-actions/changed-files@v35.9.2
uses: tj-actions/changed-files@v37.0.2
with:
files: |
go.*
Expand Down
12 changes: 7 additions & 5 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,30 @@ on:

permissions:
contents: read

jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v3
- uses: actions/setup-go@v4
with:
go-version: 1.17
go-version: '1.20'
cache: false
- uses: actions/checkout@v3
with:
fetch-depth: 2

- name: Get changed files
id: changed-files
uses: tj-actions/changed-files@v35.9.2
uses: tj-actions/changed-files@v37.0.2
with:
files: |
go.*
**/*.go
- name: golangci-lint
if: steps.changed-files.outputs.any_changed == 'true'
uses: golangci/golangci-lint-action@v3.4.0
uses: golangci/golangci-lint-action@v3
with:
version: v1.52.2
version: v1.53.3
2 changes: 1 addition & 1 deletion .github/workflows/goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
fetch-depth: 0
- uses: actions/setup-go@v3
with:
go-version: 1.17
go-version: 1.20
- name: Compile the source
uses: goreleaser/goreleaser-action@v2
with:
Expand Down
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ linters:
- gocognit
- gofmt
- goimports
- depguard
# - depguard
- misspell
- lll
- unparam
Expand Down
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
"asciicheck",
"bbolt",
"bodyclose",
"Bogdan",
"dblk",
"deadcode",
"depguard",
"Dono",
"dupl",
"errcheck",
"Errorf",
Expand Down Expand Up @@ -49,6 +51,7 @@
"logutils",
"mapstructure",
"mitchellh",
"mockgen",
"nakedret",
"nestif",
"newfiles",
Expand All @@ -59,7 +62,9 @@
"onsi",
"prealloc",
"renameio",
"Roadmap",
"robustio",
"Rosu",
"scopelint",
"sirupsen",
"Slowloris",
Expand All @@ -73,6 +78,7 @@
"titledb",
"Titlekey",
"tmpl",
"traefik",
"typecheck",
"Uauth",
"unconvert",
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ Dead simple, thanks to Golang!

If you change an interface (or add a new one), do not forget to execute `./update_mocks.sh` to generate up-to-date mocks for tests.

Do not forget to install `mockgen` first:
```sh
go install github.com/golang/mock/mockgen@v1.6.0
```

## What to launch tests?

You can run `ginkgo -r` for one shot or `ginkgo watch -r` during development.
Expand Down
4 changes: 4 additions & 0 deletions config.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ protocol: https
# This is used as title when trying to visit the shop with a non switch device
name: TinShop

# Tells if we are using a reverse proxy if front of tinshop [optional]
# This is used to rewrite correct url to download games when reverse proxy used
reverseProxy: false

# All debug flags will be stored here
debug:
# Display more information when connecting to nfs share
Expand Down
17 changes: 13 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type File struct {
ShopProtocol string `mapstructure:"protocol"`
ShopPort int `mapstructure:"port"`
Debug debug `mapstructure:"debug"`
Proxy bool `mapstructure:"reverseProxy"`
AllSources repository.ConfigSources `mapstructure:"sources"`
Name string `mapstructure:"name"`
Security security `mapstructure:"security"`
Expand Down Expand Up @@ -94,6 +95,7 @@ func (cfg *File) configChange() {
cfg.ShopHost = newConfig.ShopHost
cfg.ShopProtocol = newConfig.ShopProtocol
cfg.ShopPort = newConfig.ShopPort
cfg.Proxy = newConfig.Proxy
cfg.Debug = newConfig.Debug
cfg.AllSources = newConfig.AllSources
cfg.Name = newConfig.Name
Expand Down Expand Up @@ -148,11 +150,15 @@ func ComputeDefaultValues(config repository.Config) repository.Config {
} else {
rootShop += config.Host()
}
if config.Port() == 0 {
rootShop += ":3000"
} else if !(config.Port() == 443 && config.Protocol() == "https") && !(config.Port() == 80 && config.Protocol() == "http") {
rootShop += ":" + strconv.Itoa(config.Port())

if !config.ReverseProxy() {
if config.Port() == 0 {
rootShop += ":3000"
} else if !(config.Port() == 443 && config.Protocol() == "https") && !(config.Port() == 80 && config.Protocol() == "http") {
rootShop += ":" + strconv.Itoa(config.Port())
}
}
log.Println((rootShop))
config.SetRootShop(rootShop)

config.SetShopTemplateData(repository.ShopTemplate{
Expand Down Expand Up @@ -181,6 +187,9 @@ func (cfg *File) SetRootShop(root string) {
func (cfg *File) RootShop() string {
return cfg.rootShop
}
func (cfg *File) ReverseProxy() bool {
return cfg.Proxy
}

// Protocol returns the protocol scheme (http or https)
func (cfg *File) Protocol() string {
Expand Down
56 changes: 56 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ var _ = Describe("Config", func() {
Port().
Return(0).
AnyTimes()
myMockConfig.EXPECT().
ReverseProxy().
Return(false).
AnyTimes()
config.ComputeDefaultValues(myMockConfig)

Expect(testRootShop).To(Equal("http://tinshop.example.com:3000"))
Expand All @@ -85,6 +89,10 @@ var _ = Describe("Config", func() {
Port().
Return(443).
AnyTimes()
myMockConfig.EXPECT().
ReverseProxy().
Return(false).
AnyTimes()
config.ComputeDefaultValues(myMockConfig)

Expect(testRootShop).To(Equal("https://tinshop.example.com"))
Expand All @@ -106,6 +114,10 @@ var _ = Describe("Config", func() {
Port().
Return(80).
AnyTimes()
myMockConfig.EXPECT().
ReverseProxy().
Return(false).
AnyTimes()
config.ComputeDefaultValues(myMockConfig)

Expect(testRootShop).To(Equal("http://tinshop.example.com"))
Expand All @@ -127,10 +139,39 @@ var _ = Describe("Config", func() {
Port().
Return(8080).
AnyTimes()
myMockConfig.EXPECT().
ReverseProxy().
Return(false).
AnyTimes()
config.ComputeDefaultValues(myMockConfig)

Expect(testRootShop).To(Equal("http://tinshop.example.com:8080"))
})
It("Should not set port if reverse proxy", func() {
var testRootShop string
myMockConfig.EXPECT().
Protocol().
Return("http").
AnyTimes()
myMockConfig.EXPECT().
SetRootShop(gomock.Any()).
Return().
Do(func(rootShop string) {
testRootShop = rootShop
}).
AnyTimes()
myMockConfig.EXPECT().
Port().
Return(0).
AnyTimes()
myMockConfig.EXPECT().
ReverseProxy().
Return(true).
AnyTimes()
config.ComputeDefaultValues(myMockConfig)

Expect(testRootShop).To(Equal("http://tinshop.example.com"))
})
})
})
Context("Security for Blacklist/Whitelist tests", func() {
Expand Down Expand Up @@ -279,6 +320,21 @@ var _ = Describe("Config", func() {
Expect(myConfig.Port()).To(Equal(12345))
})
})
Describe("ReverseProxy", func() {
var myConfig config.File

BeforeEach(func() {
myConfig = config.File{}
})

It("Test with empty object", func() {
Expect(myConfig.ReverseProxy()).To(BeFalse())
})
It("Test with a value", func() {
myConfig.Proxy = true
Expect(myConfig.ReverseProxy()).To(BeTrue())
})
})
Describe("ShopTitle", func() {
var myConfig config.File

Expand Down
43 changes: 23 additions & 20 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,37 +1,40 @@
module github.com/DblK/tinshop

go 1.17
go 1.20

require (
github.com/dustin/go-humanize v1.0.0
github.com/fsnotify/fsnotify v1.5.1
github.com/dustin/go-humanize v1.0.1
github.com/fsnotify/fsnotify v1.6.0
github.com/golang/mock v1.6.0
github.com/gorilla/mux v1.8.0
github.com/onsi/ginkgo/v2 v2.0.0
github.com/onsi/gomega v1.17.0
github.com/spf13/viper v1.10.1
github.com/onsi/ginkgo/v2 v2.11.0
github.com/onsi/gomega v1.27.8
github.com/spf13/viper v1.16.0
github.com/vmware/go-nfs-client v0.0.0-20190605212624-d43b92724c1b
go.etcd.io/bbolt v1.3.6
go.etcd.io/bbolt v1.3.7
gopkg.in/fsnotify.v1 v1.4.7
)

require (
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/kr/pretty v0.2.0 // indirect
github.com/magiconair/properties v1.8.5 // indirect
github.com/mitchellh/mapstructure v1.4.3 // indirect
github.com/onsi/ginkgo v1.16.5 // indirect
github.com/pelletier/go-toml v1.9.4 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/rasky/go-xdr v0.0.0-20170124162913-1a41d1a06c93 // indirect
github.com/spf13/afero v1.6.0 // indirect
github.com/spf13/cast v1.4.1 // indirect
github.com/spf13/afero v1.9.5 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sys v0.9.0 // indirect
golang.org/x/text v0.9.0 // indirect
golang.org/x/tools v0.9.3 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/ini.v1 v1.66.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 29ec5ff

Please sign in to comment.