Sfoglia il codice sorgente

add context values into example template page

tags/v0.9.0
jimzhan 9 anni fa
parent
commit
ac5381f603
3 ha cambiato i file con 11 aggiunte e 3 eliminazioni
  1. +7
    -1
      example/main.go
  2. +1
    -1
      example/views/index.html
  3. +3
    -1
      livereload/middleware.go

+ 7
- 1
example/main.go Vedi File

@@ -9,11 +9,17 @@ import (
"github.com/goanywhere/rex/livereload"
)

type User struct {
Username string
}

func Index(w http.ResponseWriter, r *http.Request) {
if html, err := template.ParseFiles(filepath.Join("views", "index.html")); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
} else {
html.Execute(w, nil)
w.Header().Set("Content-Type", "text/html")
var user = User{Username: rex.Env.String("USER", "guest")}
html.Execute(w, user)
}
}


+ 1
- 1
example/views/index.html Vedi File

@@ -38,7 +38,7 @@

<main>
<div class="container">
<h2>Index</h2>
<h2>Welcome, {{ .Username }}</h2>
</div>
</main>


+ 3
- 1
livereload/middleware.go Vedi File

@@ -10,6 +10,7 @@ import (
"regexp"
"strings"

"github.com/Sirupsen/logrus"
"github.com/goanywhere/env"
)

@@ -21,6 +22,7 @@ type writer struct {
func (self *writer) addJavaScript(data []byte) []byte {
javascript := fmt.Sprintf(`<script src="//%s%s"></script>
</head>`, self.host, URL.JavaScript)
logrus.Infof("JS: %s", javascript)
return regexp.MustCompile(`</head>`).ReplaceAll(data, []byte(javascript))
}

@@ -66,7 +68,7 @@ func (self *writer) Write(data []byte) (size int, e error) {

func Middleware(next http.Handler) http.Handler {
// ONLY run this under debug mode.
if !env.Bool("DEBUG", true) {
if !env.Bool("DEBUG") {
return next
}
Start()

Loading…
Annulla
Salva