-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
68 lines (57 loc) · 1.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
package main
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"phonebook-backend/handlers"
"phonebook-backend/models"
"github.com/gorilla/mux"
"github.com/mongodb/mongo-go-driver/mongo"
)
// DBNAME Database name
const DBNAME = "phonebook"
// COLLECTION Collection name
const COLLECTION = "people"
// CONNECTIONSTRING DB connection string
const CONNECTIONSTRING = "mongodb://localhost:27017"
func init() {
// Populates database with dummy data
var people []models.Person
client, err := mongo.NewClient(CONNECTIONSTRING)
if err != nil {
log.Fatal(err)
}
err = client.Connect(context.Background())
if err != nil {
log.Fatal(err)
}
db := client.Database(DBNAME)
// Load values from JSON file to model
byteValues, err := ioutil.ReadFile("person_data.json")
if err != nil {
log.Fatal(err)
}
json.Unmarshal(byteValues, &people)
// Insert people into DB
var ppl []interface{}
for _, p := range people {
ppl = append(ppl, p)
}
_, err = db.Collection(COLLECTION).InsertMany(context.Background(), ppl)
if err != nil {
log.Fatal(err)
}
}
func main() {
router := mux.NewRouter()
router.HandleFunc("/people", handlers.GetAllPeopleEndpoint).Methods("GET")
router.HandleFunc("/people/{id}", handlers.GetPersonEndpoint).Methods("GET")
router.HandleFunc("/people", handlers.CreatePersonEndpoint).Methods("POST")
router.HandleFunc("/people", handlers.DeletePersonEndpoint).Methods("DELETE")
router.HandleFunc("/people/{id}", handlers.UpdatePersonEndpoint).Methods("PUT")
fmt.Println("Starting server on port 8000...")
log.Fatal(http.ListenAndServe(":8000", router))
}