Merge pull request #161 from majestrate/i2p-listener
optionally provide i2p connectivity
Cette révision appartient à :
révision
efb12315a8
4 fichiers modifiés avec 42 ajouts et 7 suppressions
|
@ -17,9 +17,11 @@ type Config struct {
|
||||||
// This will be directly passed to Gorm, and its internal
|
// This will be directly passed to Gorm, and its internal
|
||||||
// structure depends on the dialect for each db type
|
// structure depends on the dialect for each db type
|
||||||
DBParams string `json: "db_params"`
|
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{
|
var allowedDatabaseTypes = map[string]bool{
|
||||||
"sqlite3": true,
|
"sqlite3": true,
|
||||||
|
|
8
config/i2p.go
Fichier normal
8
config/i2p.go
Fichier normal
|
@ -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
13
main.go
|
@ -3,11 +3,11 @@ package main
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
|
||||||
"github.com/nicksnyder/go-i18n/i18n"
|
"github.com/nicksnyder/go-i18n/i18n"
|
||||||
|
|
||||||
"github.com/ewhal/nyaa/config"
|
"github.com/ewhal/nyaa/config"
|
||||||
"github.com/ewhal/nyaa/db"
|
"github.com/ewhal/nyaa/db"
|
||||||
|
"github.com/ewhal/nyaa/network"
|
||||||
"github.com/ewhal/nyaa/router"
|
"github.com/ewhal/nyaa/router"
|
||||||
"github.com/ewhal/nyaa/util/log"
|
"github.com/ewhal/nyaa/util/log"
|
||||||
"github.com/ewhal/nyaa/util/search" // super hacky fix
|
"github.com/ewhal/nyaa/util/search" // super hacky fix
|
||||||
|
@ -27,16 +27,17 @@ func RunServer(conf *config.Config) {
|
||||||
http.Handle("/", router.Router)
|
http.Handle("/", router.Router)
|
||||||
|
|
||||||
// Set up server,
|
// Set up server,
|
||||||
addr := fmt.Sprintf("%s:%d", conf.Host, conf.Port)
|
|
||||||
srv := &http.Server{
|
srv := &http.Server{
|
||||||
Addr: addr,
|
|
||||||
WriteTimeout: 15 * time.Second,
|
WriteTimeout: 15 * time.Second,
|
||||||
ReadTimeout: 15 * time.Second,
|
ReadTimeout: 15 * time.Second,
|
||||||
}
|
}
|
||||||
log.Infof("listening on %s", addr)
|
l, err := network.CreateHTTPListener(conf)
|
||||||
|
|
||||||
err := srv.ListenAndServe()
|
|
||||||
log.CheckError(err)
|
log.CheckError(err)
|
||||||
|
if err == nil {
|
||||||
|
log.Infof("listening on %s", l.Addr())
|
||||||
|
err := srv.Serve(l)
|
||||||
|
log.CheckError(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
24
network/network.go
Fichier normal
24
network/network.go
Fichier normal
|
@ -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
|
||||||
|
}
|
Référencer dans un nouveau ticket