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

Fix whitespace in nginx templates #597

Merged
merged 2 commits into from
Apr 28, 2023

Conversation

sjberman
Copy link
Contributor

The nginx templates were hard to read due to excessive whitespacing in the templates. It's not perfect, but these changes should clean things up quite a bit.

  • I have read the CONTRIBUTING doc
  • I have added tests that prove my fix is effective or that my feature works
  • I have checked that all unit tests pass after adding my changes
  • I have updated necessary documentation
  • I have rebased my branch onto main
  • I will ensure my PR is targeting the main branch and pulling from my branch from my own fork

The nginx templates were hard to read due to excessive whitespacing in the templates. It's not perfect, but these changes should clean things up quite a bit.
@sjberman sjberman requested a review from a team as a code owner April 27, 2023 17:06
@pleshakov
Copy link
Contributor

The generated config is much easier to read now:

upstream default_coffee_80 {
    random two least_conn;

    server 10.244.0.9:8080;
}

upstream default_tea_80 {
    random two least_conn;

    server 10.244.0.10:8080;
}

upstream invalid-backend-ref {
    random two least_conn;

    server unix:/var/lib/nginx/nginx-500-server.sock;
}





server {
        listen 80 default_server;

        default_type text/html;
        return 404;
}

server {

        server_name cafe.example.com;


        location /coffee {
                proxy_set_header Host $host;
                proxy_pass http://default_coffee_80$request_uri;
        }

        location /tea {
                proxy_set_header Host $host;
                proxy_pass http://default_tea_80$request_uri;
        }

        location / {
                return 404 "";

        }

}

server {
    listen unix:/var/lib/nginx/nginx-502-server.sock;
    access_log off;

    return 502;
}

server {
    listen unix:/var/lib/nginx/nginx-500-server.sock;
    access_log off;

    return 500;

My only concern is figuring out how to use those whitespace reduction techniques going forward (confession - I haven't mastered them :) ) Is there a set of rules/ guidelines we can have for our template writing? So that any developer can follow them, and any reviewer of future template changes can also follow them, and they both can arrive at similar result?

I think another approach is just eliminate all empty lines. For example, during template generatation.

@sjberman
Copy link
Contributor Author

@pleshakov It's definitely a bit finicky to get all the spacing correct, especially when trying to balance readability in the code using empty lines with readability in the rendered template. There may not be a perfect solution.

Regarding the templating, this is how it works.

@kate-osborn kate-osborn added the chore Pull requests for routine tasks label Apr 28, 2023
Copy link
Contributor

@pleshakov pleshakov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@sjberman sjberman enabled auto-merge (squash) April 28, 2023 19:42
@github-actions github-actions bot removed the chore Pull requests for routine tasks label Apr 28, 2023
@sjberman sjberman merged commit f73e994 into nginxinc:main Apr 28, 2023
@lucacome lucacome added the chore Pull requests for routine tasks label Apr 28, 2023
@sjberman sjberman deleted the trim-template-whitespace branch July 18, 2023 16:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
chore Pull requests for routine tasks
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

4 participants