github.com/awesome-flow/flow@v0.0.3-0.20190918184116-508d75d68a2c/web/app/agent/index.go (about)

     1  package agent
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"fmt"
     7  	"html/template"
     8  	"net/http"
     9  
    10  	core "github.com/awesome-flow/flow/pkg/corev1alpha1"
    11  )
    12  
    13  var (
    14  	tmpl *template.Template
    15  )
    16  
    17  func init() {
    18  	var err error
    19  	tmpl, err = template.ParseFiles(
    20  		"web/template/layout.tmpl",
    21  		"web/template/page/config.tmpl",
    22  		"web/template/page/index.tmpl",
    23  		"web/template/page/graphviz.tmpl",
    24  		"web/template/page/pprof.tmpl",
    25  	)
    26  	if err != nil {
    27  		panic(err.Error())
    28  	}
    29  }
    30  
    31  type Page struct {
    32  	Data interface{}
    33  	Body template.HTML
    34  }
    35  
    36  type ResponseFormat uint8
    37  
    38  const (
    39  	RespHtml ResponseFormat = iota
    40  	RespJson
    41  )
    42  
    43  const (
    44  	HdrContentType  = "Content-Type"
    45  	ContentTypeHtml = "text/html"
    46  	ContentTypeJson = "application/json"
    47  )
    48  
    49  func respondWith(rw http.ResponseWriter, format ResponseFormat, tmplName string, data interface{}) error {
    50  	var err error
    51  	switch format {
    52  	case RespHtml:
    53  		err = respondWithHtml(rw, tmplName, data)
    54  	case RespJson:
    55  		err = respondWithJson(rw, data)
    56  	default:
    57  		err = fmt.Errorf("Unknown response format: %d", format)
    58  	}
    59  	if err != nil {
    60  		rw.WriteHeader(http.StatusInternalServerError)
    61  		rw.Write([]byte(fmt.Sprintf("Internal Server Error: %s", err)))
    62  		return err
    63  	}
    64  	return nil
    65  }
    66  
    67  func respondWithHtml(rw http.ResponseWriter, tmplName string, data interface{}) error {
    68  	rw.Header().Add(HdrContentType, ContentTypeHtml)
    69  	bw := bytes.NewBuffer(nil)
    70  	if err := tmpl.ExecuteTemplate(bw, tmplName, data); err != nil {
    71  		return err
    72  	}
    73  	if err := tmpl.ExecuteTemplate(rw, "layout", &Page{Data: data, Body: template.HTML(bw.Bytes())}); err != nil {
    74  		return err
    75  	}
    76  	return nil
    77  }
    78  
    79  func respondWithJson(rw http.ResponseWriter, data interface{}) error {
    80  	js, err := json.Marshal(data)
    81  	if err != nil {
    82  		return err
    83  	}
    84  	rw.Header().Add(HdrContentType, ContentTypeJson)
    85  	if _, err := rw.Write(js); err != nil {
    86  		return err
    87  	}
    88  	return nil
    89  }
    90  
    91  type IndexPage struct {
    92  	Title string
    93  }
    94  
    95  func init() {
    96  	RegisterWebAgent(
    97  		func(*core.Context) (WebAgent, error) {
    98  			return NewDummyWebAgent(
    99  				"/",
   100  				func(rw http.ResponseWriter, req *http.Request) {
   101  					respondWith(rw, RespHtml, "index", &IndexPage{Title: "Flow admin interface"})
   102  				},
   103  			), nil
   104  		},
   105  	)
   106  }