From 8d62d977b82eef6ca7a8dc6a0436f93e87444c92 Mon Sep 17 00:00:00 2001 From: Dwi Siswanto Date: Thu, 7 Sep 2023 04:59:00 +0700 Subject: [PATCH] feat: get teler-waf version --- Makefile | 4 +++- handler.go | 15 +++++++++++++++ main.go | 9 +++++++-- vars.go | 4 ++++ www/index.html | 2 +- 5 files changed, 30 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 027a7fe..fbe2f7d 100644 --- a/Makefile +++ b/Makefile @@ -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 . \ No newline at end of file + go build -ldflags "-s -w -X 'main.telerWAFVersion=$(TELER_WAF_VERSION)'" -o playground . \ No newline at end of file diff --git a/handler.go b/handler.go index fc30a0d..ef99df3 100644 --- a/handler.go +++ b/handler.go @@ -7,6 +7,7 @@ import ( "strings" "encoding/json" + "html/template" "net/http" "net/http/httptest" @@ -14,6 +15,20 @@ import ( "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") diff --git a/main.go b/main.go index 89fa8a6..9510d30 100644 --- a/main.go +++ b/main.go @@ -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) diff --git a/vars.go b/vars.go index ead098c..1bc4e7f 100644 --- a/vars.go +++ b/vars.go @@ -3,4 +3,8 @@ package main var ( port string = "3000" hostPort string + + telerWAFVersion string = "Unknown" + + meta map[string]any ) diff --git a/www/index.html b/www/index.html index 16f65fa..23c7358 100644 --- a/www/index.html +++ b/www/index.html @@ -36,7 +36,7 @@

teler WAF Playground

- +
> teler-waf version {{ .Version }}