-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
51 lines (43 loc) · 1.2 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
package main
import (
/*"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"*/
//"test3/database"
"log"
"net/http"
"test3/controllers"
"test3/database"
"test3/entity"
"github.com/gorilla/mux"
_ "github.com/jinzhu/gorm/dialects/mysql"
)
func main() {
initDB()
log.Println("Startint the HTTP server on port 8080")
router := mux.NewRouter().StrictSlash(true)
initaliseHandlers(router)
log.Fatal(http.ListenAndServe(":8080", router))
}
func initaliseHandlers(router *mux.Router) {
router.HandleFunc("/create", controllers.CreateStudent).Methods("POST")
router.HandleFunc("/get", controllers.GetAllStudent).Methods("GET")
router.HandleFunc("/get/{id}", controllers.GetStudentByID).Methods("GET")
router.HandleFunc("/update/{id}", controllers.UpdateStudentByID).Methods("PUT")
router.HandleFunc("/delete/{id}", controllers.DeleteStudentByID).Methods("DELETE")
}
func initDB() {
config :=
database.Config{
ServerName: "localhost:3306",
User: "ey",
Password: "code64CRUD",
DB: "students",
}
connectionString := database.GetConnectionString(config)
err := database.Connect(connectionString)
if err != nil {
panic(err.Error())
}
database.Migrate(&entity.Student{})
}