ponzu-cms / ponzu

Headless CMS with automatic JSON API. Featuring auto-HTTPS from Let's Encrypt, HTTP/2 Server Push, and flexible server framework written in Go.
https://docs.ponzu-cms.org
BSD 3-Clause "New" or "Revised" License
5.68k stars 386 forks source link

How do I create the index/home page? #235

Closed tomcam closed 6 years ago

nilslice commented 6 years ago

Hi @tomcam - Ponzu is only responsible for managing data/content in its CMS, and responding to API requests for that data in JSON form.

You could create an index.html file and host it anywhere, loading your data from Ponzu via ajax/xhr or have another server make http requests before it responds.

Are you familiar with any Go web frameworks? Or any web frameworks in other languages? If not, I would suggest exploring how to define routes and start a web server in Go. You can add any number of routes using the standard http mux in Go, or use a more feature-rich router mounted to the standard router used by Ponzu.

The simplest way to get started would be to add this to one of your generated content files in the content directory of you Ponzu project:

func init() {
        http.HandleFunc("/your-route", func(res http.ResponseWriter, req *http.Request) {
                res.Write([]byte("hello")) // <- this line is just responding to your request at /your-route
        })
}

This way, you are adding a route to the same default http mux (router) exposed by the Go standard library. This is the same router Ponzu uses internally to manage http requests.

Using the same approach, you can look at the html/template package to write templates which you would load your data into.

Lastly, I would consider using the official Ponzu Go client (https://github.com/ponzu-cms/go-client) - there are examples in the README to get started.

tomcam commented 6 years ago

Perfect. Thank you.

tomcam commented 6 years ago

I'm super impressed at how I totally missed your Go client, which is just fantastic. Not too many people can be as dimwitted as I am. Thanks again for pointing me at it.

nilslice commented 6 years ago

@tomcam - thank you for the kind words, I'm glad you like it.

Haha it happens! I should update the docs to point to the Go client.. it had been in "Work in progress" state for a while, but it's solid enough now to put front and center.

If you have any issues while use the client, please let me know on the repo, or join us on the #Ponzu slack channel here: https://gophers.slack.com/messages/C3TBV356D

Thanks 👍