database: add documentation, reference new utils functions
This commit is contained in:
parent
f7df55f044
commit
244ebfda04
|
@ -2,17 +2,18 @@ package database
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/rsa"
|
"crypto/rsa"
|
||||||
"crypto/x509"
|
|
||||||
"encoding/pem"
|
|
||||||
|
|
||||||
"git.xeserv.us/xena/route/routerpc"
|
"git.xeserv.us/xena/route/routerpc"
|
||||||
|
"git.xeserv.us/xena/route/utils"
|
||||||
r "github.com/GoRethink/gorethink"
|
r "github.com/GoRethink/gorethink"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// DB is the high level wrapper to the datastore.
|
||||||
type DB struct {
|
type DB struct {
|
||||||
s *r.Session
|
s *r.Session
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// New takes a rethinkdb host and databasea and sets up a connection.
|
||||||
func New(host, database string) (*DB, error) {
|
func New(host, database string) (*DB, error) {
|
||||||
session, err := r.Connect(r.ConnectOpts{
|
session, err := r.Connect(r.ConnectOpts{
|
||||||
Address: host,
|
Address: host,
|
||||||
|
@ -34,6 +35,7 @@ var tables = []string{
|
||||||
"routes",
|
"routes",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Route is a single route object serialized to rethinkdb.
|
||||||
type Route struct {
|
type Route struct {
|
||||||
ID string `gorethink:"id,omitempty"`
|
ID string `gorethink:"id,omitempty"`
|
||||||
Hostname string `gorethink:"hostname"`
|
Hostname string `gorethink:"hostname"`
|
||||||
|
@ -42,12 +44,9 @@ type Route struct {
|
||||||
OnionKey []byte `gorethink:"onionKey"` // PEM-encoded
|
OnionKey []byte `gorethink:"onionKey"` // PEM-encoded
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SaveRoute adds the route to the database.
|
||||||
func (db *DB) SaveRoute(resp *routerpc.AddHostResponse) error {
|
func (db *DB) SaveRoute(resp *routerpc.AddHostResponse) error {
|
||||||
pemblock := &pem.Block{
|
bytes := utils.RSAPrivateKeyToPem(resp.PrivKey.(*rsa.PrivateKey))
|
||||||
Type: "RSA PRIVATE KEY",
|
|
||||||
Bytes: x509.MarshalPKCS1PrivateKey(resp.PrivKey.(*rsa.PrivateKey)),
|
|
||||||
}
|
|
||||||
bytes := pem.EncodeToMemory(pemblock)
|
|
||||||
|
|
||||||
rt := &Route{
|
rt := &Route{
|
||||||
Hostname: resp.Hostname,
|
Hostname: resp.Hostname,
|
||||||
|
@ -56,6 +55,9 @@ func (db *DB) SaveRoute(resp *routerpc.AddHostResponse) error {
|
||||||
OnionKey: bytes,
|
OnionKey: bytes,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: check if OnionHostname or Hostname actually exists in
|
||||||
|
// the database. RethinkDB doesn't support unique secondary indexes.
|
||||||
|
|
||||||
_, err := r.Table("routes").Insert(rt).RunWrite(db.s)
|
_, err := r.Table("routes").Insert(rt).RunWrite(db.s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -64,6 +66,9 @@ func (db *DB) SaveRoute(resp *routerpc.AddHostResponse) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetAllRoutes fetches all of the route entries from the database
|
||||||
|
// and returns them. This is intended for the startup process or
|
||||||
|
// admin tooling.
|
||||||
func (db *DB) GetAllRoutes() ([]Route, error) {
|
func (db *DB) GetAllRoutes() ([]Route, error) {
|
||||||
rows, err := r.Table("routes").Run(db.s)
|
rows, err := r.Table("routes").Run(db.s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in New Issue