package main

import (
	"fmt"
	"io"
	"io/ioutil"
	"os"
)

type config struct {
	controllerKey string
	ourPort       string
	logOut        io.Writer
}

func loadConfigFromEnv() (*config, error) {
	c := &config{}
	c.controllerKey = os.Getenv("CONTROLLER_KEY")
	if c.controllerKey == "" {
		return nil, fmt.Errorf("CONTROLLER_KEY is required")
	}
	port := os.Getenv("PORT")
	if port == "" {
		port = "4456"
	}
	c.ourPort = port

	logPath := os.Getenv("LOGFILE")
	c.logOut = ioutil.Discard
	if logPath != "" {
		if f, err := os.OpenFile(logPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666); err == nil {
			c.logOut = f
		}
	}
	return c, nil
}

package main

import (
	"fmt"
	"io"
	"os"
)

type config struct {
	controllerKey string
	ourPort       string
	logOut        io.Writer
}

func loadConfigFromEnv() (*config, error) {
	c := &config{}
	c.controllerKey = os.Getenv("CONTROLLER_KEY")
	if c.controllerKey == "" {
		return nil, fmt.Errorf("CONTROLLER_KEY is required")
	}
	port := os.Getenv("PORT")
	if port == "" {
		port = "4456"
	}
	c.ourPort = port

	if logPath := os.Getenv("LOGFILE"); logPath != "" {
		if f, err := os.OpenFile(logPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666); err == nil {
			c.logOut = f
		}
	}
	if c.logOut == nil {
		c.logOut = os.Stderr
	}
	return c, nil
}

Send log output to STDERR by default
controller/examples: Send log output to STDERR by default

The previous default was to discard any messages, which will
suppress errors.

Signed-off-by: Jonathan Rudenberg <3692bfa45759a67d83aedf0045f6cb635a966abf@titanous.com>
