empact/server/server.go

63 lines
1.6 KiB
Go
Raw Normal View History

2015-03-05 14:46:19 +07:00
package server
import (
2015-03-06 19:15:11 +07:00
"encoding/json"
2015-03-05 20:49:50 +07:00
"html/template"
2015-03-06 20:23:01 +07:00
"log"
2015-03-05 14:46:19 +07:00
"net/http"
2015-03-05 19:57:36 +07:00
2015-03-05 20:49:50 +07:00
"github.com/GeertJohan/go.rice"
2015-03-05 19:57:36 +07:00
)
2015-03-05 20:49:50 +07:00
var (
helloTmpl = template.New("hello")
2015-03-06 19:15:11 +07:00
appTmpl = template.New("app")
box = rice.MustFindBox("app")
2015-03-05 20:49:50 +07:00
)
func init() {
2015-03-06 19:15:11 +07: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 20:49:50 +07:00
2015-03-05 19:57:36 +07:00
http.HandleFunc("/", sessionHandler)
2015-03-06 19:36:35 +07:00
http.HandleFunc("/hello", appHelloHandler)
http.HandleFunc("/app", appAppHandler)
http.HandleFunc("/auth/signin", authSigninHandler)
http.HandleFunc("/auth/callback", authCallbackHandler)
2015-03-05 22:25:26 +07:00
http.HandleFunc("/api/", authHandler)
2015-03-06 18:18:15 +07:00
http.HandleFunc("/api/orgs", apiOrgsHandler)
http.HandleFunc("/api/teams", apiTeamsHandler)
http.HandleFunc("/api/repos", apiReposHandler)
2015-03-06 23:29:26 +07:00
http.HandleFunc("/api/stat/repos/top", statOrgReposTop)
http.HandleFunc("/api/stat/repos/activity", statOrgReposActivity)
http.HandleFunc("/api/stat/teams/top", statOrgTeamsTop)
http.HandleFunc("/api/stat/teams/activity", statOrgTeamsActivity)
http.HandleFunc("/api/stat/users/top", statOrgUsersTop)
2015-03-05 20:49:50 +07:00
}
func Start() {
2015-03-06 20:23:01 +07:00
log.Println("Starting server at http://localhost:8080")
2015-03-05 14:46:19 +07:00
http.ListenAndServe(":8080", nil)
}
2015-03-06 19:15:11 +07: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)
}