github.com/pbberlin/go-pwa@v0.0.0-20220328105622-7c26e0ca1ab8/cmd/server/handlers.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"net/http"
     7  	"runtime/metrics"
     8  	"sort"
     9  	"strings"
    10  )
    11  
    12  func home(w http.ResponseWriter, r *http.Request) {
    13  
    14  	err := r.ParseForm()
    15  	if err != nil {
    16  		log.Printf("parsing form %v", err)
    17  	}
    18  	if r.Form.Get("refuse") != "" {
    19  		w.WriteHeader(401)
    20  		log.Print("offline 401")
    21  		return
    22  	}
    23  
    24  	cnt := &strings.Builder{}
    25  	fmt.Fprintf(cnt, "<p>Hello, TLS user </p>\n")
    26  	fmt.Fprintf(cnt, "<p>Your config: </p>\n")
    27  	fmt.Fprintf(cnt, "<pre id='tls-config'>%+v  </pre>\n", r.TLS)
    28  
    29  	sc := &Scaffold{}
    30  	sc.Title = "Home page"
    31  	sc.render(w, cnt)
    32  }
    33  
    34  func offline(w http.ResponseWriter, r *http.Request) {
    35  	cnt := &strings.Builder{}
    36  	fmt.Fprintf(cnt,
    37  		`<p>
    38  			This beautiful offline page<br>
    39  			provides a way better "user experience".
    40  
    41  		</p>`)
    42  
    43  	sc := &Scaffold{}
    44  	sc.Title = "You are offline"
    45  	sc.render(w, cnt)
    46  }
    47  
    48  func plain(w http.ResponseWriter, r *http.Request) {
    49  	w.Header().Set("Content-Type", "text/plain")
    50  	fmt.Fprintf(w, "This is an example server.\n")
    51  }
    52  
    53  func saveJson(w http.ResponseWriter, r *http.Request) {
    54  	w.Header().Set("Content-Type", "application/json")
    55  	if r.PostFormValue("refuse") != "" {
    56  		return
    57  	}
    58  	fmt.Fprintf(w, "%+v", r.PostForm)
    59  }
    60  
    61  func layoutTemplateForJS(w http.ResponseWriter, r *http.Request) {
    62  	cnt := &strings.Builder{}
    63  	fmt.Fprintf(cnt, "${content}")
    64  	sc := &Scaffold{}
    65  	sc.Title = "${headerTitle}"
    66  	sc.Desc = "${headerDesc}"
    67  	sc.render(w, cnt)
    68  }
    69  
    70  var mtrcs = map[string]string{
    71  	"gcPauses":       "/gc/pauses:seconds",
    72  	"schedLatencies": "/sched/latencies:seconds",
    73  	"memoryHeapFree": "/memory/classes/heap/free:bytes",
    74  }
    75  
    76  // golangMetrics uses new 2021 package metrics
    77  func golangMetrics(w http.ResponseWriter, r *http.Request) {
    78  
    79  	w.Header().Set("Content-Type", "text/plain")
    80  
    81  	srt := make([]string, 0, len(mtrcs))
    82  	for key := range mtrcs {
    83  		srt = append(srt, key)
    84  	}
    85  	sort.Strings(srt)
    86  
    87  	for _, key := range srt {
    88  		val := mtrcs[key]
    89  
    90  		// create smpl and read it
    91  		smpl := make([]metrics.Sample, 1)
    92  		smpl[0].Name = val
    93  		metrics.Read(smpl)
    94  
    95  		if smpl[0].Value.Kind() == metrics.KindBad {
    96  			fmt.Fprintf(w, "\tmetric %q unsupported\n", val)
    97  		}
    98  
    99  		// uintVal := smpl[0].Value.Uint64()
   100  		fmt.Fprintf(w, "%-24v as %T: %d\n", key, smpl[0].Value, smpl[0].Value)
   101  	}
   102  }