From a6eb568550244bd6264dc861c3298b4cfa265e07 Mon Sep 17 00:00:00 2001 From: Alexander Tikhonov Date: Sun, 4 Aug 2024 18:47:53 +0700 Subject: [PATCH] Add parsing of new files --- config/config.go | 18 +++++++++++++++--- config/config_test.go | 27 +++++++++++++++++++++++++++ test/example3.com.conf | 26 ++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 test/example3.com.conf diff --git a/config/config.go b/config/config.go index 130835c..d8177ec 100644 --- a/config/config.go +++ b/config/config.go @@ -153,6 +153,10 @@ func (c *Config) AddConfigFile(filePath string) (*ConfigFile, error) { return nil, fmt.Errorf("file %s already exists", filePath) } +func (c *Config) ParseFile(filePath string) error { + return c.parseRecursively(filePath) +} + func (c *Config) parse() error { c.parsedFiles = make(map[string]*rawparser.Config) @@ -231,10 +235,18 @@ func (c *Config) parseRecursively(configFilePath string) error { } func (c *Config) parseFilesByPath(filePath string, override bool) ([]*rawparser.Config, error) { - files, err := filepath.Glob(filePath) + var files []string - if err != nil { - return nil, err + stat, err := os.Stat(filePath) + + if err == nil && stat.Mode().IsRegular() { + files = []string{filePath} + } else { + files, err = filepath.Glob(filePath) + + if err != nil { + return nil, err + } } var trees []*rawparser.Config diff --git a/config/config_test.go b/config/config_test.go index 2cc422a..9b6c3e7 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -2,11 +2,13 @@ package config import ( "os" + "path/filepath" "testing" "github.com/stretchr/testify/assert" ) +var sitesEnabledPath = "../test/nginx/sites-enabled" var nginxConfigFilePath = "../test/nginx/nginx.conf" var example2ConfigFilePath = "../test/nginx/sites-enabled/example2.com.conf" var exampleConfigFilePath = "../test/nginx/sites-enabled/example.com.conf" @@ -99,6 +101,31 @@ func TestAddConfigFile(t *testing.T) { assert.Len(t, httpBlocks, 1) } +func TestParseFile(t *testing.T) { + config := parseConfig(t) + serverBlocks := config.FindServerBlocksByServerName(".example3.com") + assert.Empty(t, serverBlocks) + + path := "../test/example3.com.conf" + + b, err := os.ReadFile(path) + assert.Nil(t, err) + + example3ConfigFilePath := filepath.Join(sitesEnabledPath, "example3.com.conf") + err = os.WriteFile(example3ConfigFilePath, b, 0644) + assert.Nil(t, err) + defer os.Remove(example3ConfigFilePath) + + err = config.ParseFile("sites-enabled/example3.com.conf") + assert.Nil(t, err) + + serverBlocks = config.FindServerBlocksByServerName(".example3.com") + assert.Len(t, serverBlocks, 2) + + serverBlocks = config.FindServerBlocksByServerName("example2.com") + assert.Len(t, serverBlocks, 1) +} + func parseConfig(t *testing.T) *Config { config, err := GetConfig("../test/nginx", "", false) assert.Nilf(t, err, "could not create config: %v", err) diff --git a/test/example3.com.conf b/test/example3.com.conf new file mode 100644 index 0000000..1be4bee --- /dev/null +++ b/test/example3.com.conf @@ -0,0 +1,26 @@ +server { + listen 443 ssl http2; + listen [::]:443 ssl http2; + server_name .example3.com; + + # SSL + # Some comment + ssl_certificate /opt/webmng/test/certificate/example.com.crt; # inline comment + ssl_certificate_key /opt/webmng/test/certificate/example.com.key; + ssl_trusted_certificate /opt/webmng/test/certificate/example.com.issuer.crt; + return 301 https://www.example3.com$request_uri; +} + +# HTTP redirect +server { + listen 80; + listen [::]:80; + server_name .example3.com; + include nginxconfig.io/letsencrypt.conf; + + # first comment + # second comment + location / { # inline comment + return 301 https://www.example3.com$request_uri; + } +}