-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
61 lines (50 loc) · 1.28 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
package main
import (
"fmt"
"log"
"net/http"
"github.com/ceylanomer/go-modules/customercontext"
"github.com/ceylanomer/go-modules/models"
"github.com/ceylanomer/go-modules/services"
"github.com/gorilla/mux"
"gorm.io/gorm"
)
func main() {
str, err2 := customercontext.CreateDBIfNotExist()
if err2 == nil {
fmt.Println(str)
db, err := customercontext.ConnectDB()
if err != nil {
fmt.Println(err)
}
MigrateDB(db)
InitializeDB(db)
StartAPI()
} else {
fmt.Println(str + err2.Error())
}
}
//MigrateDB migrates db
func MigrateDB(db *gorm.DB) {
db.AutoMigrate(&models.Customer{})
}
//InitializeDB is initializes the db
func InitializeDB(db *gorm.DB) {
db.Create(&models.Customer{
Name: "TestCustomer",
Address: "Istanbul",
PhoneNumber: "5425425454",
})
// var customer models.Customer
// db.First(&customer, 1)
// db.First(&customer, "Address = ?", "Istanbul")
// db.Model(&customer).Update("Address", "Atasehir")
}
//StartAPI Starts the API
func StartAPI() {
myRouter := mux.NewRouter().StrictSlash(true)
myRouter.HandleFunc("/customers", services.GetAllCustomers).Methods("GET")
myRouter.HandleFunc("/customer", services.AddCustomer).Methods("POST")
fmt.Println("Now listening on: localhost:8081")
log.Fatal(http.ListenAndServe(":8081", myRouter))
}