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
2017-05-26 12:12:52 +02:00
LastOldTorrentID = 923000
// TorrentsTableName : Name of torrent table in DB
TorrentsTableName = "torrents"
// ReportsTableName : Name of torrent report table in DB
ReportsTableName = "torrent_reports"
// CommentsTableName : Name of comments table in DB
CommentsTableName = "comments"
// UploadsOldTableName : Name of uploads table in DB
UploadsOldTableName = "user_uploads_old"
// FilesTableName : Name of files table in DB
FilesTableName = "files"
// NotificationTableName : Name of notifications table in DB
2017-05-20 20:53:05 +02:00
NotificationTableName = "notifications"
2017-05-20 12:45:27 +02:00
// for sukebei:
//LastOldTorrentID = 2303945
//TorrentsTableName = "sukebei_torrents"
2017-05-20 16:26:22 +02:00
//ReportsTableName = "sukebei_torrent_reports"
2017-05-20 12:45:27 +02:00
//CommentsTableName = "sukebei_comments"
//UploadsOldTableName = "sukebei_user_uploads_old"
2017-05-20 16:26:22 +02:00
//FilesTableName = "sukebei_files"
2017-05-08 23:40:54 +02:00
)
2017-05-26 12:12:52 +02:00
// IsSukebei : Tells if we are on the sukebei website
2017-05-20 12:45:27 +02:00
func IsSukebei ( ) bool {
return TorrentsTableName == "sukebei_torrents"
}
2017-05-26 12:12:52 +02:00
// Config : Configuration for DB, I2P, Fetcher, Go Server and Translation
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-26 12:12:52 +02:00
// Defaults : Configuration by default
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 ,
}
2017-05-26 12:12:52 +02:00
// New : Construct a new config variable
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-26 12:12:52 +02:00
// HandleConfFileFlag : Read the config from a file
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-26 12:12:52 +02:00
// SetDBType : Set the DataBase type in config
func ( config * Config ) SetDBType ( dbType string ) error {
if ! allowedDatabaseTypes [ dbType ] {
return fmt . Errorf ( "unknown database backend '%s'" , dbType )
2017-05-05 14:10:35 +02:00
}
2017-05-26 12:12:52 +02:00
config . DBType = dbType
2017-05-05 14:10:35 +02:00
return nil
}
2017-05-26 12:12:52 +02:00
// SetDBLogMode : Set the log mode in config
func ( config * Config ) SetDBLogMode ( dbLogmode string ) error {
if ! allowedDBLogModes [ dbLogmode ] {
return fmt . Errorf ( "unknown database log mode '%s'" , dbLogmode )
2017-05-11 08:01:12 +02:00
}
2017-05-26 12:12:52 +02:00
config . DBLogMode = dbLogmode
2017-05-11 08:01:12 +02:00
return nil
}
2017-05-26 12:12:52 +02:00
// Read : Decode config from json to config
2017-05-05 14:10:35 +02:00
func ( config * Config ) Read ( input io . Reader ) error {
return json . NewDecoder ( input ) . Decode ( config )
}
2017-05-26 12:12:52 +02:00
// Write : Encode config from json to config
2017-05-05 14:10:35 +02:00
func ( config * Config ) Write ( output io . Writer ) error {
return json . NewEncoder ( output ) . Encode ( config )
}
2017-05-26 12:12:52 +02:00
// Pretty : Write config json in a file
2017-05-05 14:10:35 +02:00
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
}