github.com/qri-io/qri@v0.10.1-0.20220104210721-c771715036cb/api/templates.go (about)

     1  package api
     2  
     3  import (
     4  	"html/template"
     5  	"net/http"
     6  )
     7  
     8  // templates is a collection of views for rendering with the renderTemplate function
     9  // see homeHandler for an example
    10  var templates *template.Template
    11  
    12  // DefaultWebappPort is the default port the web app will listen on
    13  const DefaultWebappPort = 2505
    14  
    15  func init() {
    16  	templates = template.Must(template.New("webapp").Parse(webapptmpl))
    17  }
    18  
    19  // templateRenderer returns a func "renderTemplate" that renders a template, using the values of a Config
    20  func renderTemplate(w http.ResponseWriter, tmpl string) {
    21  	err := templates.ExecuteTemplate(w, tmpl, map[string]interface{}{
    22  		"port": DefaultWebappPort,
    23  	})
    24  	if err != nil {
    25  		http.Error(w, err.Error(), http.StatusInternalServerError)
    26  	}
    27  }
    28  
    29  const webapptmpl = `
    30  <!DOCTYPE html>
    31  <html>
    32  <head>
    33    <title>Qri</title>
    34  </head>
    35  <body>
    36    <div id="root"></div>
    37    <script type="text/javascript" src="/webapp/main.js"></script>
    38  </body>
    39  </html>`