1
0
Fork 0
empact/server/server.go

58 lines
1.3 KiB
Go
Raw Normal View History

2015-03-05 07:46:19 +00:00
package server
import (
2015-03-06 12:15:11 +00:00
"encoding/json"
2015-03-05 13:49:50 +00:00
"html/template"
2015-03-06 13:23:01 +00:00
"log"
2015-03-05 07:46:19 +00:00
"net/http"
2015-03-05 12:57:36 +00:00
2015-03-05 13:49:50 +00:00
"github.com/GeertJohan/go.rice"
2015-03-05 12:57:36 +00:00
)
2015-03-05 13:49:50 +00:00
var (
helloTmpl = template.New("hello")
2015-03-06 12:15:11 +00:00
appTmpl = template.New("app")
box = rice.MustFindBox("app")
2015-03-05 13:49:50 +00:00
)
func init() {
2015-03-06 12:15:11 +00:00
parseTemplate("hello.tmpl", helloTmpl)
parseTemplate("app.tmpl", appTmpl)
// Serving static files
http.Handle("/app/", http.StripPrefix("/app/", http.FileServer(box.HTTPBox())))
2015-03-05 13:49:50 +00:00
2015-03-05 12:57:36 +00:00
http.HandleFunc("/", sessionHandler)
2015-03-06 12:36:35 +00:00
http.HandleFunc("/hello", appHelloHandler)
http.HandleFunc("/app", appAppHandler)
http.HandleFunc("/auth/signin", authSigninHandler)
http.HandleFunc("/auth/callback", authCallbackHandler)
2015-03-05 15:25:26 +00:00
http.HandleFunc("/api/", authHandler)
2015-03-06 11:18:15 +00:00
http.HandleFunc("/api/orgs", apiOrgsHandler)
http.HandleFunc("/api/teams", apiTeamsHandler)
http.HandleFunc("/api/repos", apiReposHandler)
2015-03-05 13:49:50 +00:00
}
func Start() {
2015-03-06 13:23:01 +00:00
log.Println("Starting server at http://localhost:8080")
2015-03-05 07:46:19 +00:00
http.ListenAndServe(":8080", nil)
}
2015-03-06 12:15:11 +00:00
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)
}