2017-05-05 15:08:02 +02:00
package config
2017-05-05 14:10:35 +02:00
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"io"
"os"
)
2017-05-08 23:40:54 +02:00
const (
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
// LastOldTorrentID is the highest torrent ID
// that was copied from the original Nyaa
LastOldTorrentID = 923000
2017-05-15 08:24:06 +02:00
TableName = "sukebei_torrents"
2017-05-08 23:40:54 +02:00
)
2017-05-05 14:10:35 +02:00
type Config struct {
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
Host string ` json:"host" `
Port int ` json:"port" `
DBType string ` json:"db_type" `
// DBParams will be directly passed to Gorm, and its internal
2017-05-05 14:10:35 +02:00
// structure depends on the dialect for each db type
2017-05-13 22:52:10 +02:00
DBParams string ` json:"db_params" `
2017-05-11 08:01:12 +02:00
DBLogMode string ` json:"db_logmode" `
2017-05-10 19:29:35 +02:00
// tracker scraper config (required)
Scrape ScraperConfig ` json:"scraper" `
2017-05-11 15:01:53 +02:00
// cache config
Cache CacheConfig ` json:"cache" `
2017-05-11 15:24:20 +02:00
// search config
Search SearchConfig ` json:"search" `
2017-05-07 21:51:37 +02:00
// optional i2p configuration
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
I2P * I2PConfig ` json:"i2p" `
2017-05-13 19:58:48 +02:00
// filesize fetcher config
2017-05-14 13:20:34 +02:00
MetainfoFetcher MetainfoFetcherConfig ` json:"metainfo_fetcher" `
2017-05-14 21:45:50 +02:00
// internationalization config
2017-05-14 00:55:17 +02:00
I18n I18nConfig ` json:"i18n" `
2017-05-05 14:10:35 +02:00
}
2017-05-14 13:20:34 +02:00
var Defaults = Config { "localhost" , 9999 , "sqlite3" , "./nyaa.db?cache_size=50" , "default" , DefaultScraperConfig , DefaultCacheConfig , DefaultSearchConfig , nil , DefaultMetainfoFetcherConfig , DefaultI18nConfig }
2017-05-14 00:55:17 +02:00
2017-05-05 14:10:35 +02:00
var allowedDatabaseTypes = map [ string ] bool {
"sqlite3" : true ,
"postgres" : true ,
"mysql" : true ,
"mssql" : true ,
}
2017-05-11 08:01:12 +02:00
var allowedDBLogModes = map [ string ] bool {
"default" : true , // errors only
"detailed" : true ,
"silent" : true ,
}
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
func New ( ) * Config {
2017-05-05 14:10:35 +02:00
var config Config
config . Host = Defaults . Host
config . Port = Defaults . Port
config . DBType = Defaults . DBType
config . DBParams = Defaults . DBParams
2017-05-11 08:01:12 +02:00
config . DBLogMode = Defaults . DBLogMode
2017-05-10 19:29:35 +02:00
config . Scrape = Defaults . Scrape
2017-05-11 15:01:53 +02:00
config . Cache = Defaults . Cache
2017-05-14 13:20:34 +02:00
config . MetainfoFetcher = Defaults . MetainfoFetcher
2017-05-13 22:52:10 +02:00
config . I18n = Defaults . I18n
2017-05-05 14:10:35 +02:00
return & config
}
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
// BindFlags returns a function which is to be used after
// flag.Parse to check and copy the flags' values to the Config instance.
func ( config * Config ) BindFlags ( ) func ( ) error {
confFile := flag . String ( "conf" , "" , "path to the configuration file" )
dbType := flag . String ( "dbtype" , Defaults . DBType , "database backend" )
2017-05-05 14:10:35 +02:00
host := flag . String ( "host" , Defaults . Host , "binding address of the server" )
port := flag . Int ( "port" , Defaults . Port , "port of the server" )
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
dbParams := flag . String ( "dbparams" , Defaults . DBParams , "parameters to open the database (see Gorm's doc)" )
2017-05-11 08:01:12 +02:00
dbLogMode := flag . String ( "dblogmode" , Defaults . DBLogMode , "database log verbosity (errors only by default)" )
2017-05-05 14:10:35 +02:00
return func ( ) error {
// You can override fields in the config file with flags.
config . Host = * host
config . Port = * port
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
config . DBParams = * dbParams
err := config . SetDBType ( * dbType )
2017-05-06 20:43:15 +02:00
if err != nil {
return err
}
2017-05-11 08:01:12 +02:00
err = config . SetDBLogMode ( * dbLogMode )
if err != nil {
return err
}
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
err = config . HandleConfFileFlag ( * confFile )
2017-05-05 14:10:35 +02:00
return err
}
}
2017-05-05 15:08:02 +02:00
func ( config * Config ) HandleConfFileFlag ( path string ) error {
2017-05-05 14:10:35 +02:00
if path != "" {
file , err := os . Open ( path )
if err != nil {
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
return fmt . Errorf ( "can't read file '%s'" , path )
2017-05-05 14:10:35 +02:00
}
err = config . Read ( bufio . NewReader ( file ) )
if err != nil {
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
return fmt . Errorf ( "failed to parse file '%s' (%s)" , path , err )
2017-05-05 14:10:35 +02:00
}
}
return nil
}
2017-05-05 15:08:02 +02:00
func ( config * Config ) SetDBType ( db_type string ) error {
2017-05-05 14:10:35 +02:00
if ! allowedDatabaseTypes [ db_type ] {
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
return fmt . Errorf ( "unknown database backend '%s'" , db_type )
2017-05-05 14:10:35 +02:00
}
config . DBType = db_type
return nil
}
2017-05-11 08:01:12 +02:00
func ( config * Config ) SetDBLogMode ( db_logmode string ) error {
if ! allowedDBLogModes [ db_logmode ] {
return fmt . Errorf ( "unknown database log mode '%s'" , db_logmode )
}
config . DBLogMode = db_logmode
return nil
}
2017-05-05 14:10:35 +02:00
func ( config * Config ) Read ( input io . Reader ) error {
return json . NewDecoder ( input ) . Decode ( config )
}
func ( config * Config ) Write ( output io . Writer ) error {
return json . NewEncoder ( output ) . Encode ( config )
}
func ( config * Config ) Pretty ( output io . Writer ) error {
data , err := json . MarshalIndent ( config , "" , "\t" )
if err != nil {
return err
}
2017-05-05 14:54:36 +02:00
data = append ( data , [ ] byte ( "\n" ) ... )
2017-05-05 14:10:35 +02:00
_ , err = output . Write ( data )
return err
2017-05-07 21:51:37 +02:00
}