Skip to content

Instantly share code, notes, and snippets.

@grapswiz
Created May 2, 2013 17:36
Show Gist options
  • Select an option

  • Save grapswiz/5503908 to your computer and use it in GitHub Desktop.

Select an option

Save grapswiz/5503908 to your computer and use it in GitHub Desktop.
appengine/go UserService
application: goxembourg
version: 1
runtime: go
api_version: go1
handlers:
- url: /v1.*
script: _go_app
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