diff --git a/config/config.go b/config/config.go index a2c4a2d9..b5a19cf2 100644 --- a/config/config.go +++ b/config/config.go @@ -8,8 +8,7 @@ import ( "fmt" "io" "os" - "sync" - ) +) type Config struct { Host string `json: "host"` @@ -17,11 +16,11 @@ type Config struct { DBType string `json: "db_type"` // This will be directly passed to Gorm, and its internal // structure depends on the dialect for each db type - DBParams string `json: "db_type"` + DBParams string `json: "db_params"` } var Defaults = Config{"localhost", 9999, "sqlite3", "./nyaa.db?cache_size=50"} -var PrintDefaults *bool + var allowedDatabaseTypes = map[string]bool{ "sqlite3": true, "postgres": true, @@ -29,45 +28,39 @@ var allowedDatabaseTypes = map[string]bool{ "mssql": true, } -var instance *Config -var once sync.Once - -func GetInstance() *Config { - once.Do(func() { - instance = &Config{} - instance.Host = Defaults.Host - instance.Port = Defaults.Port - instance.DBType = Defaults.DBType - instance.DBParams = Defaults.DBParams - instance.BindFlags() - - }) - return instance +func NewConfig() *Config { + var config Config + config.Host = Defaults.Host + config.Port = Defaults.Port + config.DBType = Defaults.DBType + config.DBParams = Defaults.DBParams + return &config } type processFlags func() error -func (config *Config) BindFlags() error { +func (config *Config) BindFlags() processFlags { // This function returns a function which is to be used after - // flag.Parse to check and copy the flags' values to the config instance. + // flag.Parse to check and copy the flags' values to the Config instance. conf_file := flag.String("conf", "", "path to the configuration file") db_type := flag.String("dbtype", Defaults.DBType, "database backend") host := flag.String("host", Defaults.Host, "binding address of the server") port := flag.Int("port", Defaults.Port, "port of the server") db_params := flag.String("dbparams", Defaults.DBParams, "parameters to open the database (see Gorm's doc)") - PrintDefaults = flag.Bool("print-defaults", false, "print the default configuration file on stdout") - flag.Parse() - err := config.HandleConfFileFlag(*conf_file) - if err != nil { + + return func() error { + // You can override fields in the config file with flags. + config.Host = *host + config.Port = *port + config.DBParams = *db_params + err := config.SetDBType(*db_type) + if err != nil { + return err + } + err = config.HandleConfFileFlag(*conf_file) return err } - // You can override fields in the config file with flags. - config.Host = *host - config.Port = *port - config.DBParams = *db_params - err = config.SetDBType(*db_type) - return err } func (config *Config) HandleConfFileFlag(path string) error { @@ -109,4 +102,4 @@ func (config *Config) Pretty(output io.Writer) error { data = append(data, []byte("\n")...) _, err = output.Write(data) return err -} +} \ No newline at end of file diff --git a/css/style-night.css b/css/style-night.css new file mode 100644 index 00000000..b89441c5 --- /dev/null +++ b/css/style-night.css @@ -0,0 +1,149 @@ +/* Torrent status colors */ +.remake { + background-color: #a05013; +} +.trusted { + background-color: #238025; +} +.aplus { + background-color: #0777ab; +} + +/* modified copy of NyaaTorrent theme */ +#mainmenu { + background: #263238; + position: fixed; + color: #f2f2f2; + width: 100%; + z-index: 4; + border: 0px solid #263238; +} + +#mainmenu a { + color: #eff5f5; +} + +#container { + padding-top: 1.25em; +} + +#container.cont-home { + background: #29363d url(/img/megumin.png) no-repeat; +} +#container.cont-view { + background: #29363d url(/img/megumin.png) no-repeat; + background-size: 75px, 100px; +} + +body { + background-color: #264040; + color: #eff5f5; + font-family: Arial, sans-serif; + margin: 0; + padding: 0; +} + +.torrentNav { + text-align: center; +} + +.pagination { + background-color: #264040; +} + +.pagination > .active > a { + background-color: #ececec; + border-color: #ececec; + color: #337ab7; /* restore usual text color */ +} + +/* Links, Text */ +a { + color: #1cb093; + text-decoration : none; +} + +.download-btn { + font-size: 20px; +} + +.table-borderless > tbody > tr > td, +.table-borderless > tbody > tr > th, +.table-borderless > tfoot > tr > td, +.table-borderless > tfoot > tr > th, +.table-borderless > thead > tr > td, +.table-borderless > thead > tr > th { + border: none; +} + +.torrent-info .name { + font-weight: bold; + overflow: hidden; + padding-bottom: 3px; + padding-top: 3px; + word-wrap: break-word; +} + +.torrent-hash { + font-family: monospace; +} + +.torrent-info .filesize { + white-space: nowrap; +} + +.comment-row td:first-of-type { + vertical-align: top; + text-align: right; +} + +/* Table style & fixes */ +.table > tbody > tr > td { + /* fix bootstrap uglyness */ + vertical-align: middle; +} + +.table > tbody > tr > th, .table > tbody > tr > td { + padding: 4px; +} + +tr.torrent-info td.date { + white-space: nowrap; +} + +.custom-table-hover > tbody > tr:hover { + opacity: 0.82; +} + +.comment-row { + border-top: 1px solid #ddd; +} + +div.container div.blockBody:nth-of-type(2) table{table-layout:fixed;} +div.container div.blockBody:nth-of-type(2) table tr:first-of-type th:first-of-type, div.container div.blockBody:nth-of-type(2) table tr:first-of-type th:nth-of-type(5){width:10%;} +div.container div.blockBody:nth-of-type(2) table tr:first-of-type th:nth-of-type(3){width:15%;} +div.container div.blockBody:nth-of-type(2) table tr:first-of-type th:nth-of-type(4){width:19%;} +div.container div.blockBody:nth-of-type(2) table tr:first-of-type th:last-of-type{width:6%;} + +/* Mobile-friendly main table */ +@media only screen and (max-width: 700px) { + table, thead, tbody, tr { + display: block; + } + + th { + display: none; + } + + td { + display: inline-block; + } + + td:nth-of-type(1), td:nth-of-type(2) { + display: block; + } + + .table > tbody > tr > td { + border: none; + } +} diff --git a/db/gorm.go b/db/gorm.go index 59779e8a..93255109 100644 --- a/db/gorm.go +++ b/db/gorm.go @@ -2,20 +2,17 @@ package db import ( "github.com/ewhal/nyaa/config" - "github.com/ewhal/nyaa/util/log" "github.com/ewhal/nyaa/model" + "github.com/ewhal/nyaa/util/log" "github.com/jinzhu/gorm" _ "github.com/jinzhu/gorm/dialects/sqlite" // _ "github.com/go-sql-driver/mysql" ) -var ORM, Errs = GormInit() +var ORM *gorm.DB // GormInit init gorm ORM. -func GormInit() (*gorm.DB, error) { - - conf := config.GetInstance() - +func GormInit(conf *config.Config) (*gorm.DB, error) { db, err := gorm.Open(conf.DBType, conf.DBParams) // db, err := gorm.Open("mysql", config.MysqlDSL()) //db, err := gorm.Open("sqlite3", "/tmp/gorm.db") @@ -39,7 +36,6 @@ func GormInit() (*gorm.DB, error) { } log.CheckError(err) - log.Infof("lol", conf.DBParams) // relation := gorm.Relationship{} // relation.Kind = "many2many" @@ -58,4 +54,4 @@ func GormInit() (*gorm.DB, error) { // handler.Setup(&relation, "users_followers", m1Type, m2Type) return db, err -} +} \ No newline at end of file diff --git a/img/megumin.png b/img/megumin.png new file mode 100644 index 00000000..9a4097af Binary files /dev/null and b/img/megumin.png differ diff --git a/main.go b/main.go old mode 100755 new mode 100644 index ea605f16..e2ce0e97 --- a/main.go +++ b/main.go @@ -2,9 +2,11 @@ package main import ( "bufio" + "flag" "fmt" "github.com/ewhal/nyaa/config" + "github.com/ewhal/nyaa/db" "github.com/ewhal/nyaa/router" "github.com/ewhal/nyaa/util/log" @@ -28,13 +30,21 @@ func RunServer(conf *config.Config) { } func main() { - conf := config.GetInstance() - if *config.PrintDefaults { + conf := config.NewConfig() + process_flags := conf.BindFlags() + defaults := flag.Bool("print-defaults", false, "print the default configuration file on stdout") + flag.Parse() + if *defaults { stdout := bufio.NewWriter(os.Stdout) conf.Pretty(stdout) stdout.Flush() os.Exit(0) } else { + err := process_flags() + if err != nil { + log.CheckError(err) + } + db.ORM, _ = db.GormInit(conf) RunServer(conf) } -} +} \ No newline at end of file diff --git a/model/torrent.go b/model/torrent.go index a1a84d98..eedbefdb 100644 --- a/model/torrent.go +++ b/model/torrent.go @@ -49,7 +49,7 @@ type Torrents struct { Hash string `gorm:"column:torrent_hash"` Date int64 `gorm:"column:date"` Downloads int `gorm:"column:downloads"` - Filesize string `gorm:"column:filesize"` + Filesize int64 `gorm:"column:filesize"` Description []byte `gorm:"column:description"` Comments []byte `gorm:"column:comments"` Statuses Statuses `gorm:"ForeignKey:status_id;AssociationForeignKey:status_id"` @@ -112,7 +112,7 @@ func (t *Torrents) ToJson() TorrentsJson { Status: t.Status, Hash: t.Hash, Date: time.Unix(t.Date, 0).Format(time.RFC3339), - Filesize: t.Filesize, + Filesize: util.FormatFilesize(t.Filesize), Description: template.HTML(util.UnZlib(t.Description)), Comments: b, Sub_Category: t.Sub_Categories.ToJson(), diff --git a/templates/FAQ.html b/templates/FAQ.html index aa1e01b9..b2ab379c 100644 --- a/templates/FAQ.html +++ b/templates/FAQ.html @@ -30,7 +30,7 @@

How are we recovering?

The aforementioned databases are being hosted at nyaa.pantsu.cat and sukebei.pantsu.cat. There is a search function, and (almost) full nyaa functionality should be coming soon. - Seeder/leecher statistics are possible via scraping and might be restored somewhen in the future, + Seeder/leecher statistics are possible via scraping and might be restored sometime in the future, since other feature take priority right now.

Are the torrents still working?

@@ -38,7 +38,7 @@ As long as the file is listed on the DHT network, business should carry on as usual.

How do I download the torrents?

-

Just use the magnet link. The magnet link will used by your BitTorrent client to look up the file on the +

Just use the magnet link. The magnet link will be used by your BitTorrent client to look up the file on the DHT network and it should download just fine.

The magnet link should look like this: magnet:?xt=urn:btih:[hash]&dn=[name]&tr=[tracker]&tr=[...]

@@ -51,7 +51,7 @@

https://github.com/ewhal/nyaa/issues.

Why is your shit written in Go?

-

It's the authors favorite programming language.

+

It's the author's favorite programming language.


nyaa.pantsu.cat and sukebei.pantsu.cat do not host any files.

diff --git a/templates/home.html b/templates/home.html index 8e3ef6f4..6f578b40 100644 --- a/templates/home.html +++ b/templates/home.html @@ -46,6 +46,11 @@ {{end}} + {{end}} diff --git a/util/format.go b/util/format.go new file mode 100644 index 00000000..fb2cbb1d --- /dev/null +++ b/util/format.go @@ -0,0 +1,24 @@ +package util + +import ( + "fmt" +) + +func FormatFilesize(bytes int64) string { + var unit string + var value float64 + if bytes > 1024*1024*1024 { + unit = "GiB" + value = float64(bytes) / (1024*1024*1024) + } else if bytes > 1024*1024 { + unit = "MiB" + value = float64(bytes) / (1024*1024) + } else if bytes > 1024 { + unit = "KiB" + value = float64(bytes) / (1024) + } else { + unit = "B" + value = float64(bytes) + } + return fmt.Sprintf("%.1f %s", value, unit) +}