//RegisterUser ... func (client *ClientController) RegisterUser(res http.ResponseWriter, req *http.Request) { //Extract the models from the request render := render.New(render.Options{}) registeredType := new(services.RegisteredUser) errs := binding.Bind(req, registeredType) userlogic.ServiceList = client.ServiceList userlogic.Logger = client.Logger if errs.Handle(res) { client.Logger.Crit(errs.Error()) render.JSON(res, 422, errs.Error()) return } bindingErr := registeredType.Validate(req, errs) if bindingErr != nil { client.Logger.Crit(bindingErr.Error()) render.JSON(res, 422, bindingErr.Error()) return } person, user, err := userlogic.RegisterUser(registeredType.Person, registeredType.User) if err != nil { client.Logger.Error(err.Error()) panic(err) } render.JSON(res, 200, map[string]interface{}{"Person": person, "User": user}) }
//AddNewAddressToUser ... Add address to a user profile func (client *ClientController) AddNewAddressToUser(res http.ResponseWriter, req *http.Request, next http.HandlerFunc) { render := render.New(render.Options{}) address := models.Addresses{} errs := binding.Bind(req, &address) userlogic.ServiceList = client.ServiceList userlogic.Logger = client.Logger userlogic.AuthorizationToken = req.Header.Get("Authorization") if errs.Handle(res) { client.Logger.Crit(errs.Error()) render.JSON(res, 422, errs.Error()) return } bindingErr := address.Validate(req, errs) if bindingErr != nil { client.Logger.Crit(bindingErr.Error()) render.JSON(res, 422, bindingErr.Error()) return } savedEntity, bSave, err := userlogic.AddNewAddressToUser(address) if !bSave && err != nil { client.Logger.Error(err.Error()) panic(err) } render.JSON(res, 200, savedEntity) }
//RefreshUserToken ... func (client *ClientController) RefreshUserToken(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) { render := render.New(render.Options{}) bs, err := ioutil.ReadAll(r.Body) if err != nil { render.JSON(w, 422, err.Error()) return } resp, body, reqErrs := request.Post(client.ServiceList["auth"] + "/refresh-token-auth").Send(string(bs)).End() if reqErrs != nil { panic(reqErrs) } render.JSON(w, resp.StatusCode, body) }
//LogoutUser ... func (client *ClientController) LogoutUser(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) { render := render.New(render.Options{}) resp, body, reqErrs := request.Get(client.ServiceList["auth"]+"/logout").Set("Authorization", r.Header.Get("Authorization")).End() if reqErrs != nil { panic(reqErrs) } render.JSON(w, resp.StatusCode, body) }
// Login ... func (sec *SecurityController) Login(w http.ResponseWriter, r *http.Request) { render := render.New(render.Options{}) queryDict, errReq := sec.HTTPUtilDunc.DecodeHTTPBody(r) qryparam := map[string]interface{}{} if errReq != nil { //u.Logger.Error(errReq.Error()) panic(errReq) } username, usrExists := queryDict["Username"].(string) password, exists := queryDict["Password"].(string) email, emailExists := queryDict["Email"].(string) personDetail := new(models.Person) if !exists { render.JSON(w, 422, map[string]interface{}{"msg": "Invalid Data"}) return } user := models.User{} if usrExists { qryparam["username"] = username } if emailExists { qryparam["email"] = email } _, err := sec.DataStore.SearchAnyGenericObject(qryparam, &user) if err != nil && err.ErrNo == 1001 { if err != nil && err.ErrNo == 1001 { render.JSON(w, 404, err.Error()) return } } responseStatus, token := services.Login(&user, password) //Get user to person details if responseStatus == 401 { render.JSON(w, responseStatus, map[string]interface{}{"msg": "Unauthorized"}) return } userPersonParam := map[string]interface{}{"id": &user.PersonID} _, errUserPerson := sec.DataStore.SearchAnyGenericObject(userPersonParam, &personDetail) if errUserPerson != nil { render.JSON(w, 422, errUserPerson.Error()) } personJSON, _ := json.Marshal(personDetail) render.JSON(w, responseStatus, map[string]interface{}{"token": string(token), "user_detail": string(personJSON)}) }
//ChangePassword ... Change Password of a logged on user func (client *ClientController) ChangePassword(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) { render := render.New(render.Options{}) bs, err := ioutil.ReadAll(r.Body) if err != nil { render.JSON(w, 422, err.Error()) return } userlogic.ServiceList = client.ServiceList userlogic.AuthorizationToken = r.Header.Get("Authorization") userlogic.Logger = client.Logger user, passwordChanged, err := userlogic.ChangePassword(string(bs)) if !passwordChanged && err != nil { fmt.Println(err.Error()) panic(err) } render.JSON(w, 200, user) }
//ActivateUser ... Change Password of a logged on user func (client *ClientController) ActivateUser(w http.ResponseWriter, r *http.Request) { render := render.New(render.Options{}) userlogic.ServiceList = client.ServiceList bs, err := ioutil.ReadAll(r.Body) if err != nil { render.JSON(w, 422, err.Error()) return } userlogic.Logger = client.Logger user, passwordChanged, err := userlogic.ActivateUser(string(bs)) if err != nil { panic(err) } if !passwordChanged && err != nil { panic(err) } render.JSON(w, 200, user) }
func main() { render := render.New() // TODO: move out walker := func(p string, fi os.FileInfo, err error) error { if err != nil { return err } if path.Base(p) == "schema.json" { endpoint := "/" + path.Dir(p) log.Println("GET endpoint registered: " + endpoint) var schema Schema f, err := os.Open(p) if err != nil { return err } if err := json.NewDecoder(f).Decode(&schema); err != nil { return err } result := make(map[string]interface{}) for k, v := range schema.Properties { switch v.Type { case "string": sentence := lorem.Sentence(1, rand.Intn(10)) result[k] = sentence[0 : len(sentence)-1] case "integer": result[k] = rand.Intn(1024) default: result[k] = "unknown type" } } http.HandleFunc(endpoint, func(w http.ResponseWriter, r *http.Request) { render.JSON(w, http.StatusOK, result) }) } return nil } if err := filepath.Walk(".", walker); err != nil { // TODO: flag for path log.Fatal(err) } port := ":" port += os.Getenv("PORT") if port == ":" { port += "8080" } log.Println("Listening " + port) log.Fatal(http.ListenAndServe(port, nil)) }
func main() { mux := web.New() render := render.New(render.Options{}) mux.Use(middleware.Logger) mux.Use(jsonp.Handle) mux.Get("/", func(w http.ResponseWriter, r *http.Request) { data := &SomeObj{"superman"} render.JSON(w, 200, data) }) err := http.ListenAndServe(":4444", mux) if err != nil { log.Fatal(err) } }