Hugo - My IP With Caddy
There are websites that show your internet IP, ever want to create your own?
Hugo is a static site generator, there is no server side processing once deployed. However Caddy v2 templates feature is giving this a twist.
Following is a simple way to create a fully themed Hugo “show my ip” page with Caddy v2 servers.
Create Page
Create a new Hugo page:
|
|
Inside myip.md
with following content:
|
|
{{.RemoteIP}}
is a Caddy template function1.
Create a menu entry in config.toml
|
|
Generate and deploy your site.
Caddyfile
In Caddyfile
, add following line inside your site config:
|
|
The templates
line tell caddy server to treat /home/myip/
as a template2.
Take this site Caddyfile
as example:
|
|
Restart caddy server.
Plain Text
You can also create a plain text version to be used by curl
command as I shown in How to Find My Public IP.
With File
In Hugo site root, create static/myip/intex.html
:
|
|
Again, add templates line in Caddyfile
:
|
|
Generate and deploy your site, restart caddy server.
Test from command line:
|
|
Caddyfile Only
This post on Caddy forum shown it can be done with just Caddyfile.
|
|
The template
text is embedded directly in respond
, no extra file required.
I turned it into a snippet here.
Simultaneous IPv4 IPv6
Hugo - My IP Page With Javascript will explore how to show IPv4 and IPv6 at the same time.