From b9560f01e701390f64d0ab843969a5d895c13967 Mon Sep 17 00:00:00 2001 From: Gregory Ballantine Date: Fri, 25 Nov 2022 15:22:20 -0500 Subject: [PATCH] Separated route handlers into a separate module --- colossus.go | 21 ++------------------- routes/hardware.go | 16 ++++++++++++++++ routes/index.go | 18 ++++++++++++++++++ routes/init.go | 11 +++++++++++ 4 files changed, 47 insertions(+), 19 deletions(-) create mode 100644 routes/hardware.go create mode 100644 routes/index.go create mode 100644 routes/init.go diff --git a/colossus.go b/colossus.go index 33698b6..08167ee 100644 --- a/colossus.go +++ b/colossus.go @@ -1,12 +1,11 @@ package main import ( - "net/http" - "github.com/flamego/flamego" "github.com/flamego/template" "git.metaunix.net/BitGoblin/colossus/models" + "git.metaunix.net/BitGoblin/colossus/routes" ) func main() { @@ -29,23 +28,7 @@ func main() { })) // register route handlers - f.Get("/", func(t template.Template, data template.Data) { - var hardware []models.Component - models.DB.Find(&hardware) - data["hardware"] = hardware - - data["title"] = "Dashboard" - t.HTML(http.StatusOK, "index") - }) - - f.Get("/hardware/add", func(c flamego.Context) { - models.DB.Create(&models.Component{ - Name: "PowerColor RX 570 4GB", - Manufacturer: "PowerColor", - Type: "Graphics Card", - }) - c.Redirect("/") - }) + routes.InitRoutes(f) f.Run() } diff --git a/routes/hardware.go b/routes/hardware.go new file mode 100644 index 0000000..e7ecb9e --- /dev/null +++ b/routes/hardware.go @@ -0,0 +1,16 @@ +package routes + +import ( + "github.com/flamego/flamego" + + "git.metaunix.net/BitGoblin/colossus/models" +) + +func getHardwareAdd(c flamego.Context) { + models.DB.Create(&models.Component{ + Name: "PowerColor RX 570 4GB", + Manufacturer: "PowerColor", + Type: "Graphics Card", + }) + c.Redirect("/") +} diff --git a/routes/index.go b/routes/index.go new file mode 100644 index 0000000..c5a50c9 --- /dev/null +++ b/routes/index.go @@ -0,0 +1,18 @@ +package routes + +import ( + "net/http" + + "github.com/flamego/template" + + "git.metaunix.net/BitGoblin/colossus/models" +) + +func getIndex(t template.Template, data template.Data) { + var hardware []models.Component + models.DB.Find(&hardware) + data["hardware"] = hardware + + data["title"] = "Dashboard" + t.HTML(http.StatusOK, "index") +} diff --git a/routes/init.go b/routes/init.go new file mode 100644 index 0000000..6f7a9cc --- /dev/null +++ b/routes/init.go @@ -0,0 +1,11 @@ +package routes + +import ( + "github.com/flamego/flamego" +) + +func InitRoutes(f *flamego.Flame) { + f.Get("/", getIndex) + + f.Get("/hardware/add", getHardwareAdd) +}