How did you install discourse? If you do a standard installation, you’ll get a certificate from let’s encrypt by default. It won’t work with cloudflare in front, however.
You might be able to look at the let’s encrypt template and modify it to do what you suggest.