Add packages: initializers & models, populate with ex. data

This commit is contained in:
mikgor 2018-10-25 18:06:54 +02:00
parent ecbe0ad93e
commit 198c34bc28
3 changed files with 72 additions and 21 deletions

View File

@ -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)
}

View File

@ -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()
}

36
models/models.go Normal file
View File

@ -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