diff --git a/config/config.go b/config/config.go index 8cf094b..35dbde1 100644 --- a/config/config.go +++ b/config/config.go @@ -21,6 +21,7 @@ import ( "reflect" "regexp" "strconv" + "strings" argparse "github.com/alexflint/go-arg" log "github.com/sirupsen/logrus" @@ -41,6 +42,8 @@ type AmCLI struct { ConfigFile string `arg:"-C,--config,env:AMSTERDAM_CONFIG" help:"Location of the configuration file."` Debug bool `arg:"-D,--debug,env:AMSTERDAM_DEBUG" help:"Force Amsterdam to run in debug mode."` Production bool `arg:"-P,--prod,--production,env:AMSTERDAM_PROD" help:"Force Amsterdam to run in production mode."` + DatabaseURL string `arg:"-d,--database,env:AMSTERDAM_DATABASE_URL" help:"Database URL for Amsterdam to connect to."` + Listen string `arg:"-l,--listen,env:AMSTERDAM_LISTEN" help:"Specifies the local address and port for Amsterdam to listen on."` DebugPanic bool `arg:"--debug-panic" help:"Development Only - disable Echo panic recovery"` BuggyAttachments bool `arg:"--buggy-attachments" help:"Some attachments may be buggy - truncate data if necessary"` } @@ -62,6 +65,7 @@ func (*AmCLI) Version() string { type AmConfig struct { Site struct { Production bool `yaml:"production"` + Listen string `yaml:"listen"` BaseURL string `yaml:"baseURL"` Title string `yaml:"title"` SiteIcon struct { @@ -152,6 +156,9 @@ type AmConfig struct { // AmConfigComputed is the configuration values which are "computed" based only on values in AmConfig. type AmConfigComputed struct { DebugMode bool // are we in debug mode? + Listen string // listen address + DatabaseDriver string // name of database driver + DatabaseDSN string // DSN for the database UploadMaxSize int32 // maximum upload size in bytes UploadNoCompress map[string]bool // which upload types are not compressed? } @@ -332,6 +339,22 @@ func SetupConfig() { } else { GlobalComputedConfig.DebugMode = !GlobalConfig.Site.Production } + if CommandLine.Listen != "" { + GlobalComputedConfig.Listen = CommandLine.Listen + } else { + GlobalComputedConfig.Listen = GlobalConfig.Site.Listen + } + if CommandLine.DatabaseURL != "" { + p := strings.Index(CommandLine.DatabaseURL, ":") + if p < 0 { + panic("Invalid database URL on command line") + } + GlobalComputedConfig.DatabaseDriver = CommandLine.DatabaseURL[:p] + GlobalComputedConfig.DatabaseDSN = CommandLine.DatabaseURL[p+1:] + } else { + GlobalComputedConfig.DatabaseDriver = GlobalConfig.Database.Driver + GlobalComputedConfig.DatabaseDSN = GlobalConfig.Database.Dsn + } tmp, err := parseDataSize(GlobalConfig.Posting.Uploads.MaxSize) if err != nil { panic(err.Error()) diff --git a/config/default.yaml b/config/default.yaml index bef3d74..330980f 100644 --- a/config/default.yaml +++ b/config/default.yaml @@ -8,6 +8,7 @@ # site: production: false + listen: ":1323" baseURL: "http://localhost:1323" title: "Amsterdam Web Communities System" siteIcon: diff --git a/database/base.go b/database/base.go index 170b438..60bd341 100644 --- a/database/base.go +++ b/database/base.go @@ -25,7 +25,7 @@ var amdb *sqlx.DB // SetupDb sets up the database and associated items. func SetupDb() (func(), error) { exitfns := make([]func(), 0, 2) - db, err := sqlx.Open(config.GlobalConfig.Database.Driver, config.GlobalConfig.Database.Dsn) + db, err := sqlx.Connect(config.GlobalComputedConfig.DatabaseDriver, config.GlobalComputedConfig.DatabaseDSN) if err == nil { amdb = db setupAdCache() diff --git a/database/post.go b/database/post.go index d955d42..cbc9684 100644 --- a/database/post.go +++ b/database/post.go @@ -21,6 +21,7 @@ import ( "time" "git.erbosoft.com/amy/amsterdam/config" + "github.com/jmoiron/sqlx" log "github.com/sirupsen/logrus" ) @@ -653,15 +654,15 @@ func AmGetPublishedPosts(ctx context.Context) ([]*PostHeader, error) { } // Use the post IDs to build a SQL statement. - pidStrs := make([]string, len(pids)) - for i, pid := range pids { - pidStrs[i] = fmt.Sprintf("%d", pid) + query, args, err := sqlx.In("SELECT * FROM posts WHERE postid IN (?)", pids) + if err != nil { + return nil, err } - sql := fmt.Sprintf("SELECT * FROM posts WHERE postid IN (%s)", strings.Join(pidStrs, ", ")) + query = amdb.Rebind(query) // Use the SQL to read in all the post headers using a single database query. var data []PostHeader - if err = amdb.SelectContext(ctx, &data, sql); err != nil { + if err = amdb.SelectContext(ctx, &data, query, args...); err != nil { return nil, err } if len(data) < len(pids) { diff --git a/main.go b/main.go index 9f9353e..96a92d9 100644 --- a/main.go +++ b/main.go @@ -255,7 +255,7 @@ func main() { // Start server go func() { - if err := e.Start(":1323"); err != nil && err != http.ErrServerClosed { + if err := e.Start(config.GlobalComputedConfig.Listen); err != nil && err != http.ErrServerClosed { e.Logger.Fatalf("shutting down the server: %v", err) } }()