User orgs api
This commit is contained in:
parent
317ee6e075
commit
1bcebaed59
|
@ -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
|
||||
}
|
|
@ -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)
|
||||
}
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue