Add access control configuration
This commit is contained in:
@@ -7,3 +7,7 @@ cache:
|
|||||||
java_status_duration: 1m
|
java_status_duration: 1m
|
||||||
bedrock_status_duration: 1m
|
bedrock_status_duration: 1m
|
||||||
icon_duration: 24h
|
icon_duration: 24h
|
||||||
|
access_control:
|
||||||
|
enable: true
|
||||||
|
allowed_origins:
|
||||||
|
- '*'
|
||||||
@@ -22,6 +22,7 @@ var (
|
|||||||
BedrockStatusDuration: time.Minute,
|
BedrockStatusDuration: time.Minute,
|
||||||
IconDuration: time.Minute * 15,
|
IconDuration: time.Minute * 15,
|
||||||
},
|
},
|
||||||
|
AccessControl: ConfigAccessControl{},
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -32,6 +33,7 @@ type Config struct {
|
|||||||
Port uint16 `yaml:"port"`
|
Port uint16 `yaml:"port"`
|
||||||
Redis *string `yaml:"redis"`
|
Redis *string `yaml:"redis"`
|
||||||
Cache ConfigCache `yaml:"cache"`
|
Cache ConfigCache `yaml:"cache"`
|
||||||
|
AccessControl ConfigAccessControl `yaml:"access_control"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// ConfigCache represents the caching durations of various responses.
|
// ConfigCache represents the caching durations of various responses.
|
||||||
@@ -42,6 +44,12 @@ type ConfigCache struct {
|
|||||||
IconDuration time.Duration `yaml:"icon_duration"`
|
IconDuration time.Duration `yaml:"icon_duration"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ConfigAccessControl is the configuration for the CORS headers
|
||||||
|
type ConfigAccessControl struct {
|
||||||
|
Enable bool `yaml:"enable"`
|
||||||
|
AllowedOrigins []string `yaml:"allowed_origins"`
|
||||||
|
}
|
||||||
|
|
||||||
// ReadFile reads the configuration from the given file and overrides values using environment variables.
|
// ReadFile reads the configuration from the given file and overrides values using environment variables.
|
||||||
func (c *Config) ReadFile(file string) error {
|
func (c *Config) ReadFile(file string) error {
|
||||||
data, err := os.ReadFile(file)
|
data, err := os.ReadFile(file)
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"main/src/assets"
|
"main/src/assets"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
"github.com/gofiber/fiber/v2/middleware/cors"
|
"github.com/gofiber/fiber/v2/middleware/cors"
|
||||||
@@ -25,11 +26,13 @@ func init() {
|
|||||||
Data: assets.Favicon,
|
Data: assets.Favicon,
|
||||||
}))
|
}))
|
||||||
|
|
||||||
|
if config.AccessControl.Enable {
|
||||||
app.Use(cors.New(cors.Config{
|
app.Use(cors.New(cors.Config{
|
||||||
AllowOrigins: "*",
|
AllowOrigins: strings.Join(config.AccessControl.AllowedOrigins, ","),
|
||||||
AllowMethods: "HEAD,OPTIONS,GET,POST",
|
AllowMethods: "HEAD,OPTIONS,GET,POST",
|
||||||
ExposeHeaders: "X-Cache-Hit,X-Cache-Time-Remaining",
|
ExposeHeaders: "X-Cache-Hit,X-Cache-Time-Remaining",
|
||||||
}))
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
if config.Environment == "development" {
|
if config.Environment == "development" {
|
||||||
app.Use(logger.New(logger.Config{
|
app.Use(logger.New(logger.Config{
|
||||||
|
|||||||
Reference in New Issue
Block a user