-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
37 lines (28 loc) · 1.05 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
package main
import (
"log"
"net/http"
"firstapi/api"
"firstapi/db"
"github.com/gorilla/mux"
_ "github.com/jinzhu/gorm/dialects/sqlite"
)
type Product struct {
Id uint `json:"id" gorm:"primary_key,AUTO_INCREMENT"`
Name *string `json:"name"`
Price *uint `json:"price"`
}
func main() {
database := db.InitDB()
defer database.Close()
router := mux.NewRouter()
apiRouter := router.PathPrefix("/api").Subrouter()
productsController := api.NewProductsController(database)
apiRouter.HandleFunc("/products", productsController.GetProductList).Methods("GET")
apiRouter.HandleFunc("/products", productsController.CreateProduct).Methods("POST")
apiRouter.HandleFunc("/products/{id}", productsController.GetProduct).Methods("GET")
apiRouter.HandleFunc("/products/{id}", productsController.UpdateProduct).Methods("PUT")
apiRouter.HandleFunc("/products/{id}", productsController.DeleteProduct).Methods("DELETE")
log.Fatal(http.ListenAndServe(":8080", router))
// review pluralsight chapter for channels by nigel
}