Archived
1
0

add postgres vendored

This commit is contained in:
Jeff Becker
2017-09-09 10:20:33 -04:00
parent ea91db2f58
commit eed8c07ef6
49 changed files with 12387 additions and 9 deletions

View File

@@ -99,10 +99,13 @@ func Main() {
var db database.Database
for _, fconf := range conf.Frontends {
var f frontend.Frontend
f, err = frontend.NewHTTPFrontend(fconf, db)
f, err = frontend.NewHTTPFrontend(&fconf, db, nserv.Storage)
if err == nil {
log.Infof("serving frontend %s", f.Name())
go f.Serve()
frontends = append(frontends, f)
} else {
log.Fatalf("failed to set up frontend %s: %s", fconf.Name(), err.Error())
}
}
@@ -122,12 +125,14 @@ func Main() {
log.Infof("reloading config: %s", cfgFname)
nserv.ReloadServer(conf.NNTP)
nserv.ReloadFeeds(conf.Feeds)
nserv.ReloadStorage(conf.Store)
for idx := range frontends {
f := frontends[idx]
for i := range conf.Frontends {
c := conf.Frontends[i]
if c != nil && c.Name() == f.Name() {
f.Reload(c)
if c.Name() == f.Name() {
// TODO: inject storage config?
f.Reload(&c)
}
}
}

View File

@@ -24,7 +24,7 @@ type Config struct {
// list of feeds to add on runtime
Feeds []*FeedConfig `json:"feeds"`
// frontend config
Frontends []*FrontendConfig `json:"frontends"`
Frontends []FrontendConfig `json:"frontends"`
// unexported fields ...
// absolute filepath to configuration
@@ -39,7 +39,7 @@ var DefaultConfig = Config{
WebHooks: []*WebhookConfig{DefaultWebHookConfig},
NNTPHooks: []*NNTPHookConfig{DefaultNNTPHookConfig},
Feeds: DefaultFeeds,
Frontends: []*FrontendConfig{&DefaultFrontendConfig},
Frontends: []FrontendConfig{DefaultFrontendConfig},
Log: "debug",
}

View File

@@ -15,6 +15,8 @@ type FrontendConfig struct {
Static string `json:"static_dir"`
// http middleware configuration
Middleware *MiddlewareConfig `json:"middleware"`
// storage config
Storage *StoreConfig `json:"-"`
}
func (cfg *FrontendConfig) Name() string {

View File

@@ -11,7 +11,10 @@ import (
type Database interface {
ThreadByMessageID(msgid string) (*model.Thread, error)
ThreadByHash(hash string) (*model.Thread, error)
MessageIDByHash(hash string) (string, error)
BoardPage(newsgroup string, pageno, perpage int) (*model.BoardPage, error)
StorePost(post model.Post) error
Init() error
}
// get new database connector from configuration

View File

@@ -1,10 +1,13 @@
package database
import (
"database/sql"
_ "github.com/lib/pq"
"nntpchan/lib/model"
)
type PostgresDB struct {
conn *sql.DB
}
func (db *PostgresDB) ThreadByMessageID(msgid string) (thread *model.Thread, err error) {
@@ -13,6 +16,10 @@ func (db *PostgresDB) ThreadByMessageID(msgid string) (thread *model.Thread, err
}
func (db *PostgresDB) ThreadByHash(hash string) (thread *model.Thread, err error) {
return
}
func (db *PostgresDB) MessageIDByHash(hash string) (msgid string, err error) {
return
}
@@ -22,7 +29,30 @@ func (db *PostgresDB) BoardPage(newsgroup string, pageno, perpage int) (page *mo
return
}
func createPostgresDatabase(addr, user, passwd string) (p *PostgresDB, err error) {
func (db *PostgresDB) StorePost(post model.Post) (err error) {
return
}
func (db *PostgresDB) Init() (err error) {
return
}
func createPostgresDatabase(addr, user, passwd string) (p *PostgresDB, err error) {
p = new(PostgresDB)
var authstring string
if len(addr) > 0 {
authstring += " host=" + addr
}
if len(user) > 0 {
authstring += " username=" + user
}
if len(passwd) > 0 {
authstring += " password=" + passwd
}
p.conn, err = sql.Open("postgres", authstring)
if err != nil {
p = nil
}
return
}

View File

@@ -5,6 +5,7 @@ import (
"nntpchan/lib/database"
"nntpchan/lib/model"
"nntpchan/lib/nntp"
"nntpchan/lib/store"
)
// a frontend that displays nntp posts and allows posting
@@ -33,7 +34,7 @@ type Frontend interface {
}
// create a new http frontend give frontend config
func NewHTTPFrontend(c *config.FrontendConfig, db database.Database) (f Frontend, err error) {
func NewHTTPFrontend(c *config.FrontendConfig, db database.Database, s store.Storage) (f Frontend, err error) {
var mid Middleware
if c.Middleware != nil {
@@ -43,7 +44,7 @@ func NewHTTPFrontend(c *config.FrontendConfig, db database.Database) (f Frontend
if err == nil {
// create http frontend only if no previous errors
f, err = createHttpFrontend(c, mid, db)
f, err = createHttpFrontend(c, mid, db, s)
}
return
}

View File

@@ -11,6 +11,7 @@ import (
"nntpchan/lib/database"
"nntpchan/lib/model"
"nntpchan/lib/nntp"
"nntpchan/lib/store"
"time"
)
@@ -31,6 +32,8 @@ type httpFrontend struct {
apiserve *api.Server
// database driver
db database.Database
// article storage
storage store.Storage
}
func (f *httpFrontend) Name() string {
@@ -92,12 +95,16 @@ func (f *httpFrontend) SentArticleVia(msgid nntp.MessageID, feedname string) {
// TODO: implement
}
func createHttpFrontend(c *config.FrontendConfig, mid Middleware, db database.Database) (f *httpFrontend, err error) {
func createHttpFrontend(c *config.FrontendConfig, mid Middleware, db database.Database, s store.Storage) (f *httpFrontend, err error) {
f = new(httpFrontend)
// set db
// db.Ensure() called elsewhere
f.db = db
// set up storage
// s.Ensure() called elsewhere
f.storage = s
// set bind address
f.addr = c.BindAddr

View File

@@ -59,6 +59,11 @@ func (s *Server) ReloadFeeds(feeds []*config.FeedConfig) {
}
// reload storage configuration
func (s *Server) ReloadStorage(c *config.StoreConfig) {
}
func (s *Server) GotArticle(msgid MessageID, group Newsgroup) {
log.WithFields(log.Fields{
"pkg": "nntp-server",