-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
executable file
·71 lines (56 loc) · 1.8 KB
/
main.go
File metadata and controls
executable file
·71 lines (56 loc) · 1.8 KB
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
package main
import (
"log"
"net/http"
"os"
"test/controller"
"test/database"
"test/entity"
"github.com/gorilla/handlers"
godotenv "github.com/joho/godotenv"
"github.com/gorilla/mux"
_ "github.com/jinzhu/gorm/dialects/mysql" //Required for MySQL dialect
)
func main() {
initDB()
log.Println("Starting the HTTP server on port 8090")
router := mux.NewRouter()
headersOk := handlers.AllowedHeaders([]string{"X-Requested-With", "Content-Type", "Authorization"})
methodsOk := handlers.AllowedMethods([]string{"GET", "HEAD", "POST", "PUT", "OPTIONS", "DELETE"})
originsOk := handlers.AllowedOrigins([]string{"*"})
corsMiddleware := handlers.CORS(headersOk, methodsOk, originsOk)
initaliseHandlers(router)
log.Fatal(http.ListenAndServe(":8090", corsMiddleware(router)))
}
func initaliseHandlers(router *mux.Router) {
router.HandleFunc("/create", controller.Createproduct).Methods("POST")
router.HandleFunc("/get", controller.GetAllproduct).Methods("GET")
router.HandleFunc("/get/{id}", controller.GetproductByID).Methods("GET")
router.HandleFunc("/update/{id}", controller.UpdateproductByID).Methods("PUT")
router.HandleFunc("/delete/{id}", controller.DeletproductByID).Methods("DELETE")
}
func initDB() {
error := godotenv.Load()
if error != nil {
log.Println("Error while loading .env")
}
DB_HOST := os.Getenv("DB_HOST")
DB_PORT := os.Getenv("DB_PORT")
DB_USER := os.Getenv("DB_USER")
DB_PASSWORD := os.Getenv("DB_PASSWORD")
DB_NAME := os.Getenv("DB_NAME")
config :=
database.Config{
ServerName: DB_HOST,
Port: DB_PORT,
User: DB_USER,
Password: DB_PASSWORD,
DB: DB_NAME,
}
connectionString := database.GetConnectionString(config)
err := database.Connect(connectionString)
if err != nil {
panic(err.Error())
}
database.Migrate(&entity.Product{})
}