Rest API to configure Caddy

Is there a plugin (and if no, how could I write one) that provides a Rest API to configure caddy?

We are serving several web app clouds with caddy and when a customer registers a new domain, I’d like to just let the app request this to caddy and caddy sets up the new host.

1 Like

Great idea! It would be a nice one!

It’s on our TODO list. A standard plugin probably won’t cut it, so it’s in our plans before “feature complete”.

1 Like

Still not ready, but, I have something much of what was mentioned in this post.

I put an online server with a demo of an REST API that does more or less what @kellertobias asked.

Before showing the use of the API I would like to say what is the Daspanel project:

  • Developed from scratch using Docker. You can run a dev environment on your local computer and it will work the same way on a production server.
  • A replacement for Cpanel, Plesk and others.
  • Fully manageable via REST API.
  • Like Caddy, every site on HTTPS
  • Using Caddy as your HTTPS server

Now let’s demonstrate the use of the API (these are working examples, you can run these tests in your computer):

Create new site

curl -X POST --header ‘Content-Type: application/json’ --header ‘Accept: application/json’ --header ‘Authorization: cj68e9ec90000325l9e3c2uy1’ -d ‘{“runtime”: “php71”, “runtime”: “php71”, “sitedescription”: “Dastest.me site”, “sitetype”: “grav” }’ ‘https://api.dastest.me/1.0/sites/

Take note of these 2 json response fields:

_cuid (the first one) I refer to it after as _CUID_RESPONSE
active_version I refer to it after as ACTIVE_VERSION_RESPONSE

OBS:

  1. Runtime options: php56, php70, php71 and static
  2. Sitetype options for PHP runtime’s: grav, wordpress, generic, cakephp2x and nextcloud12x
  3. Sitetype options for STATIC runtime: generic

Now the site is online without any content on https://_CUID_RESPONSE.sites.dastest.me

Install site content (optional) - in this example I’m installing Grav

curl -X POST --header ‘Content-Type: application/json’ --header ‘Accept: application/json’ --header ‘Authorization: cj68e9ec90000325l9e3c2uy1’ -d ‘{ “auth_type”: “NONE”, “directory”: “/”, “password”: “mypassword”, “url”: “https://github.com/getgrav/grav/releases/download/1.3.1/grav-admin-v1.3.1.zip”, “user”: “username”, “version”: “ACTIVE_VERSION_RESPONSE” }’ ‘https://api.dastest.me/1.0/sites/content/_CUID_RESPONSE/remotezip

OBS:

  1. auth_type, directory, password and user fields are ignored by now. In the future you can use it to install content from protected sources
  2. url is where Daspanel can download the content to be put on the site

Now the site is online with some content on https://_CUID_RESPONSE.sites.dastest.me

Map (redirect) www.dastest.me to the new created site

curl -X POST --header ‘Content-Type: application/json’ --header ‘Accept: application/json’ --header ‘Authorization: cj68e9ec90000325l9e3c2uy1’ -d ‘{ “domain”: “dastest.me”, “hosturl”: “www”, “ssl”: “auto”, “version”: “ACTIVE_VERSION_RESPONSE” }’ ‘https://api.dastest.me/1.0/sites/_CUID_RESPONSE/redirects

Now the site is online on https://dastest.me

OBS:

  1. When hosturl == www the root domain also is mapped to the site
  2. You cann’t create an redirect if hosturl + domain it’s used by another site. If you got an error on this step Get list off all sites to see wich redirects are in use

Delete the site and all domains redirects to it

curl -X DELETE --header ‘Accept: application/json’ --header ‘Authorization: cj68e9ec90000325l9e3c2uy1’ ‘https://api.dastest.me/1.0/sites/_CUID_RESPONSE

Get list of all sites

curl -X GET --header ‘Accept: application/json’ --header ‘Authorization: cj68e9ec90000325l9e3c2uy1’ ‘https://api.dastest.me/1.0/sites/


Instead of using curl command line you can test the API online (and see his docs) going to this address:

https://api.dastest.me/1.0/sites/ui/#/
https://api.dastest.me/1.0/sites/ui/#/SiteRedirects
https://api.dastest.me/1.0/sites/ui/#/SiteVersions

Before any test click on the authorize button in top lef and inform api_key = cj68e9ec90000325l9e3c2uy1


About versions:

  1. A site can have many versions like: production, test, etc. With this feature you can have an production site and clone it to test with an new version of PHP without any risk to disturb the working site.
  2. Every site have an active version and per default it is displayed in the URL https://_CUID_RESPONSE.sites.dastest.me
  3. The API docs for working with site versions is in this URL https://api.dastest.me/1.0/sites/ui/#/SiteVersions

Using your domain in this test:

  1. Add in your domain dns server 2 entries:

After this DNS changes are published in the internet you can make API test’s using mydomain.com instead of dastest.me


Daspanel can be tested on a local computer by clicking the Install link on this page: https://daspanel.com

When testing Daspanel in your local computer change every reference of dastest.me to daspanel.site. The domain daspanel.site always resolve to 127.0.0.1

Although a lot is missing/wrong, there is a draft documentation on this link: https://docs.daspanel.com


This test server, dastest.me, will be reset every day.

1 Like