diff --git a/config/config.go b/config/config.go index 931a8e74..56fe4699 100644 --- a/config/config.go +++ b/config/config.go @@ -17,9 +17,11 @@ type Config struct { // This will be directly passed to Gorm, and its internal // structure depends on the dialect for each db type DBParams string `json: "db_params"` + // optional i2p configuration + I2P *I2PConfig `json: "i2p"` } -var Defaults = Config{"localhost", 9999, "sqlite3", "./nyaa.db?cache_size=50"} +var Defaults = Config{"localhost", 9999, "sqlite3", "./nyaa.db?cache_size=50", nil} var allowedDatabaseTypes = map[string]bool{ "sqlite3": true, diff --git a/config/i2p.go b/config/i2p.go new file mode 100644 index 00000000..60f0f966 --- /dev/null +++ b/config/i2p.go @@ -0,0 +1,8 @@ +package config + +// i2p connectivity config +type I2PConfig struct { + Name string `json: "name"` + Addr string `json: "samaddr"` + Keyfile string `json: "keyfile"` +} diff --git a/main.go b/main.go index c4e719d4..2784a9c5 100644 --- a/main.go +++ b/main.go @@ -3,11 +3,11 @@ package main import ( "bufio" "flag" - "fmt" "github.com/nicksnyder/go-i18n/i18n" "github.com/ewhal/nyaa/config" "github.com/ewhal/nyaa/db" + "github.com/ewhal/nyaa/network" "github.com/ewhal/nyaa/router" "github.com/ewhal/nyaa/util/log" "github.com/ewhal/nyaa/util/search" // super hacky fix @@ -27,16 +27,17 @@ func RunServer(conf *config.Config) { http.Handle("/", router.Router) // Set up server, - addr := fmt.Sprintf("%s:%d", conf.Host, conf.Port) srv := &http.Server{ - Addr: addr, WriteTimeout: 15 * time.Second, ReadTimeout: 15 * time.Second, } - log.Infof("listening on %s", addr) - - err := srv.ListenAndServe() + l, err := network.CreateHTTPListener(conf) log.CheckError(err) + if err == nil { + log.Infof("listening on %s", l.Addr()) + err := srv.Serve(l) + log.CheckError(err) + } } func main() { diff --git a/network/network.go b/network/network.go new file mode 100644 index 00000000..89b249b6 --- /dev/null +++ b/network/network.go @@ -0,0 +1,24 @@ +package network + +import ( + "fmt" + "github.com/ewhal/nyaa/config" + "github.com/ewhal/nyaa/util/log" + "github.com/majestrate/i2p-tools/lib/i2p" + "net" +) + +// CreateHTTPListener creates a net.Listener for main http webapp given main config +func CreateHTTPListener(conf *config.Config) (l net.Listener, err error) { + if conf.I2P == nil { + l, err = net.Listen("tcp", fmt.Sprintf("%s:%d", conf.Host, conf.Port)) + } else { + s := i2p.NewSession(conf.I2P.Name, conf.I2P.Addr, conf.I2P.Keyfile) + err = s.Open() + if s != nil { + log.Infof("i2p address: %s", s.B32Addr()) + l = s + } + } + return +}