-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
103 lines (94 loc) · 3.59 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package main
import (
"context"
"encoding/json"
"fmt"
"net/http"
"time"
"log"
"golang.org/x/crypto/bcrypt"
"github.com/gorilla/mux"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
)
var client *mongo.Client
type User struct {
ID primitive.ObjectID `json:"_id,omitempty" bson:"_id,omitempty"`
Name string `json:"name,omitempty" bson:"name,omitempty"`
Email string `json:"email,omitempty" bson:"email,omitempty"`
Password string `json:"_password,omitempty" bson:"_password,omitempty"`
}
func CreateUserEndpoint(response http.ResponseWriter, request *http.Request) {}
func GetUsersEndpoint(response http.ResponseWriter, request *http.Request) {}
func GetUserEndpoint(response http.ResponseWriter, request *http.Request) {}
//to Create an User
func CreateUserEndpoint(response http.ResponseWriter, request *http.Request) {
response.Header().Set("content-type", "application/json")
var user User
_ = json.NewDecoder(request.Body).Decode(&user)
//encrypting the password before uploding to databse
User.Password = HashPassword(User.Password)
collection := client.Database("instagram-backend").Collection("users")
ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
result, _ := collection.InsertOne(ctx, user)
json.NewEncoder(response).Encode(result)
}
//to Get a user using id
func GetUserEndpoint(response http.ResponseWriter, request *http.Request) {
response.Header().Set("content-type", "application/json")
params := mux.Vars(request)
id, _ := primitive.ObjectIDFromHex(params["id"])
var user User
collection := client.Database("instagram-backend").Collection("users")
ctx, _ := context.WithTimeout(context.Background(), 30*time.Second)
err := collection.FindOne(ctx, User{ID: id}).Decode(&user)
if err != nil {
response.WriteHeader(http.StatusInternalServerError)
response.Write([]byte(`{ "message": "` + err.Error() + `" }`))
return
}
json.NewEncoder(response).Encode(user)
}
func GetUsersEndpoint(response http.ResponseWriter, request *http.Request) {
response.Header().Set("content-type", "application/json")
var users []User
collection := client.Database("instagram-backend").Collection("users")
ctx, _ := context.WithTimeout(context.Background(), 30*time.Second)
cursor, err := collection.Find(ctx, bson.M{})
if err != nil {
response.WriteHeader(http.StatusInternalServerError)
response.Write([]byte(`{ "message": "` + err.Error() + `" }`))
return
}
defer cursor.Close(ctx)
for cursor.Next(ctx) {
var user User
cursor.Decode(&user)
users = append(users, user)
}
if err := cursor.Err(); err != nil {
response.WriteHeader(http.StatusInternalServerError)
response.Write([]byte(`{ "message": "` + err.Error() + `" }`))
return
}
json.NewEncoder(response).Encode(users)
}
//to hash encrypt the password
func HashPassword(password string) string {
bytes, err := bcrypt.GenerateFromPassword([]byte(password), 14)
if err != nil {
log.Panic(err)
}
return string(bytes)
}
func main() {
fmt.Println("Starting the application...")
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
clientOptions := options.Client().ApplyURI("mongodb://localhost:27017")
client, _ = mongo.Connect(ctx, clientOptions)
router := mux.NewRouter()
router.HandleFunc("/user", CreateUserEndpoint).Methods("POST")
router.HandleFunc("/users", GetUsersEndpoint).Methods("GET")
router.HandleFunc("/user/{id}", GetUserEndpoint).Methods("GET")
http.ListenAndServe(":12345", router)
}