-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathmain.go
42 lines (32 loc) · 1.09 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
package main
import (
"fmt"
"golang-crud-rest-api/controllers"
"golang-crud-rest-api/database"
"log"
"net/http"
"github.com/gorilla/mux"
"gorm.io/gorm"
)
var DB *gorm.DB
func main() {
// Load Configurations from config.json using Viper
LoadAppConfig()
// Initialize Database
database.Connect(AppConfig.ConnectionString)
database.Migrate()
// Initialize the router
router := mux.NewRouter().StrictSlash(true)
// Register Routes
RegisterProductRoutes(router)
// Start the server
log.Println(fmt.Sprintf("Starting Server on port %s", AppConfig.Port))
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%v", AppConfig.Port), router))
}
func RegisterProductRoutes(router *mux.Router) {
router.HandleFunc("/api/products", controllers.GetProducts).Methods("GET")
router.HandleFunc("/api/products/{id}", controllers.GetProductById).Methods("GET")
router.HandleFunc("/api/products", controllers.CreateProduct).Methods("POST")
router.HandleFunc("/api/products/{id}", controllers.UpdateProduct).Methods("PUT")
router.HandleFunc("/api/products/{id}", controllers.DeleteProduct).Methods("DELETE")
}