Source

GtugGoSandbox / goproj / positions / positions.go

package positions

import (
    "appengine"
    "appengine/user"
    "fmt"
    "http"
    "json"
    "rand"
)


type Position struct{
    Name string
    Location [2]float32
}

func init(){
    http.HandleFunc("/position/",handler)
    http.HandleFunc("/", defaultHandler)
}

func handler(w http.ResponseWriter,r *http.Request){
    c := appengine.NewContext(r)
    u := user.Current(c)
    if u == nil{
        url, err := user.LoginURL(c,r.URL.String())
        if err != nil{
            http.Error(w,err.String(),http.StatusInternalServerError)
            return
        }
        w.Header().Set("Location",url)
        w.WriteHeader(http.StatusFound)
        return
    }

    p := Position{u.String(),[2]float32{101.66478+rand.Float32(),3.10593+rand.Float32()}}
    b,err := json.Marshal(p)
    if err != nil{
        http.Error(w,err.String(),http.StatusInternalServerError)
        return
    }
    fmt.Fprint(w,string(b))
}