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