Created
May 2, 2013 17:36
-
-
Save grapswiz/5503908 to your computer and use it in GitHub Desktop.
appengine/go UserService
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| application: goxembourg | |
| version: 1 | |
| runtime: go | |
| api_version: go1 | |
| handlers: | |
| - url: /v1.* | |
| script: _go_app |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package togo | |
| import ( | |
| "net/http" | |
| "fmt" | |
| "encoding/json" | |
| "appengine" | |
| "appengine/user" | |
| ) | |
| type Auth struct { | |
| LoginUrl string | |
| LogoutUrl string | |
| LoggedIn bool | |
| } | |
| type Json interface { | |
| toJson() | |
| } | |
| func (auth Auth) toJson() (ret string) { | |
| b, err := json.Marshal(auth) | |
| if err != nil { | |
| return "{}" | |
| } | |
| return string(b) | |
| } | |
| func init() { | |
| http.HandleFunc("/", handler) | |
| http.HandleFunc("/v1/auth", authHandler) | |
| } | |
| func handler(rw http.ResponseWriter, req *http.Request) { | |
| fmt.Fprintf(rw, "Hello, Go") | |
| } | |
| func authHandler(rw http.ResponseWriter, req *http.Request) { | |
| c := appengine.NewContext(req) | |
| u := user.Current(c) | |
| var auth Auth | |
| auth.LoginUrl, _ = user.LoginURL(c, "/") | |
| auth.LogoutUrl, _ = user.LogoutURL(c, "/") | |
| if u == nil { | |
| auth.LoggedIn = false | |
| } else { | |
| auth.LoggedIn = true | |
| } | |
| rw.Header().Set("Content-Type", "application/json") | |
| fmt.Fprintf(rw, "%s", auth.toJson()) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment