-
Notifications
You must be signed in to change notification settings - Fork 7
/
bookthief.go
116 lines (102 loc) · 3.17 KB
/
bookthief.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Package main implements the bookthief application
package main
import (
_ "embed"
"flag"
"fmt"
"html"
"html/template"
"net/http"
"time"
"github.com/gorilla/mux"
"github.com/flomesh-io/fsm/demo/cmd/common"
"github.com/flomesh-io/fsm/pkg/logger"
"github.com/flomesh-io/fsm/pkg/utils"
)
const (
participantName = "bookthief"
httpStatusNotFound = "404"
)
var (
//go:embed bookthief.html.template
htmlTpl string
tmpl *template.Template
books common.BookThiefThievery
log = logger.NewPretty(participantName)
port = flag.Int("port", 14001, "port on which this app is listening for incoming HTTP")
)
func renderTemplate(w http.ResponseWriter) {
err := tmpl.Execute(w, map[string]string{
"Identity": getIdentity(),
"BooksStolenV1": fmt.Sprintf("%d", books.BooksStolenV1),
"BooksStolenV2": fmt.Sprintf("%d", books.BooksStolenV2),
"BooksStolen": fmt.Sprintf("%d", books.BooksStolen),
"Time": time.Now().Format("Mon, 02 Jan 2006 15:04:05 MST"),
})
if err != nil {
log.Fatal().Err(err).Msg("Could not render template")
}
}
func getIdentity() string {
return utils.GetEnv("IDENTITY", "Bookthief")
}
type handler struct {
path string
fn func(http.ResponseWriter, *http.Request)
method string
}
func getIndex(w http.ResponseWriter, r *http.Request) {
renderTemplate(w)
fmt.Printf("%s; URL: %q; Count: %d\n", getIdentity(), html.EscapeString(r.URL.Path), books.BooksStolen)
}
func getHandlers() []handler {
return []handler{
{"/", getIndex, "GET"},
{"/raw", common.GetRawGenerator(&books), "GET"},
{"/reset", reset, "GET"},
}
}
func reset(w http.ResponseWriter, _ *http.Request) {
books.BooksStolen = 0
books.BooksStolenV1 = 0
books.BooksStolenV2 = 0
renderTemplate(w)
}
func debugServer() {
flag.Parse()
var err error
tmpl, err = template.New("").Parse(htmlTpl)
if err != nil {
log.Fatal().Err(err).Msg("Failed to parse HTML template file")
}
router := mux.NewRouter()
for _, h := range getHandlers() {
router.HandleFunc(h.path, h.fn).Methods(h.method)
}
http.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {})
log.Info().Msgf("Bookthief running on port %d", *port)
//#nosec G114: Use of net/http serve function that has no support for setting timeouts
err = http.ListenAndServe(fmt.Sprintf(":%d", *port), router)
log.Fatal().Err(err).Msgf("Failed to start HTTP server on port %d", *port)
}
func main() {
go debugServer()
// The bookthief is not allowed to purchase books from the bookstore.
//
// Depending on whether SMI policies or permissive traffic policy is enabled,
// the HTTP response status code will differ for in-mesh requests.
//
// Expected response code when bookthief tries to buy books from the bookstore:
// 1. With SMI policies: 0
// 2. With permissive traffic policy: 200
//
// When it tries to make an egress request, we expect a 200 response with egress enabled and a 404 response with egress disabled.
meshExpectedResponseCode := common.GetExpectedResponseCodeFromEnvVar(common.BookthiefExpectedResponseCodeEnvVar, httpStatusNotFound)
common.GetBooks(
participantName,
meshExpectedResponseCode,
&books.BooksStolen,
&books.BooksStolenV1,
&books.BooksStolenV2,
)
}