Separated route handlers into a separate module

This commit is contained in:
Gregory Ballantine 2022-11-25 15:22:20 -05:00
parent 64fc6fd8fc
commit b9560f01e7
4 changed files with 47 additions and 19 deletions

View File

@ -1,12 +1,11 @@
package main package main
import ( import (
"net/http"
"github.com/flamego/flamego" "github.com/flamego/flamego"
"github.com/flamego/template" "github.com/flamego/template"
"git.metaunix.net/BitGoblin/colossus/models" "git.metaunix.net/BitGoblin/colossus/models"
"git.metaunix.net/BitGoblin/colossus/routes"
) )
func main() { func main() {
@ -29,23 +28,7 @@ func main() {
})) }))
// register route handlers // register route handlers
f.Get("/", func(t template.Template, data template.Data) { routes.InitRoutes(f)
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("/")
})
f.Run() f.Run()
} }

16
routes/hardware.go Normal file
View File

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

18
routes/index.go Normal file
View File

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

11
routes/init.go Normal file
View File

@ -0,0 +1,11 @@
package routes
import (
"github.com/flamego/flamego"
)
func InitRoutes(f *flamego.Flame) {
f.Get("/", getIndex)
f.Get("/hardware/add", getHardwareAdd)
}