Skip to content

Commit

Permalink
feat: some generall imporvemnts especially pre-commit and linting
Browse files Browse the repository at this point in the history
  • Loading branch information
OnlyNico43 committed Nov 1, 2024
1 parent 54dd332 commit cc749eb
Show file tree
Hide file tree
Showing 9 changed files with 298 additions and 115 deletions.
13 changes: 13 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for all configuration options:
# https://docs.github.com/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file

version: 2
updates:
- package-ecosystem: "gomod" # See documentation for possible values
directory: "/" # Location of package manifests
schedule:
interval: "weekly"
reviewers:
- "OnlyNico43"
28 changes: 13 additions & 15 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
name: Run Tests

on:
push:
branches:
- main
pull_request:
branches:
- main
on: ["push", "pull_request"]

jobs:
format:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v5
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
- name: format
run: go fmt
test:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v5
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
- name: test
env:
GIN_MODE: release
run: go test
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
- name: golangci-lint
uses: golangci/golangci-lint-action@v6

3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.vscode
.idea
out
23 changes: 23 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
repos:
- repo: local
hooks:
- id: gofmt
name: gofmt
entry: gofmt -w .
language: system
pass_filenames: false

- id: go-mod-tidy
name: go-mod-tidy
entry: go mod tidy
language: system
pass_filenames: false
require_serial: true

- id: golangci-lint
name: golangci-lint
entry: golangci-lint run
language: system
pass_filenames: false
require_serial: true

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func main() {
}
```

Done now your application is equipped with a basic cors configuration
Done, now your application is equipped with a basic cors configuration


## How to configure
Expand Down
29 changes: 17 additions & 12 deletions cors.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ This is a cors middleware for the gin http framwork and can be used to configure
package cors

import (
"fmt"
"net/http"
"slices"
"strconv"
Expand Down Expand Up @@ -57,28 +58,32 @@ type Config struct {
Adds wildcard to the array if no value was set.
Panics if the allowCredentials is true and the header is a wildcard
*/
func checkCredentials(header []string, allowCredentials bool) []string {
if header == nil && !allowCredentials {
header = []string{"*"}
return header
} else if (header == nil || slices.Contains(header, "*")) && allowCredentials {
panic("The allowed origins must be set and cannot contain the \"*\" wildcard when AllowCredentials is true")
} else {
return header
func checkCredentials(header []string, allowCredentials bool, headerName string) []string {
if header == nil || len(header) <= 0 {
if allowCredentials {
panic(fmt.Sprintf("The %s must be set when AllowCredentials is true", headerName))
}
return []string{"*"}
}

if slices.Contains(header, "*") && allowCredentials {
panic(fmt.Sprintf("The %s cannot contain the \"*\" wildcard when AllowCredentials is true", headerName))
}

return header
}

/*
Validates the config and sets empty values to their defaults if necessary
*/
func (c Config) validate() Config {
c.AllowedOrigins = checkCredentials(c.AllowedOrigins, c.AllowCredentials)
c.AllowedOrigins = checkCredentials(c.AllowedOrigins, c.AllowCredentials, "allowed origins")

c.AllowedMethods = checkCredentials(c.AllowedMethods, c.AllowCredentials)
c.AllowedMethods = checkCredentials(c.AllowedMethods, c.AllowCredentials, "allowed methods")

c.AllowedHeaders = checkCredentials(c.AllowedHeaders, c.AllowCredentials)
c.AllowedHeaders = checkCredentials(c.AllowedHeaders, c.AllowCredentials, "allowed headers")

c.ExposeHeaders = checkCredentials(c.ExposeHeaders, c.AllowCredentials)
c.ExposeHeaders = checkCredentials(c.ExposeHeaders, c.AllowCredentials, "expose headers")

if c.MaxAge == 0 {
c.MaxAge = 24 * time.Hour
Expand Down
Loading

0 comments on commit cc749eb

Please sign in to comment.