now supports some basic configuration

This commit is contained in:
Jeffrey Paul 2019-08-21 10:31:57 +02:00
parent f36da8dde9
commit e1fc2f11d7
4 changed files with 35 additions and 7 deletions

View File

@ -1,11 +1,19 @@
VERSION := $(shell git rev-parse --short HEAD) VERSION := $(shell git rev-parse --short HEAD)
BUILDTIME := $(shell date -u '+%Y-%m-%dT%H:%M:%SZ') BUILDTIME := $(shell date -u '+%Y-%m-%dT%H:%M:%SZ')
BUILDUSER := $(shell whoami)
BUILDHOST := $(shell hostname -s)
BUILDARCH := $(shell uname -m)
GOLDFLAGS += -X main.Version=$(VERSION) GOLDFLAGS += -X main.Version=$(VERSION)
GOLDFLAGS += -X main.Buildtime=$(BUILDTIME) GOLDFLAGS += -X main.Buildtime=$(BUILDTIME)
GOLDFLAGS += -X main.Builduser=$(BUILDUSER)@$(BUILDHOST)
GOLDFLAGS += -X main.Buildarch=$(BUILDARCH)
GOFLAGS = -ldflags "$(GOLDFLAGS)" GOFLAGS = -ldflags "$(GOLDFLAGS)"
default: run default: rundebug
rundebug: build
DEBUG=1 ./ircd
run: build run: build
./ircd ./ircd

29
main.go
View File

@ -1,5 +1,6 @@
package main package main
import "os"
import "fmt" import "fmt"
import "github.com/sirupsen/logrus" import "github.com/sirupsen/logrus"
import "github.com/sneak/sircd/sircd" import "github.com/sneak/sircd/sircd"
@ -7,22 +8,22 @@ import "github.com/spf13/viper"
var Version string var Version string
var Buildtime string var Buildtime string
var Builduser string
var Buildarch string
func main() { func main() {
// set up logging
log := logrus.New()
log.SetLevel(logrus.DebugLevel)
log.SetReportCaller(true)
log.Println("sircd starting up")
c := viper.New() c := viper.New()
// default config variables // default config variables
c.SetDefault("myhostname", "irc.example.com") c.SetDefault("myhostname", "irc.example.com")
c.SetDefault("network", "ExampleNet") c.SetDefault("network", "ExampleNet")
c.SetDefault("admin", "webmaster@example.com") c.SetDefault("admin", "webmaster@example.com")
c.SetDefault("loglevel", "info")
c.SetDefault("version", Version) c.SetDefault("version", Version)
c.SetDefault("buildtime", Buildtime) c.SetDefault("buildtime", Buildtime)
c.SetDefault("builduser", Builduser)
c.SetDefault("buildarch", Buildarch)
// read config file // read config file
c.SetConfigName("sircd") // name of config file (without extension) c.SetConfigName("sircd") // name of config file (without extension)
@ -34,6 +35,24 @@ func main() {
panic(fmt.Errorf("Fatal error config file: %s \n", err)) panic(fmt.Errorf("Fatal error config file: %s \n", err))
} }
// this is the only config settable by env var by design
// do everything else in the config file
if os.Getenv("DEBUG") != "" {
c.Set("loglevel", "debug")
}
// set up logging
log := logrus.New()
log.SetReportCaller(false)
switch c.GetString("loglevel") {
case "error":
log.SetLevel(logrus.ErrorLevel)
case "info":
log.SetLevel(logrus.InfoLevel)
default:
log.SetLevel(logrus.DebugLevel)
log.SetReportCaller(true)
}
// instantiate server // instantiate server
s := sircd.New(c) s := sircd.New(c)
// give it our logger // give it our logger

View File

@ -1,3 +1,4 @@
myhostname: irc.example.com myhostname: irc.example.com
network: ExampleNet network: ExampleNet
admin: webmaster@example.com admin: webmaster@example.com
loglevel: info

View File

@ -43,7 +43,7 @@ func (s *ircd) SetServerName(name string) {
} }
func (s *ircd) Start() { func (s *ircd) Start() {
s.log.Infof("sircd version=%s buildtime=%s starting.", s.c.GetString("version"), s.c.GetString("buildtime")) s.log.Infof("sircd version %s (%s) built %s by %s starting.", s.c.GetString("version"), s.c.GetString("buildarch"), s.c.GetString("buildtime"), s.c.GetString("builduser"))
s.newClients = make(chan *ircClient, 128) s.newClients = make(chan *ircClient, 128)
s.deadClients = make(chan *ircClient, 128) s.deadClients = make(chan *ircClient, 128)
s.messageQueue = make(chan *ircMessage, 128) s.messageQueue = make(chan *ircMessage, 128)