This commit is contained in:
+15
@@ -2,5 +2,20 @@
|
||||
<profile version="1.0">
|
||||
<option name="myName" value="Project Default" />
|
||||
<inspection_tool class="GoNilness" enabled="false" level="WARNING" enabled_by_default="false" />
|
||||
<inspection_tool class="GoUnhandledErrorResult" enabled="true" level="WARNING" enabled_by_default="true">
|
||||
<methods>
|
||||
<method importPath="hash" receiver="Hash" name="Write" />
|
||||
<method importPath="strings" receiver="*Builder" name="Write" />
|
||||
<method importPath="strings" receiver="*Builder" name="WriteByte" />
|
||||
<method importPath="bytes" receiver="*Buffer" name="WriteRune" />
|
||||
<method importPath="bytes" receiver="*Buffer" name="Write" />
|
||||
<method importPath="bytes" receiver="*Buffer" name="WriteString" />
|
||||
<method importPath="strings" receiver="*Builder" name="WriteString" />
|
||||
<method importPath="bytes" receiver="*Buffer" name="WriteByte" />
|
||||
<method importPath="strings" receiver="*Builder" name="WriteRune" />
|
||||
<method importPath="math/rand" receiver="*Rand" name="Read" />
|
||||
<method importPath="git.aiterp.net/rpdata/api/database" receiver="Database" name="Close" />
|
||||
</methods>
|
||||
</inspection_tool>
|
||||
</profile>
|
||||
</component>
|
||||
@@ -28,17 +28,17 @@ func main() {
|
||||
log.Fatalln("Failed to init store:", err)
|
||||
}
|
||||
|
||||
repos, stopFn, err := database.Init(config.Global().Database)
|
||||
db, err := database.Init(config.Global().Database)
|
||||
if err != nil {
|
||||
log.Fatalln("Failed to init db:", err)
|
||||
}
|
||||
defer stopFn()
|
||||
services := services.NewBundle(repos)
|
||||
defer db.Close(context.Background())
|
||||
serviceBundle := services.NewBundle(db)
|
||||
|
||||
instrumentation.Register()
|
||||
|
||||
http.Handle("/", handler.Playground("RPData API", "/graphql"))
|
||||
http.Handle("/graphql", queryHandler(services))
|
||||
http.Handle("/graphql", queryHandler(serviceBundle))
|
||||
|
||||
http.Handle("/metrics", promhttp.Handler())
|
||||
|
||||
@@ -51,7 +51,7 @@ func main() {
|
||||
log.Println("Characters updated")
|
||||
}()
|
||||
|
||||
go logListedChanges(services.Changes)
|
||||
go logListedChanges(serviceBundle.Changes)
|
||||
|
||||
log.Fatal(http.ListenAndServe(":8081", nil))
|
||||
}
|
||||
|
||||
+11
-2
@@ -1,6 +1,7 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"git.aiterp.net/rpdata/api/database/mongodb"
|
||||
@@ -11,12 +12,20 @@ import (
|
||||
// ErrDriverUnrecognized is returned if the driver is not recognized
|
||||
var ErrDriverUnrecognized = errors.New("Driver not recognized, check your config or update rpdata")
|
||||
|
||||
type Database interface {
|
||||
Changes() repositories.ChangeRepository
|
||||
Characters() repositories.CharacterRepository
|
||||
Tags() repositories.TagRepository
|
||||
|
||||
Close(ctx context.Context) error
|
||||
}
|
||||
|
||||
// Init sets up the database.
|
||||
func Init(config config.Database) (bundle *repositories.Bundle, closeFn func() error, err error) {
|
||||
func Init(config config.Database) (Database, error) {
|
||||
switch config.Driver {
|
||||
case "mongo", "mgo", "mongodb":
|
||||
return mongodb.Init(config)
|
||||
default:
|
||||
return nil, nil, ErrDriverUnrecognized
|
||||
return nil, ErrDriverUnrecognized
|
||||
}
|
||||
}
|
||||
|
||||
+36
-15
@@ -1,6 +1,7 @@
|
||||
package mongodb
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/globalsign/mgo/bson"
|
||||
"time"
|
||||
@@ -10,8 +11,33 @@ import (
|
||||
"github.com/globalsign/mgo"
|
||||
)
|
||||
|
||||
type MongoDB struct {
|
||||
session *mgo.Session
|
||||
|
||||
changes repositories.ChangeRepository
|
||||
characters repositories.CharacterRepository
|
||||
tags repositories.TagRepository
|
||||
}
|
||||
|
||||
func (m *MongoDB) Changes() repositories.ChangeRepository {
|
||||
return m.changes
|
||||
}
|
||||
|
||||
func (m *MongoDB) Characters() repositories.CharacterRepository {
|
||||
return m.characters
|
||||
}
|
||||
|
||||
func (m *MongoDB) Tags() repositories.TagRepository {
|
||||
return m.tags
|
||||
}
|
||||
|
||||
func (m *MongoDB) Close(ctx context.Context) error {
|
||||
m.session.Close()
|
||||
return nil
|
||||
}
|
||||
|
||||
// Init initializes the mongodb database
|
||||
func Init(cfg config.Database) (bundle *repositories.Bundle, closeFn func() error, err error) {
|
||||
func Init(cfg config.Database) (*MongoDB, error) {
|
||||
port := cfg.Port
|
||||
if port <= 0 {
|
||||
port = 27017
|
||||
@@ -27,7 +53,7 @@ func Init(cfg config.Database) (bundle *repositories.Bundle, closeFn func() erro
|
||||
Source: cfg.Db,
|
||||
})
|
||||
if err != nil {
|
||||
return
|
||||
return nil, err
|
||||
}
|
||||
|
||||
db := session.DB(cfg.Db)
|
||||
@@ -35,27 +61,22 @@ func Init(cfg config.Database) (bundle *repositories.Bundle, closeFn func() erro
|
||||
characters, err := newCharacterRepository(db)
|
||||
if err != nil {
|
||||
session.Close()
|
||||
return nil, nil, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
changes, err := newChangeRepository(db)
|
||||
if err != nil {
|
||||
session.Close()
|
||||
return nil, nil, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
bundle = &repositories.Bundle{
|
||||
Characters: characters,
|
||||
Changes: changes,
|
||||
Tags: newTagRepository(db),
|
||||
}
|
||||
return &MongoDB{
|
||||
session: session,
|
||||
|
||||
closeFn = func() error {
|
||||
session.Close()
|
||||
return nil
|
||||
}
|
||||
|
||||
return
|
||||
changes: changes,
|
||||
characters: characters,
|
||||
tags: newTagRepository(db),
|
||||
}, nil
|
||||
}
|
||||
|
||||
type counter struct {
|
||||
|
||||
@@ -2,12 +2,5 @@ package repositories
|
||||
|
||||
import "errors"
|
||||
|
||||
// A Bundle is a set of repositories.
|
||||
type Bundle struct {
|
||||
Characters CharacterRepository
|
||||
Changes ChangeRepository
|
||||
Tags TagRepository
|
||||
}
|
||||
|
||||
// ErrNotFound should be returned instead of any database-specific not found error.
|
||||
var ErrNotFound = errors.New("Resource not found")
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"git.aiterp.net/rpdata/api/repositories"
|
||||
"git.aiterp.net/rpdata/api/database"
|
||||
"git.aiterp.net/rpdata/api/services/loaders"
|
||||
)
|
||||
|
||||
@@ -13,16 +13,16 @@ type Bundle struct {
|
||||
}
|
||||
|
||||
// NewBundle creates a new bundle.
|
||||
func NewBundle(repos *repositories.Bundle) *Bundle {
|
||||
func NewBundle(db database.Database) *Bundle {
|
||||
bundle := &Bundle{}
|
||||
|
||||
bundle.Changes = &ChangeService{
|
||||
changes: repos.Changes,
|
||||
changes: db.Changes(),
|
||||
}
|
||||
bundle.Tags = &TagService{tags: repos.Tags}
|
||||
bundle.Tags = &TagService{tags: db.Tags()}
|
||||
bundle.Characters = &CharacterService{
|
||||
characters: repos.Characters,
|
||||
loader: loaders.CharacterLoaderFromRepository(repos.Characters),
|
||||
characters: db.Characters(),
|
||||
loader: loaders.CharacterLoaderFromRepository(db.Characters()),
|
||||
changeService: bundle.Changes,
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user