github.com/google/go-safeweb@v0.0.0-20231219055052-64d8cfc90fbb/examples/echo/echo.go (about)

     1  // Copyright 2022 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //	https://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // echo implements a simple echo server which listents on localhost:8080.
    16  //
    17  // Endpoints:
    18  //   - /echo?message=YourMessage
    19  //   - /uptime
    20  package main
    21  
    22  import (
    23  	"fmt"
    24  	"log"
    25  	"net/http"
    26  	"time"
    27  
    28  	"github.com/google/safehtml"
    29  	"github.com/google/safehtml/template"
    30  
    31  	"github.com/google/go-safeweb/examples/echo/security/web"
    32  	"github.com/google/go-safeweb/safehttp"
    33  )
    34  
    35  var start time.Time
    36  
    37  func main() {
    38  	port := 8080
    39  	addr := fmt.Sprintf("localhost:%d", port)
    40  	m := web.NewMuxConfigDev(port).Mux()
    41  
    42  	m.Handle("/echo", safehttp.MethodGet, safehttp.HandlerFunc(echo))
    43  	m.Handle("/uptime", safehttp.MethodGet, safehttp.HandlerFunc(uptime))
    44  
    45  	start = time.Now()
    46  	log.Printf("Visit http://%s\n", addr)
    47  	log.Printf("Listening on %s...\n", addr)
    48  	log.Fatal(http.ListenAndServe(addr, m))
    49  }
    50  
    51  func echo(w safehttp.ResponseWriter, req *safehttp.IncomingRequest) safehttp.Result {
    52  	q, err := req.URL().Query()
    53  	if err != nil {
    54  		return w.WriteError(safehttp.StatusBadRequest)
    55  	}
    56  	x := q.String("message", "")
    57  	if len(x) == 0 {
    58  		return w.WriteError(safehttp.StatusBadRequest)
    59  	}
    60  	return w.Write(safehtml.HTMLEscaped(x))
    61  }
    62  
    63  var uptimeTmpl *template.Template = template.Must(template.New("uptime").Parse(
    64  	`<h1>Uptime: {{ .Uptime }}</h1>
    65  {{- if .EasterEgg }}<h1>You've found an easter egg using "{{ .EasterEgg }}". Congrats!</h1>{{ end -}}`))
    66  
    67  func uptime(w safehttp.ResponseWriter, req *safehttp.IncomingRequest) safehttp.Result {
    68  	var x struct {
    69  		Uptime    time.Duration
    70  		EasterEgg string
    71  	}
    72  	x.Uptime = time.Since(start)
    73  
    74  	// Easter egg handling.
    75  	q, err := req.URL().Query()
    76  	if err != nil {
    77  		return w.WriteError(safehttp.StatusBadRequest)
    78  	}
    79  	if egg := q.String("easter_egg", ""); len(egg) != 0 {
    80  		x.EasterEgg = egg
    81  	}
    82  
    83  	return safehttp.ExecuteTemplate(w, uptimeTmpl, x)
    84  }