From 951363bc0446b84d78e1e4644e88a521f6143fed Mon Sep 17 00:00:00 2001 From: Roland Osborne Date: Tue, 11 Jan 2022 10:28:00 -0800 Subject: [PATCH] preparing schema through automigration --- net/schema/cmd | 1 + net/server/go.mod | 9 ++++++++- net/server/go.sum | 19 +++++++++++++++++++ net/server/internal/routers.go | 4 ++-- net/server/main.go | 9 ++++++++- 5 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 net/schema/cmd diff --git a/net/schema/cmd b/net/schema/cmd new file mode 100644 index 00000000..09d07740 --- /dev/null +++ b/net/schema/cmd @@ -0,0 +1 @@ +sqlite3 databag.db "VACUUM;" diff --git a/net/server/go.mod b/net/server/go.mod index 4e40b733..a7c85967 100644 --- a/net/server/go.mod +++ b/net/server/go.mod @@ -2,4 +2,11 @@ module databag go 1.17 -require github.com/gorilla/mux v1.8.0 // indirect +require ( + github.com/gorilla/mux v1.8.0 // indirect + github.com/jinzhu/inflection v1.0.0 // indirect + github.com/jinzhu/now v1.1.3 // indirect + github.com/mattn/go-sqlite3 v1.14.9 // indirect + gorm.io/driver/sqlite v1.2.6 // indirect + gorm.io/gorm v1.22.4 // indirect +) diff --git a/net/server/go.sum b/net/server/go.sum index 53502880..3125734d 100644 --- a/net/server/go.sum +++ b/net/server/go.sum @@ -1,2 +1,21 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= +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.2/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +github.com/jinzhu/now v1.1.3 h1:PlHq1bSCSZL9K0wUhbm2pGLoTWs2GwVhsP6emvGV/ZI= +github.com/jinzhu/now v1.1.3/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +github.com/mattn/go-sqlite3 v1.14.9 h1:10HX2Td0ocZpYEjhilsuo6WWtUqttj2Kb0KtD86/KYA= +github.com/mattn/go-sqlite3 v1.14.9/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gorm.io/driver/sqlite v1.2.6 h1:SStaH/b+280M7C8vXeZLz/zo9cLQmIGwwj3cSj7p6l4= +gorm.io/driver/sqlite v1.2.6/go.mod h1:gyoX0vHiiwi0g49tv+x2E7l8ksauLK0U/gShcdUsjWY= +gorm.io/gorm v1.22.3/go.mod h1:F+OptMscr0P2F2qU97WT1WimdH9GaQPoDW7AYd5i2Y0= +gorm.io/gorm v1.22.4 h1:8aPcyEJhY0MAt8aY6Dc524Pn+pO29K+ydu+e/cXSpQM= +gorm.io/gorm v1.22.4/go.mod h1:1aeVC+pe9ZmvKZban/gW4QPra7PRoTEssyc922qCAkk= diff --git a/net/server/internal/routers.go b/net/server/internal/routers.go index 2ea7ae26..e3bacb0d 100644 --- a/net/server/internal/routers.go +++ b/net/server/internal/routers.go @@ -13,7 +13,7 @@ import ( "fmt" "net/http" "strings" - + "gorm.io/gorm" "github.com/gorilla/mux" ) @@ -26,7 +26,7 @@ type Route struct { type Routes []Route -func NewRouter() *mux.Router { +func NewRouter(db *gorm.DB) *mux.Router { router := mux.NewRouter().StrictSlash(true) for _, route := range routes { var handler http.Handler diff --git a/net/server/main.go b/net/server/main.go index 4a81bd9e..8c18653f 100644 --- a/net/server/main.go +++ b/net/server/main.go @@ -12,13 +12,20 @@ package main import ( "log" "net/http" + "gorm.io/gorm" + "gorm.io/driver/sqlite" app "databag/internal" ) func main() { + + db, err := gorm.Open(sqlite.Open("databag.db"), &gorm.Config{}) + if err != nil { + panic("failed to connect database") + } log.Printf("Server started") - router := app.NewRouter() + router := app.NewRouter(db) log.Fatal(http.ListenAndServe(":7000", router)) }