1
0
Fork 0

User orgs api

This commit is contained in:
Gregory Eremin 2015-03-05 22:25:26 +07:00
parent 317ee6e075
commit 1bcebaed59
4 changed files with 59 additions and 4 deletions

30
db/org.go Normal file
View File

@ -0,0 +1,30 @@
package db
type (
Org struct {
Login string
Descr string
ID int64
AvatarURL string
}
)
const (
userOrgsQuery = "select org from members where user = ?"
)
func UserOrgs(login string) (orgs []string) {
if res, err := stmt(userOrgsQuery).Query(login); err == nil {
defer res.Close()
for res.Next() {
var org string
if err := res.Scan(&org); err != nil {
panic(err)
}
orgs = append(orgs, org)
}
} else {
panic(err)
}
return
}

17
server/api.go Normal file
View File

@ -0,0 +1,17 @@
package server
import (
"encoding/json"
"net/http"
"github.com/localhots/empact/db"
)
func orgsListHandler(w http.ResponseWriter, r *http.Request) {
jsonHandler(w, r)
login := currentUser(r)
orgs := db.UserOrgs(login)
b, _ := json.Marshal(orgs)
w.Write(b)
}

View File

@ -12,10 +12,6 @@ import (
)
func authHelloHandler(w http.ResponseWriter, r *http.Request) {
if currentUser(r) != "" {
http.Redirect(w, r, "/app", 302)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf8")
helloTmpl.ExecuteTemplate(w, "hello", map[string]interface{}{})
}
@ -49,3 +45,9 @@ func authCallbackHandler(w http.ResponseWriter, r *http.Request) {
}
}
}
func authHandler(w http.ResponseWriter, r *http.Request) {
if currentUser(r) == "" {
http.Redirect(w, r, "/auth/hello", 302)
}
}

View File

@ -19,11 +19,17 @@ func init() {
helloTmpl, _ = helloTmpl.Parse(tmplText)
http.HandleFunc("/", sessionHandler)
http.HandleFunc("/api/", authHandler)
http.HandleFunc("/api/orgs", orgsListHandler)
http.HandleFunc("/auth/hello", authHelloHandler)
http.HandleFunc("/auth/signin", authSigninHandler)
http.HandleFunc("/auth/callback", authCallbackHandler)
}
func jsonHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf8")
}
func Start() {
fmt.Println("Starting server at http://localhost:8080")
http.ListenAndServe(":8080", nil)