configure server with json

This commit is contained in:
Jeremy Latt 2014-02-09 07:53:42 -08:00
parent 55d3d6e946
commit 281558072a
4 changed files with 51 additions and 12 deletions

27
irc/config.go Normal file
View file

@ -0,0 +1,27 @@
package irc
import (
"encoding/json"
"os"
)
type Config struct {
Name string
Listen string
Password string
Debug map[string]bool
}
func LoadConfig() (config *Config, err error) {
config = &Config{}
file, err := os.Open("ergonomadic.json")
if err != nil {
return
}
defer file.Close()
decoder := json.NewDecoder(file)
err = decoder.Decode(config)
return
}

View file

@ -19,16 +19,17 @@ type Server struct {
clients ClientNameMap
}
func NewServer(name string) *Server {
func NewServer(config *Config) *Server {
commands := make(chan Command)
server := &Server{
ctime: time.Now(),
name: name,
name: config.Name,
commands: commands,
clients: make(ClientNameMap),
channels: make(ChannelNameMap),
}
go server.receiveCommands(commands)
go server.listen(config.Listen)
return server
}
@ -42,7 +43,7 @@ func (server *Server) receiveCommands(commands <-chan Command) {
}
}
func (s *Server) Listen(addr string) {
func (s *Server) listen(addr string) {
listener, err := net.Listen("tcp", addr)
if err != nil {
log.Fatal("Server.Listen: ", err)