github.com/cayleygraph/cayley@v0.7.7/internal/http/ui.go (about)

     1  package http
     2  
     3  import (
     4  	"html/template"
     5  	"net/http"
     6  
     7  	"github.com/gobuffalo/packr/v2"
     8  	"github.com/julienschmidt/httprouter"
     9  )
    10  
    11  var templatesBox = packr.New("Templates", "../../templates")
    12  var t *template.Template
    13  
    14  func setupUI() {
    15  	templateDirectory := templatesBox.List()
    16  	for _, file := range templateDirectory {
    17  		data, _ := templatesBox.FindString(file)
    18  		if t == nil {
    19  			t = template.New(file)
    20  		} else {
    21  			t = t.New(file)
    22  		}
    23  		_, err := t.Parse(data)
    24  		if err != nil {
    25  			panic(err)
    26  		}
    27  	}
    28  }
    29  
    30  func serveUI(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
    31  	uiType := params.ByName("ui_type")
    32  	if r.URL.Path == "/" {
    33  		uiType = "query"
    34  	}
    35  	err := t.ExecuteTemplate(w, uiType+".html", nil)
    36  	if err != nil {
    37  		http.Error(w, err.Error(), http.StatusInternalServerError)
    38  	}
    39  }