Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding multiple server stanzas to an http block; bug? #13

Open
sidler-mozilla opened this issue Dec 26, 2019 · 1 comment
Open

adding multiple server stanzas to an http block; bug? #13

sidler-mozilla opened this issue Dec 26, 2019 · 1 comment

Comments

@sidler-mozilla
Copy link

In [1]: from nginx.config.api import Config, Section, Location, KeyOption, KeyValueOption, KeyMultiValueOption                                                                                        

In [2]: http = Section('http', include='../conf/mime.types')                                       

In [3]: server1 = Section('server', KeyValueOption('listen', '80'), KeyValueOption('server_name', 'server1.com'), KeyMultiValueOption('return', [301, 'https://$server_name$request_uri']))           

In [4]: server2 = Section('server', KeyValueOption('listen', '443'), KeyValueOption('server_name', 'server2.com'), KeyMultiValueOption('return', [302, 'https://$server_name$request_uri']))          

In [5]: http.sections.add(server1, server2)      

In [6]: http                                     
Out[6]: 

http {
    include ../conf/mime.types;
    server {
        listen 443;
        server_name server2.com;
        return 302 https://$server_name$request_uri;
    }
}

I then make a very small change to the code and run it again:

 sidler@76  ~  cd repos/scottidler/nginx-config-builder/src 
 sidler@76  ~/repos/scottidler/nginx-config-builder/src   master ●  git diff
diff --git i/src/nginx/config/api/options.py w/src/nginx/config/api/options.py
index 18f8945..0b6db52 100644
--- i/src/nginx/config/api/options.py
+++ w/src/nginx/config/api/options.py
@@ -116,7 +116,6 @@ class AttrDict(dict):
         self._owner = owner
         return ret
 
-
 class AttrList(AttrDict):
     """ A dictionary/list hybrid that exposes values as attributes. """
     def __iter__(self):
@@ -125,7 +124,7 @@ class AttrList(AttrDict):
     def append(self, item):
         if hasattr(item, '_parent'):
             item._parent = self._owner
-        if hasattr(item, 'name'):
+        if hasattr(item, 'name') and item.name != 'server':
             self[item.name] = item
         else:
             self[hash(item)] = item
 sidler@76  ~/repos/scottidler/nginx-config-builder/src   master ●  sudo -E ipython3
Python 3.7.5 (default, Nov 20 2019, 09:21:52) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.10.2 -- An enhanced Interactive Python. Type '?' for help.

In [1]: from nginx.config.api import Config, Section, Location, KeyOption, KeyValueOption, KeyMultiValueOption                                                                                        

In [2]: http = Section('http', include='../conf/mime.types')                                       

In [3]: server1 = Section('server', KeyValueOption('listen', '80'), KeyValueOption('server_name', 'server1.com'), KeyMultiValueOption('return', [301, 'https://$server_name$request_uri']))           

In [4]: server2 = Section('server', KeyValueOption('listen', '443'), KeyValueOption('server_name', 'server2.com'), KeyMultiValueOption('return', [302, 'https://$server_name$request_uri']))          

In [5]: http.sections.add(server1, server2)      

In [6]: http                                     
Out[6]: 

http {
    include ../conf/mime.types;
    server {
        listen 80;
        server_name server1.com;
        return 301 https://$server_name$request_uri;
    }
    server {
        listen 443;
        server_name server2.com;
        return 302 https://$server_name$request_uri;
    }
}

It seems what is happening here is that it tests to see if the object as a 'name' field and if so, it uses this for the hash of the dictionary. In the case of the server blocks, the 'name' field is 'server' and therefore it only allows for one server block. Is this intentional? Is this a bug? Or is there another way that this is supposed to be used?

@anish-gupta-gemini
Copy link

Can you guide how you used this? Did you create your own library?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants