github.com/braveheart12/just@v0.8.7/metrics/status.go (about)

     1  /*
     2   *    Copyright 2019 Insolar Technologies
     3   *
     4   *    Licensed under the Apache License, Version 2.0 (the "License");
     5   *    you may not use this file except in compliance with the License.
     6   *    You may obtain a copy of the License at
     7   *
     8   *        http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   *    Unless required by applicable law or agreed to in writing, software
    11   *    distributed under the License is distributed on an "AS IS" BASIS,
    12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   *    See the License for the specific language governing permissions and
    14   *    limitations under the License.
    15   */
    16  
    17  package metrics
    18  
    19  import (
    20  	"bytes"
    21  	"fmt"
    22  	"html/template"
    23  	"io"
    24  	"net/http"
    25  	"time"
    26  
    27  	"github.com/insolar/insolar/version"
    28  )
    29  
    30  var statusTmpl = `
    31  <html>
    32  <head>
    33  <title>_status page</title>
    34  <style>
    35  	* {
    36  		box-sizing: border-box;
    37  	}
    38  
    39  	h1 {
    40  		text-transform: uppercase;
    41  	}
    42  
    43  	section {
    44  		width: 100%;
    45  		border: 1px solid blue;
    46  	}
    47  
    48  	section header {
    49  		text-transform:uppercase;
    50  		width: 100%;
    51  		height: 48px;
    52  		padding: 0 12px;
    53  		line-height: 48px;
    54  		font-size: 24px;
    55  		background-color:blue;
    56  		color: whitesmoke;
    57  	}
    58  
    59  	dl {
    60  		display: grid;
    61  		grid-template-columns: 200px 1fr;
    62  		grid-template-rows: 1fr;
    63  	}
    64  
    65  	dl dt {
    66  		grid-column: 1;
    67  		font-weight: bold;
    68  		text-transform: capitalize;
    69  		text-align: right;
    70  	}
    71  
    72  	dl dd {
    73  		grid-column: 2;
    74  	}
    75  </style>
    76  </head>
    77  <body>
    78  
    79  <h1>STATUS</h1>
    80  
    81  <h2>Build info</h2>
    82  <pre>
    83  {{ .VersionInfo }}
    84  </pre>
    85  
    86  <section>
    87  <header>General</header>
    88  <div class="content">
    89  <dl>
    90  <dt>Uptime:</dt> <dd>{{ .Uptime }}</dd>
    91  <dt>metrics:</dt> <dd><a href="/metrics">/metrics</a></dd>
    92  <dt>pprof:</dt> <dd><a href="/debug/pprof">/debug/pprof</a></dd>
    93  <dt>rpcz:</dt> <dd> <a href="/debug/rpcz">/debug/rpcz</a></dd>
    94  <dt>tracez:</dt> <dd><a href="/debug/tracez">/debug/tracez</a></dd>
    95  </dl>
    96  </div>
    97  </section>
    98  
    99  </body>
   100  </html>
   101  `
   102  
   103  var parsedStatusTmpl = template.Must(template.New("proc_status").Parse(string(statusTmpl)))
   104  
   105  type procStatus struct {
   106  	StartTime time.Time
   107  }
   108  
   109  func newProcStatus() *procStatus {
   110  	info := &procStatus{
   111  		StartTime: time.Now(),
   112  	}
   113  	return info
   114  }
   115  
   116  func (ps *procStatus) ServeHTTP(w http.ResponseWriter, r *http.Request) {
   117  	var b bytes.Buffer
   118  	err := parsedStatusTmpl.Execute(&b, struct {
   119  		VersionInfo string
   120  		Uptime      string
   121  	}{
   122  		VersionInfo: version.GetFullVersion(),
   123  		Uptime:      fmt.Sprintf("%v", time.Since(ps.StartTime)),
   124  	})
   125  	if err != nil {
   126  		http.Error(w, fmt.Sprintln("Template error:", err),
   127  			http.StatusInternalServerError)
   128  		return
   129  	}
   130  
   131  	w.Header().Set("Content-Type", "text/html")
   132  	_, err = io.Copy(w, &b)
   133  	if err != nil {
   134  		http.Error(w, fmt.Sprintln("Copy error:", err),
   135  			http.StatusInternalServerError)
   136  		return
   137  	}
   138  }