Added GORM to handle database
This commit is contained in:
parent
7c55247af0
commit
64fc6fd8fc
4
.gitignore
vendored
4
.gitignore
vendored
@ -33,3 +33,7 @@ public/js/
|
||||
|
||||
# NPM dependencies for Grunt
|
||||
node_modules/
|
||||
|
||||
# Local data
|
||||
data/
|
||||
|
||||
|
20
colossus.go
20
colossus.go
@ -1,14 +1,19 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/flamego/flamego"
|
||||
"github.com/flamego/template"
|
||||
|
||||
"git.metaunix.net/BitGoblin/colossus/models"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// initialize database connection
|
||||
models.InitDB()
|
||||
|
||||
// start initialize Flamego router
|
||||
f := flamego.Classic()
|
||||
|
||||
// serve static files from ./public/
|
||||
@ -25,11 +30,22 @@ 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")
|
||||
})
|
||||
|
||||
fmt.Println("test!!?")
|
||||
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()
|
||||
}
|
||||
|
0
data/.gitkeep
Normal file
0
data/.gitkeep
Normal file
5
go.mod
5
go.mod
@ -7,8 +7,13 @@ require (
|
||||
github.com/fatih/color v1.13.0 // indirect
|
||||
github.com/flamego/flamego v1.7.0 // indirect
|
||||
github.com/flamego/template v1.1.0 // indirect
|
||||
github.com/jinzhu/inflection v1.0.0 // indirect
|
||||
github.com/jinzhu/now v1.1.5 // indirect
|
||||
github.com/mattn/go-colorable v0.1.9 // indirect
|
||||
github.com/mattn/go-isatty v0.0.14 // indirect
|
||||
github.com/mattn/go-sqlite3 v1.14.15 // indirect
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
|
||||
gorm.io/driver/sqlite v1.4.3 // indirect
|
||||
gorm.io/gorm v1.24.2 // indirect
|
||||
)
|
||||
|
13
go.sum
13
go.sum
@ -6,14 +6,27 @@ github.com/flamego/flamego v1.7.0 h1:c1Lu16PBAZKkpsjHw42vwotdoQnMMpUi60ITP41W12w
|
||||
github.com/flamego/flamego v1.7.0/go.mod h1:dnVMBJyHKaxjcqRVN93taSK+YB/9p+Op1GdLIuA1hFQ=
|
||||
github.com/flamego/template v1.1.0 h1:iYtCzY3TeYpsoQiGApFXw2qycKdMzimz2gkO/SlcksM=
|
||||
github.com/flamego/template v1.1.0/go.mod h1:bgnmEXNumarhQIUzFgn18CDG6u8cM6X09c7UOTwZcxM=
|
||||
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
|
||||
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
|
||||
github.com/jinzhu/now v1.1.4 h1:tHnRBy1i5F2Dh8BAFxqFzxKqqvezXrL2OW1TnX+Mlas=
|
||||
github.com/jinzhu/now v1.1.4/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
|
||||
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
|
||||
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
|
||||
github.com/mattn/go-colorable v0.1.9 h1:sqDoxXbdeALODt0DAeJCVp38ps9ZogZEAXjus69YV3U=
|
||||
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
|
||||
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
|
||||
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
|
||||
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
|
||||
github.com/mattn/go-sqlite3 v1.14.15 h1:vfoHhTN1af61xCRSWzFIWzx2YskyMTwHLrExkBOjvxI=
|
||||
github.com/mattn/go-sqlite3 v1.14.15/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
|
||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I=
|
||||
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
gorm.io/driver/sqlite v1.4.3 h1:HBBcZSDnWi5BW3B3rwvVTc510KGkBkexlOg0QrmLUuU=
|
||||
gorm.io/driver/sqlite v1.4.3/go.mod h1:0Aq3iPO+v9ZKbcdiz8gLWRw5VOPcBOPUQJFLq5e2ecI=
|
||||
gorm.io/gorm v1.24.0/go.mod h1:DVrVomtaYTbqs7gB/x2uVvqnXzv0nqjB396B8cG4dBA=
|
||||
gorm.io/gorm v1.24.2 h1:9wR6CFD+G8nOusLdvkZelOEhpJVwwHzpQOUM+REd6U0=
|
||||
gorm.io/gorm v1.24.2/go.mod h1:DVrVomtaYTbqs7gB/x2uVvqnXzv0nqjB396B8cG4dBA=
|
||||
|
12
models/component.go
Normal file
12
models/component.go
Normal file
@ -0,0 +1,12 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Component struct {
|
||||
gorm.Model
|
||||
Name string
|
||||
Manufacturer string
|
||||
Type string
|
||||
}
|
17
models/init.go
Normal file
17
models/init.go
Normal file
@ -0,0 +1,17 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/driver/sqlite"
|
||||
)
|
||||
|
||||
var (
|
||||
DB *gorm.DB
|
||||
)
|
||||
|
||||
func InitDB() {
|
||||
DB, _ = gorm.Open(sqlite.Open("data/colossus.db"), &gorm.Config{})
|
||||
|
||||
// Migrate the schema
|
||||
DB.AutoMigrate(&Component{})
|
||||
}
|
@ -14,6 +14,11 @@
|
||||
</div>
|
||||
<div class="four columns">
|
||||
<p><a href="/hardware/add">Add new hardware</a></p>
|
||||
<ul>
|
||||
{{ range $i, $comp := .hardware }}
|
||||
<li>{{ $comp.Name }}</li>
|
||||
{{ end }}
|
||||
</ul>
|
||||
</div>
|
||||
<div class="four columns">
|
||||
<p><a href="/game/add">Add new game</a></p>
|
||||
|
Loading…
Reference in New Issue
Block a user