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

Add Caddyfile lexer #363

Merged
merged 4 commits into from
May 16, 2020
Merged

Conversation

francislavoie
Copy link
Contributor

Closes #338

So, I definitely have no idea what I'm doing 😄

I spent most of the day hacking on this, seems like it's in decent shape, but who knows. There's probably a much better way to approach it, but this is what I came up with after trying to resolve a ton of edge cases.

I was testing it doing a spam of:

$ go build && ./chroma -l caddyfile Caddyfile --html -s tango > test.html

and

$ go build && ./chroma --trace -l caddyfile Caddyfile

I implemented two lexers here because we have two main usecases - displaying a full Caddyfile config, and showing just partials (matchers and directives) in documentation and such.

I'm not certain which token types to pick, I just kinda with what sorta made sense and made it look pretty in the output 🤷‍♂️

I'd very much appreciate any tips/feedback on this. 🎉

Here's a couple somewhat representative config examples and screenshots. Caddyfiles are very rarely this long or complicated, but these show most of the tricky elements.

{
	debug
}

example.com, fake.org {
	root * /srv

	route {
		# Add trailing slash for directory requests
		@canonicalPath {
			file {
				try_files {path}/index.php
			}
			not path */
		}
		redir @canonicalPath {path}/ 308

		# If the requested file does not exist, try index files
		@indexFiles {
			file {
				try_files {path} {path}/index.php index.php
				split_path .php
			}
		}
		rewrite @indexFiles {http.matchers.file.relative}

		# Proxy PHP files to the FastCGI responder
		@phpFiles {
			path *.php
		}
		reverse_proxy @phpFiles unix//var/run/php7.4-fpm.sock {
			transport fastcgi {
				split .php
			}
		}
	}

	@encode_exts {
		path / *.html *.js *.css *.svg
	}

	header {
		X-Content-Type-Options nosniff
		X-XSS-Protection "1; mode=block"
		X-Robots-Tag none
		Content-Security-Policy "frame-ancestors 'self'"
		X-Frame-Options DENY
		Referrer-Policy same-origin
	}
	
	@singleLine not path /matcher
	respond @singleLine "Awesome."

	file_server
}

Another:

# Caddy v2 file for Kirby 2 & Kirby 3 local sites
# v0.1.0
# 2020/05/10
#=====================
# Global Block
{
  email youremail@domain.tld
  local_certs
}

# Snippets
# Common server snippet use 'import common'
(common) {
  encode gzip
  php_fastcgi localhost:9000
  tls internal
  file_server
  header * {
    # You may want some other header options...
    X-Frame-Options "DENY"
    X-XSS-Protection "1; mode=block"
    X-Content-Type-Options "nosniff"
    X-Frame-Options "SAMEORIGIN"
  }
}

#---------------------
# Kirby 2 & 3 common snippet use 'import kirby'
(kirby) {  
  @blocked {
    path *.txt *.md *.mdown /site/* /kirby/*
  }
  redir @blocked /
}

#---------------------
# Kirby 2 snippet use 'import kirby2'
(kirby2) {
  # /panel files
  handle /panel* {
    try_files {path} {path}/ /panel/index.php?{uri}&{query}
  }
  
  @notPanel {
    not path /panel*
  }
  handle @notPanel {
    try_files {path}/ /index.php?&{query}
  }
}

#---------------------
# Cachebuster rewrite style.123213.css -> style.css 'import cachebuster'
(cachebuster) {
  @cachedFiles {
      not file
      path_regexp cached ~*(.+)\.(?:\d+)\.(js|css|jpg|svg|png)
  }
  rewrite @cachedFiles {http.regexp.cached.1}.{http.regexp.cached.2}
}

#=====================
# Virtual Hosts - domains need to be added to your hosts file

kirby2site.test:443 {
  root * /PATH_TO_SITE/kirby2site
  import common
  import kirby
  import kirby2
}

kirby3site.test:443 {
  root * /PATH_TO_SITE/kirby3site
  import common
  import kirby
}

kirby2cachebustersite.test:443 {
  root * /PATH_TO_SITE/kirby2cachebustersite
  import common
  import cachebuster
  import kirby
  import kirby2
}

Screenshots:
image

Another:
image

@mholt
Copy link

mholt commented May 16, 2020

This is beautiful. 😍

@alecthomas
Copy link
Owner

Very nice! Please add a test file per the instructions in lexers/README.md.

Overall looks pretty good, I'll come back with some suggestions shortly.

alecthomas added a commit that referenced this pull request May 16, 2020
)

// CaddyfileCommon are the rules common to both of the lexer variants
var CaddyfileCommon = Rules{
Copy link
Owner

Choose a reason for hiding this comment

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

No need to make this or the other intermediate values public.

@alecthomas alecthomas merged commit 6896804 into alecthomas:master May 16, 2020
@alecthomas
Copy link
Owner

LGTM, merged. Any followups just send a new PR.

@alecthomas
Copy link
Owner

Thanks!

@francislavoie
Copy link
Contributor Author

Thank you @alecthomas!!

I already made a PR to update the Caddy website with this caddyserver/website#41

I definitely will have a followup PR soon, I noticed a few edgecases that aren't well covered.

mrsdizzie pushed a commit to mrsdizzie/chroma that referenced this pull request Jul 15, 2020
mrsdizzie pushed a commit to mrsdizzie/chroma that referenced this pull request Jul 15, 2020
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

Successfully merging this pull request may close these issues.

Caddyfile syntax highlighting
3 participants