1
0
Fork 0
empact/server/server.go

63 lines
1.6 KiB
Go

package server
import (
"encoding/json"
"html/template"
"log"
"net/http"
"github.com/GeertJohan/go.rice"
)
var (
helloTmpl = template.New("hello")
appTmpl = template.New("app")
box = rice.MustFindBox("app")
)
func init() {
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("/hello", appHelloHandler)
http.HandleFunc("/app", appAppHandler)
http.HandleFunc("/auth/signin", authSigninHandler)
http.HandleFunc("/auth/callback", authCallbackHandler)
http.HandleFunc("/api/", authHandler)
http.HandleFunc("/api/orgs", apiOrgsHandler)
http.HandleFunc("/api/teams", apiTeamsHandler)
http.HandleFunc("/api/repos", apiReposHandler)
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)
}
func Start() {
log.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)
}