Skip to content
This repository has been archived by the owner on Aug 11, 2023. It is now read-only.

Commit

Permalink
feat: connecting to db
Browse files Browse the repository at this point in the history
  • Loading branch information
qin-guan committed May 11, 2023
1 parent 625f88a commit fc6a9d9
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
.vscode
config.yaml
*.db
17 changes: 17 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package main

import (
"context"
"fmt"
"log"

"github.com/np-inprove/server/internal/config"
"github.com/np-inprove/server/internal/ent"
"github.com/np-inprove/server/internal/logger"

_ "github.com/mattn/go-sqlite3"
)

func main() {
Expand All @@ -25,4 +30,16 @@ func main() {
}

appLogger.Info("Hello, world!")

client, err := ent.Open(cfg.Database.DriverName, cfg.Database.DataSourceName)
if err != nil {
appLogger.Fatal(fmt.Sprintf("failed opening connection to sqlite: %v", err))
}
defer func(client *ent.Client) {
_ = client.Close()
}(client)

if err := client.Schema.Create(context.Background()); err != nil {
appLogger.Fatal(fmt.Sprintf("failed creating schema resources: %v", err))
}
}
6 changes: 2 additions & 4 deletions config/config.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ http:
port: 5000

database:
host:
name:
username:
password:
drivername: sqlite3
datasourcename: file:test.db?_fk=1&cache=shared&mode=memory

app:
production: false
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ require (
)

require (
github.com/mattn/go-sqlite3 v1.14.16
github.com/spf13/viper v1.15.0
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348 h1:MtvEpTB6LX3v
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y=
github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 h1:DpOJ2HYzCv8LZP15IdmG+YdwD2luVPHITV96TkirNBM=
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
Expand Down
10 changes: 4 additions & 6 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ type Config struct {
Port int
}
Database struct {
Host string
Name string
Username string
Password string
DriverName string
DataSourceName string
}
App struct {
Production bool
Expand Down Expand Up @@ -62,8 +60,8 @@ func NewTest() (*Config, error) {
return nil, err
}

if !strings.Contains(config.Database.Name, "test") {
return nil, fmt.Errorf("database name used for testing should contain 'test' substring: %s", config.Database.Name)
if !strings.Contains(config.Database.DataSourceName, "test") {
return nil, fmt.Errorf("database source name used for test cases should contain 'test' substring: %s", config.Database.DataSourceName)
}

return config, nil
Expand Down

0 comments on commit fc6a9d9

Please sign in to comment.