App, server helpers
This commit is contained in:
parent
13f971a731
commit
3f034017f2
|
@ -0,0 +1,20 @@
|
|||
{{define "app"}}
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf8">
|
||||
<title>Empact</title>
|
||||
<link rel="stylesheet" href="/app/hello.css">
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<h1>Please Sign in with your GitHub account</h1>
|
||||
<a href="/auth/signin">
|
||||
<img src="/app/github_mark_120.png">
|
||||
</a>
|
||||
|
||||
<script src="/app/hello.js"></script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
{{end}}
|
Before Width: | Height: | Size: 4.2 KiB After Width: | Height: | Size: 4.2 KiB |
|
@ -1,6 +1,7 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"net/http"
|
||||
|
@ -10,13 +11,16 @@ import (
|
|||
|
||||
var (
|
||||
helloTmpl = template.New("hello")
|
||||
appTmpl = template.New("app")
|
||||
box = rice.MustFindBox("app")
|
||||
)
|
||||
|
||||
func init() {
|
||||
box := rice.MustFindBox("static")
|
||||
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(box.HTTPBox())))
|
||||
tmplText, _ := box.String("hello.tmpl")
|
||||
helloTmpl, _ = helloTmpl.Parse(tmplText)
|
||||
parseTemplate("hello.tmpl", helloTmpl)
|
||||
parseTemplate("app.tmpl", appTmpl)
|
||||
|
||||
// Serving static files
|
||||
http.Handle("/app/", http.StripPrefix("/app/", http.FileServer(box.HTTPBox())))
|
||||
|
||||
http.HandleFunc("/", sessionHandler)
|
||||
http.HandleFunc("/api/", authHandler)
|
||||
|
@ -32,3 +36,21 @@ func Start() {
|
|||
fmt.Println("Starting server at http://localhost:8080")
|
||||
http.ListenAndServe(":8080", nil)
|
||||
}
|
||||
|
||||
func parseTemplate(file string, tmpl *template.Template) {
|
||||
if tmplText, err := box.String(file); err == nil {
|
||||
tmpl, _ = tmpl.Parse(tmplText)
|
||||
} else {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func respondWith(w http.ResponseWriter, resp interface{}) {
|
||||
b, err := json.Marshal(resp)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf8")
|
||||
w.Write(b)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue