Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0

add initial optional i2p connectivity

Cette révision appartient à :
Jeff Becker 2017-05-07 15:51:37 -04:00
Parent 9337a68516
révision 37350a1233
4 fichiers modifiés avec 43 ajouts et 8 suppressions

Voir le fichier

@ -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,

8
config/i2p.go Fichier normal
Voir le fichier

@ -0,0 +1,8 @@
package config
// i2p connectivity config
type I2PConfig struct {
Name string `json: "name"`
Addr string `json: "samaddr"`
Keyfile string `json: "keyfile"`
}

13
main.go
Voir le fichier

@ -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/signals"
@ -26,16 +26,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() {

24
network/network.go Fichier normal
Voir le fichier

@ -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
}