package main import ( "encoding/json" "io/ioutil" "log" "net/http" "github.com/andreanidouglas/auth-go/dao" "github.com/andreanidouglas/auth-go/internals" model "github.com/andreanidouglas/auth-go/model" ) type ResponseLogin struct { Authenticated bool `json:"authenticated"` User model.AuthUser `json:"user,omitempty"` } type ResponseSignup struct { Created bool `json:"created"` User model.AuthUser `json:"user,omitempty"` } func (r *ResponseSignup) JsonResponseSignInSetAndWrite(rw http.ResponseWriter, authenticated bool, u *model.AuthUser) error { r.Created = authenticated u.PasswordHash = "" r.User = *u b, err := json.Marshal(r) if err != nil { return err } _, err = rw.Write(append(b, '\n')) return err } func (r *ResponseLogin) JsonResponseSetAndWrite(rw http.ResponseWriter, authenticated bool, u *model.AuthUser) error { r.Authenticated = authenticated u.PasswordHash = "" r.User = *u b, err := json.Marshal(r) if err != nil { return err } _, err = rw.Write(append(b, '\n')) return err } func setupRoute() { dao := dao.NewUserRepository() crypt := internals.NewCrypt() http.HandleFunc("/all", func(rw http.ResponseWriter, r *http.Request) { var authUser model.AuthUser if r.Method == "POST" { rw.WriteHeader(http.StatusMethodNotAllowed) return } all, err := authUser.GetAllUsers(dao) if err != nil { rw.WriteHeader(http.StatusInternalServerError) return } jsonRes, err := json.Marshal(all) if err != nil { rw.WriteHeader(http.StatusInternalServerError) return } rw.WriteHeader(http.StatusOK) rw.Write(jsonRes) }) http.HandleFunc("/signup", func(rw http.ResponseWriter, r *http.Request) { var authUser model.AuthUser res := &ResponseSignup{Created: false} if r.Method == "GET" { rw.WriteHeader(http.StatusMethodNotAllowed) return } reqBody, err := ioutil.ReadAll(r.Body) if err != nil { rw.WriteHeader(http.StatusBadRequest) return } err = json.Unmarshal(reqBody, &authUser) if err != nil { rw.WriteHeader(http.StatusBadRequest) return } id, err := authUser.CreateUser(dao, crypt) if err != nil { rw.WriteHeader(http.StatusBadRequest) res.JsonResponseSignInSetAndWrite(rw, false, &model.AuthUser{}) return } authUser.Id = id rw.WriteHeader(http.StatusOK) rw.Header().Add("Content-Type", "application/json") if err = res.JsonResponseSignInSetAndWrite(rw, true, &authUser); err != nil { log.Fatalf("could not write back to client: %v", err) } }) http.HandleFunc("/login", func(rw http.ResponseWriter, r *http.Request) { var authUser model.AuthUser res := &ResponseLogin{Authenticated: false} if r.Method == "GET" { rw.WriteHeader(http.StatusMethodNotAllowed) return } reqBody, err := ioutil.ReadAll(r.Body) if err != nil { rw.WriteHeader(http.StatusBadRequest) return } err = json.Unmarshal(reqBody, &authUser) if err != nil { rw.WriteHeader(http.StatusBadRequest) return } id, err := authUser.AuthenticateUser(dao, crypt) if err != nil { log.Printf("could not authenticate user: %v", err) rw.WriteHeader(http.StatusUnauthorized) res.JsonResponseSetAndWrite(rw, false, &model.AuthUser{}) return } authUser.Id = id rw.WriteHeader(http.StatusOK) rw.Header().Add("Content-Type", "application/json") if err = res.JsonResponseSetAndWrite(rw, true, &authUser); err != nil { log.Fatalf("could not write back to client: %v", err) } }) } func main() { setupRoute() log.Fatal(http.ListenAndServe("localhost:8080", nil)) }