diff options
| author | Christian Krinitsin <mail@krinitsin.com> | 2025-04-20 23:22:47 +0200 |
|---|---|---|
| committer | Christian Krinitsin <mail@krinitsin.com> | 2025-04-20 23:22:47 +0200 |
| commit | f9ab9bdd2bf1c40545ec67a20933289cc77c74cc (patch) | |
| tree | e8ce8fa4ab0bfd8d88b1a21560340da83f4a1231 | |
| parent | 54f11ff6ce482fec53ab4a9e5019651e67597b52 (diff) | |
| download | shopping-list-f9ab9bdd2bf1c40545ec67a20933289cc77c74cc.tar.gz shopping-list-f9ab9bdd2bf1c40545ec67a20933289cc77c74cc.zip | |
creates connection to database
| -rw-r--r-- | main.go | 3 | ||||
| -rw-r--r-- | models/database.go | 30 |
2 files changed, 33 insertions, 0 deletions
diff --git a/main.go b/main.go index a08c858..a2372f3 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,7 @@ import ( "net/http" "github.com/ckrinitsin/go-backend/handlers" + "github.com/ckrinitsin/go-backend/models" "github.com/gin-gonic/gin" ) @@ -15,6 +16,8 @@ var templatesFS embed.FS func main() { r := gin.Default() + models.ConnectDatabase() + tmpl := template.Must(template.ParseFS(templatesFS, "templates/*")) r.SetHTMLTemplate(tmpl) diff --git a/models/database.go b/models/database.go new file mode 100644 index 0000000..12d4253 --- /dev/null +++ b/models/database.go @@ -0,0 +1,30 @@ +package models + +import ( + "time" + + "gorm.io/driver/sqlite" + "gorm.io/gorm" +) + +type Entry struct { + ID uint + Text string + Checked bool + CreatedAt time.Time + UpdatedAt time.Time +} + +var DB *gorm.DB + +func ConnectDatabase() { + db, err := gorm.Open(sqlite.Open("shopping_list.db"), &gorm.Config{}) + + if err != nil { + panic("Failed to connect to database!") + } + + db.AutoMigrate(&Entry{}) + + DB = db +} |