From 198c34bc2830d2937ba668a3d74c98ea22be9dc2 Mon Sep 17 00:00:00 2001 From: mikgor Date: Thu, 25 Oct 2018 18:06:54 +0200 Subject: [PATCH] Add packages: initializers & models, populate with ex. data --- initializers/initializers.go | 34 ++++++++++++++++++++++++++++++++++ market.go | 23 ++--------------------- models/models.go | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 21 deletions(-) create mode 100644 initializers/initializers.go create mode 100644 models/models.go diff --git a/initializers/initializers.go b/initializers/initializers.go new file mode 100644 index 0000000..a2d4521 --- /dev/null +++ b/initializers/initializers.go @@ -0,0 +1,34 @@ +package initializers + +import ( + "net/http" + . "Elektromarket/models" +) + +func initializePages() { + Pages = make(map[string]Page) + Pages["index"] = Page{Path: "/", Template: "templates/index.html", Data: map[string]interface{}{"test": '1'}} +} + +func initializeData() { + Categories = []Category { + {1, "Laptopy"}, + {2, "Komputery"}, + {3, "Smartfony"}, + {4, "Smartwache"}, + {5, "Monitory"}, + {6, "Drukarki"}, + {7, "Myszki"}, + {8, "Klawiatury"}, + {9, "Akcesoria"}, + } +} + +func Initialize() { + initializePages() + initializeData() + for k := range Pages { + http.HandleFunc(Pages[k].Path, Pages[k].HandlePage) + } + http.ListenAndServe(":8000", nil) +} diff --git a/market.go b/market.go index 0b2576e..095f06d 100644 --- a/market.go +++ b/market.go @@ -1,26 +1,7 @@ package main -import ( - "net/http" - "html/template" -) - -type Page struct { - Path string - Template string - Data map[string]interface{} -} - -func (p Page) HandlePage(w http.ResponseWriter, r *http.Request) { - t, _ := template.ParseFiles(p.Template) - t.Execute(w, p.Data) -} +import . "Elektromarket/initializers" func main() { - var pages map[string]Page = make(map[string]Page) - pages["index"] = Page{Path: "/", Template: "templates/index.html", Data: map[string]interface{}{"test": "1"}} - for k := range pages { - http.HandleFunc(pages[k].Path, pages[k].HandlePage) - } - http.ListenAndServe(":8000", nil) + Initialize() } diff --git a/models/models.go b/models/models.go new file mode 100644 index 0000000..e674199 --- /dev/null +++ b/models/models.go @@ -0,0 +1,36 @@ +package models + +import ( + "net/http" + "html/template" +) + +type Page struct { + Path string + Template string + Data map[string]interface{} +} + +func (p Page) HandlePage(w http.ResponseWriter, r *http.Request) { + t, _ := template.ParseFiles(p.Template) + t.Execute(w, p.Data) +} + +type Category struct { + Id uint + Name string +} + +type Product struct { + Id uint + Category Category + Name string + Description string + ImgUrl string + Quantity uint + Price uint +} + +var Pages map[string]Page +var Categories []Category +var Products []Product