[PR #12] [MERGED] feat: associate comments with config objects #41

Closed
opened 2026-02-28 01:20:39 +03:00 by kerem · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/tufanbarisyildirim/gonginx/pull/12
Author: @0xJacky
Created: 1/3/2023
Status: Merged
Merged: 1/4/2023
Merged by: @tufanbarisyildirim

Base: masterHead: master


📝 Commits (3)

  • 1deefb8 feat: associate comments with config objects
  • aead840 chore: update readme
  • 4c3a636 fix: QuotedString as directive name is empty

📊 Changes

13 files changed (+179 additions, -68 deletions)

View changed files

📝 .gitignore (+3 -0)
📝 README.md (+1 -1)
📝 directive.go (+14 -4)
📝 directive_test.go (+10 -0)
📝 dumper.go (+11 -6)
📝 examples/formatting/main.go (+22 -5)
📝 http.go (+17 -7)
📝 include.go (+8 -4)
📝 parser/parser.go (+30 -15)
📝 server.go (+17 -8)
📝 statement.go (+6 -4)
📝 upstream.go (+20 -8)
📝 upstream_server.go (+20 -6)

📄 Description

  1. Single-line comment
# comment: access_log
access_log logs/big.server.access.log main;
  1. Inline comment
server 127.0.0.3:8000 weight=5; # inline comment: server 127.0.0.3:8000 weight=5
server 127.0.0.3:8001 weight=5; # inline comment: server 127.0.0.3:8000 weight=5
  1. Multiline comment
# comment: big_server_com
# comment: upstream big_server_com
upstream big_server_com {
server 127.0.0.3:8000 weight=5;
server 127.0.0.3:8001 weight=5;
server 192.168.0.1:8000;
server 192.168.0.1:8001;
}
  1. associate comments with config objects to print them on config generation and make it configurable with dumper.Style
package main

import (
	"fmt"

	"github.com/tufanbarisyildirim/gonginx"
	"github.com/tufanbarisyildirim/gonginx/parser"
)

func main() {
	p := parser.NewStringParser(`user www www;
worker_processes 5;
error_log logs/error.log;
pid logs/nginx.pid;
worker_rlimit_nofile 8192;
events { worker_connections 4096; } 
# http comment
http {
# include comment
include mime.types;
include proxy.conf;
include fastcgi.conf;
index index.html index.htm index.php;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local]  $status '  
'"$request" $body_bytes_sent "$http_referer" '
' "$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
tcp_nopush on;
server_names_hash_bucket_size 128;
server {
listen 80;
server_name domain1.com www.domain1.com;
access_log logs/domain1.access.log main;
root html;
# comment: location
location ~ \.php$ {
fastcgi_pass 127.0.0.1:1025; } } 
# comment: server
server {
listen 80;
server_name domain2.com www.domain2.com;
access_log logs/domain2.access.log main;
location ~ ^/(images|javascript|js|css|flash|media|static)/ {
root /var/www/virtual/big.server.com/htdocs;
expires 30d;
} location / { proxy_pass http://127.0.0.1:8080; } }
# comment: big_server_com
# comment: upstream big_server_com
upstream big_server_com {
server 127.0.0.3:8000 weight=5; # inline comment: server 127.0.0.3:8000 weight=5
server 127.0.0.3:8001 weight=5; # inline comment: server 127.0.0.3:8000 weight=5
server 192.168.0.1:8000;
server 192.168.0.1:8001;
}
# comment: server
server { # comment: listen
listen 80;
server_name big.server.com;
# comment: access_log
access_log logs/big.server.access.log main;
location / { proxy_pass http://big_server_com; } } }`)

	c := p.Parse()
	fmt.Println(gonginx.DumpConfig(c, gonginx.IndentedStyle))

}

Output:

user www www;
worker_processes 5;
error_log logs/error.log;
pid logs/nginx.pid;
worker_rlimit_nofile 8192;
events {
    worker_connections 4096;
}
# http comment
http {
    # include comment
    include mime.types;
    include proxy.conf;
    include fastcgi.conf;
    index index.html index.htm index.php;
    default_type application/octet-stream;
    log_format main '$remote_addr - $remote_user [$time_local]  $status ' '"$request" $body_bytes_sent "$http_referer" ' ' "$http_user_agent" "$http_x_forwarded_for"';
    access_log logs/access.log main;
    sendfile on;
    tcp_nopush on;
    server_names_hash_bucket_size 128;
    # comment: big_server_com
    # comment: upstream big_server_com
    upstream big_server_com {
        # inline comment: server 127.0.0.3:8000 weight=5
        server 127.0.0.3:8000 weight=5;
        # inline comment: server 127.0.0.3:8000 weight=5
        server 127.0.0.3:8001 weight=5;
        server 192.168.0.1:8000;
        server 192.168.0.1:8001;
    }
    server {
        listen 80;
        server_name domain1.com www.domain1.com;
        access_log logs/domain1.access.log main;
        root html;
        # comment: location
        location ~ \.php$ {
            fastcgi_pass 127.0.0.1:1025;
        }
    }
    # comment: server
    server {
        listen 80;
        server_name domain2.com www.domain2.com;
        access_log logs/domain2.access.log main;
        location ~ ^/(images|javascript|js|css|flash|media|static)/ {
            root /var/www/virtual/big.server.com/htdocs;
            expires 30d;
        }
        location / {
            proxy_pass http://127.0.0.1:8080;
        }
    }
    # comment: server
    server {
        # comment: listen
        listen 80;
        server_name big.server.com;
        # comment: access_log
        access_log logs/big.server.access.log main;
        location / {
            proxy_pass http://big_server_com;
        }
    }
}


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/tufanbarisyildirim/gonginx/pull/12 **Author:** [@0xJacky](https://github.com/0xJacky) **Created:** 1/3/2023 **Status:** ✅ Merged **Merged:** 1/4/2023 **Merged by:** [@tufanbarisyildirim](https://github.com/tufanbarisyildirim) **Base:** `master` ← **Head:** `master` --- ### 📝 Commits (3) - [`1deefb8`](https://github.com/tufanbarisyildirim/gonginx/commit/1deefb85cc88aa5011b99e4e3560aab20b45b52b) feat: associate comments with config objects - [`aead840`](https://github.com/tufanbarisyildirim/gonginx/commit/aead8402675393cd6e4771cd7cf83fa67edfdda7) chore: update readme - [`4c3a636`](https://github.com/tufanbarisyildirim/gonginx/commit/4c3a63627efb0501fb637ac341093dba04a6ce07) fix: QuotedString as directive name is empty ### 📊 Changes **13 files changed** (+179 additions, -68 deletions) <details> <summary>View changed files</summary> 📝 `.gitignore` (+3 -0) 📝 `README.md` (+1 -1) 📝 `directive.go` (+14 -4) 📝 `directive_test.go` (+10 -0) 📝 `dumper.go` (+11 -6) 📝 `examples/formatting/main.go` (+22 -5) 📝 `http.go` (+17 -7) 📝 `include.go` (+8 -4) 📝 `parser/parser.go` (+30 -15) 📝 `server.go` (+17 -8) 📝 `statement.go` (+6 -4) 📝 `upstream.go` (+20 -8) 📝 `upstream_server.go` (+20 -6) </details> ### 📄 Description 1. Single-line comment ``` # comment: access_log access_log logs/big.server.access.log main; ``` 3. Inline comment ``` server 127.0.0.3:8000 weight=5; # inline comment: server 127.0.0.3:8000 weight=5 server 127.0.0.3:8001 weight=5; # inline comment: server 127.0.0.3:8000 weight=5 ``` 4. Multiline comment ``` # comment: big_server_com # comment: upstream big_server_com upstream big_server_com { server 127.0.0.3:8000 weight=5; server 127.0.0.3:8001 weight=5; server 192.168.0.1:8000; server 192.168.0.1:8001; } ``` 5. associate comments with config objects to print them on config generation and make it configurable with `dumper.Style` ``` package main import ( "fmt" "github.com/tufanbarisyildirim/gonginx" "github.com/tufanbarisyildirim/gonginx/parser" ) func main() { p := parser.NewStringParser(`user www www; worker_processes 5; error_log logs/error.log; pid logs/nginx.pid; worker_rlimit_nofile 8192; events { worker_connections 4096; } # http comment http { # include comment include mime.types; include proxy.conf; include fastcgi.conf; index index.html index.htm index.php; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] $status ' '"$request" $body_bytes_sent "$http_referer" ' ' "$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log main; sendfile on; tcp_nopush on; server_names_hash_bucket_size 128; server { listen 80; server_name domain1.com www.domain1.com; access_log logs/domain1.access.log main; root html; # comment: location location ~ \.php$ { fastcgi_pass 127.0.0.1:1025; } } # comment: server server { listen 80; server_name domain2.com www.domain2.com; access_log logs/domain2.access.log main; location ~ ^/(images|javascript|js|css|flash|media|static)/ { root /var/www/virtual/big.server.com/htdocs; expires 30d; } location / { proxy_pass http://127.0.0.1:8080; } } # comment: big_server_com # comment: upstream big_server_com upstream big_server_com { server 127.0.0.3:8000 weight=5; # inline comment: server 127.0.0.3:8000 weight=5 server 127.0.0.3:8001 weight=5; # inline comment: server 127.0.0.3:8000 weight=5 server 192.168.0.1:8000; server 192.168.0.1:8001; } # comment: server server { # comment: listen listen 80; server_name big.server.com; # comment: access_log access_log logs/big.server.access.log main; location / { proxy_pass http://big_server_com; } } }`) c := p.Parse() fmt.Println(gonginx.DumpConfig(c, gonginx.IndentedStyle)) } ``` Output: ``` user www www; worker_processes 5; error_log logs/error.log; pid logs/nginx.pid; worker_rlimit_nofile 8192; events { worker_connections 4096; } # http comment http { # include comment include mime.types; include proxy.conf; include fastcgi.conf; index index.html index.htm index.php; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] $status ' '"$request" $body_bytes_sent "$http_referer" ' ' "$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log main; sendfile on; tcp_nopush on; server_names_hash_bucket_size 128; # comment: big_server_com # comment: upstream big_server_com upstream big_server_com { # inline comment: server 127.0.0.3:8000 weight=5 server 127.0.0.3:8000 weight=5; # inline comment: server 127.0.0.3:8000 weight=5 server 127.0.0.3:8001 weight=5; server 192.168.0.1:8000; server 192.168.0.1:8001; } server { listen 80; server_name domain1.com www.domain1.com; access_log logs/domain1.access.log main; root html; # comment: location location ~ \.php$ { fastcgi_pass 127.0.0.1:1025; } } # comment: server server { listen 80; server_name domain2.com www.domain2.com; access_log logs/domain2.access.log main; location ~ ^/(images|javascript|js|css|flash|media|static)/ { root /var/www/virtual/big.server.com/htdocs; expires 30d; } location / { proxy_pass http://127.0.0.1:8080; } } # comment: server server { # comment: listen listen 80; server_name big.server.com; # comment: access_log access_log logs/big.server.access.log main; location / { proxy_pass http://big_server_com; } } } ``` --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
kerem 2026-02-28 01:20:39 +03:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/gonginx#41
No description provided.