Simple file list from browse (perhaps json?)

1. The problem I’m having:

I have the need to get just the list of files from the file_server’s browse directive, to be fed into a shell script, to present a dialog for the user to pick the file which would get processed elsewhere, all inside a terminal session, ie. I can’t parse a HTML file

2. Error messages and/or full log output:

N/A

3. Caddy version:

2.7

4. How I installed and ran Caddy:

Custom compile for acme_dns

a. System environment:

Linux, Devuan/Debian/Ubuntu
typically with supervisor

b. Command:

supervisorctl start caddy

c. Service/unit/compose file:

d. My complete Caddy config:

{
	log {
		format json
		output file /var/log/caddy/apter-global.log {
		}
		level debug
	}
}

localhost {
   respond "Hello, world!"
}

localhost:2016 {
   respond "Goodbye, world!"
}


aptcacher.theb.org.in {
        handle_path /echo {
     respond "Goodbye, world!"
        }
	# Set this path to your site's directory.
	log {
		output file /var/log/caddy/apter-access.log {
		}
		level debug
	}
        tls {
                dns acmedns /etc/acmedns/clientstorage.json
                propagation_timeout -1
                propagation_delay 15s
        }

	# Enable the static file server.
	handle_path /files/* {
		file_server {
			browse
			root /files
		}
	}
	handle_path /hv/* {
		file_server {
			browse /files/template.file
			root /files
		}
	}
	# Another common task is to set up a reverse proxy:
	# reverse_proxy localhost:8080

	# Or serve a PHP site through php-fpm:
	# php_fastcgi localhost:9000
}

5. Links to relevant resources:

I’ve looked, and got lost in {{html .Name}} and Modules - Caddy Documentation
as I was directed there from file_server (Caddyfile directive) — Caddy Documentation

What I miss, is a “loop” directive, or rather, which variable(s) to pump to a loop directive.

Is there a “How To”/Tutorial I can follow to assist me?

I just realized it isn’t documented, but if you include the browse sub-directive in your config and set the header Accept: application/json in the request, Caddy will respond with a JSON array of the directory listing.

With this simple Caddyfile:

http://localhost {
	file_server browse
}

This root dir:

~ $ tree throwaway                                                                                                                               docker20.10.23,
throwaway
├── Caddyfile
├── dir-1
├── file-1.txt
└── file-2.txt

2 directories, 3 files

Returns this result:

~ $ curl -H 'Accept: application/json' http://localhost/ | jq .                                                                                  docker20.10.23,
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   424  100   424    0     0   254k      0 --:--:-- --:--:-- --:--:--  414k
[
  {
    "name": "dir-1/",
    "size": 64,
    "url": "./dir-1/",
    "mod_time": "2023-08-15T12:39:28.751575632Z",
    "mode": 2147484141,
    "is_dir": true,
    "is_symlink": false
  },
  {
    "name": "file-1.txt",
    "size": 0,
    "url": "./file-1.txt",
    "mod_time": "2023-08-15T12:39:19.809986695Z",
    "mode": 420,
    "is_dir": false,
    "is_symlink": false
  },
  {
    "name": "file-2.txt",
    "size": 0,
    "url": "./file-2.txt",
    "mod_time": "2023-08-15T12:39:24.901089695Z",
    "mode": 420,
    "is_dir": false,
    "is_symlink": false
  }
]
1 Like

THAT part :face_with_peeking_eye:

Yeah, trying to correct that right now :sweat_smile:

1 Like

thought you’ve solved my problem, I still want to know if there are some simple examples/tutorialsto use the templates, as I see some other uses it’ll solve for me, but I need to know how to do the listings and loop/output over the entries… future needs :slight_smile:

For that you’ll need to learn about Go templates. The documentation of the template handler covers the fields and functions available within templates as well as linking to other helpful resources. I don’t know of any tutorials personally, but you can find tons if you search on Google to evaluate per your own criteria.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.