Skip to content

Commit

Permalink
feat: get teler-waf version
Browse files Browse the repository at this point in the history
  • Loading branch information
dwisiswant0 committed Sep 6, 2023
1 parent f9af063 commit 8d62d97
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 4 deletions.
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
TELER_WAF_VERSION := $(shell go list -m -f '{{ .Version }}' github.com/kitabisa/teler-waf)

build:
go build -ldflags "-s -w" -o playground .
go build -ldflags "-s -w -X 'main.telerWAFVersion=$(TELER_WAF_VERSION)'" -o playground .
15 changes: 15 additions & 0 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,28 @@ import (
"strings"

"encoding/json"
"html/template"
"net/http"
"net/http/httptest"

"github.com/kitabisa/teler-waf"
"github.com/kitabisa/teler-waf/option"
)

func indexHandler(w http.ResponseWriter, r *http.Request) {
tmpl, err := template.ParseFiles("www/index.html")
if err != nil {
handleError(w, http.StatusInternalServerError, err.Error())
return
}

err = tmpl.Execute(w, meta)
if err != nil {
handleError(w, http.StatusInternalServerError, err.Error())
return
}
}

func playHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")

Expand Down
9 changes: 7 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,19 @@ func init() {
if portEnv != "" {
port = portEnv
}

hostPort = net.JoinHostPort("0.0.0.0", port)

meta = map[string]interface{}{
"Version": telerWAFVersion,
}
}

func main() {
http.Handle("/", http.FileServer(http.Dir("www")))
http.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir("www/assets/"))))
http.HandleFunc("/", indexHandler)
http.HandleFunc("/api/play", playHandler)

log.Printf("teler-waf version %s\n", telerWAFVersion)
log.Printf("Server started on http://%s\n", hostPort)
if err := http.ListenAndServe(hostPort, nil); err != nil {
log.Fatal(err)
Expand Down
4 changes: 4 additions & 0 deletions vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ package main
var (
port string = "3000"
hostPort string

telerWAFVersion string = "Unknown"

meta map[string]any
)
2 changes: 1 addition & 1 deletion www/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
</head>
<body>
<h1>teler WAF Playground</h1>

<pre>> teler-waf version <a href="https://github.com/kitabisa/teler-waf/releases/tag/{{ .Version }}">{{ .Version }}</a></pre>
<div class="playground">
<button id="play" type="button" onclick="return bake(this);">Play</button>
<button id="clear" type="button" onclick="return clearLogs();">Clear logs</button>
Expand Down

0 comments on commit 8d62d97

Please sign in to comment.