Skip to content

Commit

Permalink
Move test template HTML into static directory
Browse files Browse the repository at this point in the history
  • Loading branch information
iradul committed Sep 10, 2018
1 parent 48c4611 commit bcc89d0
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 29 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@ build/
certs/
deploy/
debug
test.html
1 change: 0 additions & 1 deletion k2ws/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ func main() {
configFile := flag.String("config", "config.yaml", "Config file location")
flag.Parse()

readTemplate()
list := ReadK2WS(*configFile)
for i := range list {
go func(k2ws *K2WS) {
Expand Down
24 changes: 18 additions & 6 deletions k2ws/k2ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"html/template"
"log"
"net/http"
"net/url"
Expand Down Expand Up @@ -30,6 +31,12 @@ type K2WSKafka struct {
MessageType string
}

type templateInfo struct {
TestPath, WSURL string
}

var localStatic = false
var testTemplate *template.Template
var rexStatic = regexp.MustCompile(`(.*)(/static/.+(\.[a-z0-9]+))$`)
var mimeTypes = map[string]string{
".js": "application/javascript",
Expand Down Expand Up @@ -79,7 +86,7 @@ func (k2ws *K2WS) ServeHTTP(w http.ResponseWriter, r *http.Request) {
testUIPath = "/"
}
if _, exists := k2ws.TestUIs[testUIPath]; exists {
if payload, err := FSByte(false, submatch[2]); err == nil {
if payload, err := FSByte(localStatic, submatch[2]); err == nil {
var mime = "application/octet-stream"
if m, ok := mimeTypes[submatch[3]]; ok {
mime = m
Expand All @@ -91,11 +98,16 @@ func (k2ws *K2WS) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
}
} else if wsPath, exists := k2ws.TestUIs[r.URL.Path]; exists {
// readTemplate()
if k2ws.TLSCertFile != "" {
homeTemplate.Execute(w, templateInfo{strings.TrimRight(r.URL.Path, "/"), "wss://" + r.Host + *wsPath})
} else {
homeTemplate.Execute(w, templateInfo{strings.TrimRight(r.URL.Path, "/"), "ws://" + r.Host + *wsPath})
html, err := FSString(localStatic, "/static/test.html")
if err == nil {
wsURL := "ws://" + r.Host + *wsPath
if k2ws.TLSCertFile != "" {
wsURL = "wss://" + r.Host + *wsPath
}
if testTemplate == nil || localStatic {
testTemplate = template.Must(template.New("").Parse(html))
}
testTemplate.Execute(w, templateInfo{strings.TrimRight(r.URL.Path, "/"), wsURL})
}
return
} else if kcfg, exists := k2ws.WebSockets[r.URL.Path]; exists {
Expand Down
64 changes: 64 additions & 0 deletions k2ws/static.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 0 additions & 21 deletions k2ws/html-template.go → k2ws/static/test.html
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
package main

import (
"html/template"
)

type templateInfo struct {
TestPath, WSURL string
}

var homeTemplate *template.Template

func readTemplate() {
homeTemplate = template.Must(template.New("").Parse(`
<!DOCTYPE html>
<html style="height:100%">

Expand Down Expand Up @@ -354,10 +340,3 @@
</body>

</html>
`))
// html, err := ioutil.ReadFile("../test.html")
// if err != nil {
// log.Printf("Error while reading config.yaml file: \n%v ", err)
// }
// homeTemplate = template.Must(template.New("").Parse(string(html)))
}

0 comments on commit bcc89d0

Please sign in to comment.