package database import ( "crypto/rsa" "crypto/x509" "encoding/pem" "git.xeserv.us/xena/route/routerpc" r "github.com/GoRethink/gorethink" ) type DB struct { s *r.Session } func New(host, database string) (*DB, error) { session, err := r.Connect(r.ConnectOpts{ Address: host, Database: database, }) if err != nil { return nil, err } db := &DB{ s: session, } return db, nil } var tables = []string{ "certs", "routes", } type Route struct { ID string `gorethink:"id,omitempty"` Hostname string `gorethink:"hostname"` OnionHostname string `gorethink:"onionhostname"` Token string `gorethink:"token"` OnionKey []byte `gorethink:"onionKey"` // PEM-encoded } func (db *DB) SaveRoute(resp *routerpc.AddHostResponse) error { pemblock := &pem.Block{ Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(resp.PrivKey.(*rsa.PrivateKey)), } bytes := pem.EncodeToMemory(pemblock) rt := &Route{ Hostname: resp.Hostname, OnionHostname: resp.OnionHostname, Token: resp.Token, OnionKey: bytes, } _, err := r.Table("routes").Insert(rt).RunWrite(db.s) if err != nil { return err } return nil } func (db *DB) GetAllRoutes() ([]Route, error) { rows, err := r.Table("routes").Run(db.s) if err != nil { return nil, err } var routes []Route err = rows.All(&routes) if err != nil { return nil, err } return routes, nil }