func main() { f, _ := os.Create("log/membership.log") log.SetOutput(f) //http.HandleFunc("/favicon.ico", endpoints.StaticHandler) //http.HandleFunc("/login", endpoints.LoginHandler) //http.HandleFunc("/", endpoints.Handler) port := os.Args[1] log.Println("rpc listening on port:", port) // err := http.ListenAndServe(":"+port, nil) // if err != nil { // fmt.Printf("{error: \"%s\"}", err.Error()) // } //set up to listen for rpc iport, err := strconv.ParseInt(port, 10, 64) if err != nil { log.Printf("port error: %s\n", err.Error()) } //for rdp, we don't know the key or namespace at this point svc := service.NewMembershipService("", "") svr, err := rpc.NewServer("Membership", svc, iport) for { svr.Accept() } if err != nil { log.Fatal("server error:", err) } }
func LoginHandler(w http.ResponseWriter, r *http.Request) { ref := r.Referer() if ref == "" { ref = "localhost" } service := service.NewMembershipService() service.Init(ref) m := r.Method if m != "POST" { server.JsonErr(w, errors.New("must POST when logging in")) } dec := json.NewDecoder(r.Body) var reg models.Registration err := dec.Decode(reg) if err != nil { server.JsonErr(w, err) return } reg, err = service.Login(reg) if err != nil { server.JsonErr(w, err) } server.Json(w, reg) return }
func Handler(w http.ResponseWriter, r *http.Request) { ref := r.Referer() if ref == "" { ref = "localhost" } service := service.NewMembershipService() service.Init(ref) var reg models.Registration switch r.Method { case "GET": sID := r.URL.Path[1:] log.Println("routing GET", sID) var id int64 = 0 if sID == "" { id = 0 } else { convid, err := strconv.ParseInt(sID, 10, 64) id = convid if err != nil { server.JsonErr(w, err) return } } if id == 0 { reg, err := service.List() if err != nil { server.JsonErr(w, err) return } server.Json(w, reg) return } else { reg, err := service.Get(id) if err != nil { server.JsonErr(w, err) return } server.Json(w, reg) return } case "POST": dec := json.NewDecoder(r.Body) err := dec.Decode(®) if err != nil { server.JsonErr(w, err) return } reg, err := service.Create(reg) if err != nil { server.JsonErr(w, err) return } server.Json(w, reg) return case "PUT": dec := json.NewDecoder(r.Body) err := dec.Decode(®) if err != nil { server.JsonErr(w, err) return } reg, err = service.Update(reg) if err != nil { server.JsonErr(w, err) return } server.Json(w, r) return case "DELETE": sID := r.URL.Path[1:] id, err := strconv.ParseInt(sID, 10, 64) err = service.Delete(id) if err != nil { server.JsonErr(w, err) return } server.JsonStatus(w, 1, "registration deleted") return } }