Skip to content

Commit

Permalink
Added logutils, got server config working
Browse files Browse the repository at this point in the history
Added logutils for simple log filtering.  Got configuration working for
the http server bind address and ports.  Now there is a default, which
can be overridden with a configuration file, which can be overridden
with a command line flag
  • Loading branch information
danesparza committed Mar 14, 2016
1 parent 7599131 commit cd236dc
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
3 changes: 2 additions & 1 deletion centralconfig.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

http:
port: 3000
boltdb:
database: config.db
15 changes: 9 additions & 6 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ package cmd

import (
"encoding/json"
"fmt"
"log"
"net/http"
"strconv"

"github.com/spf13/viper"

Expand Down Expand Up @@ -37,11 +36,11 @@ func serve(cmd *cobra.Command, args []string) {

// If we have a config file, report it:
if viper.ConfigFileUsed() != "" {
fmt.Println("Using config file:", viper.ConfigFileUsed())
log.Println("[INFO] Using config file:", viper.ConfigFileUsed())
}

// Get configuration information
fmt.Println(viper.GetString("boltdb.database"))
log.Printf("[INFO] Using BoltDB database: %s", viper.GetString("boltdb.database"))

// Create a router and setup our REST endpoints...
r := mux.NewRouter()
Expand All @@ -65,14 +64,18 @@ func serve(cmd *cobra.Command, args []string) {
json.NewEncoder(w).Encode(tweets)
})

portString := strconv.Itoa(serverPort)
http.ListenAndServe(serverInterface+":"+portString, r)
log.Printf("[INFO] HTTP server info: %s:%s", viper.GetString("http.bind"), viper.GetString("http.port"))
http.ListenAndServe(viper.GetString("http.bind")+":"+viper.GetString("http.port"), r)
}

func init() {
RootCmd.AddCommand(serveCmd)

// Setup our flags
serveCmd.Flags().IntVarP(&serverPort, "port", "p", 1313, "port on which the server will listen")
serveCmd.Flags().StringVarP(&serverInterface, "bind", "", "127.0.0.1", "interface to which the server will bind")

// Bind config flags for optional config file override:
viper.BindPFlag("http.port", serveCmd.Flags().Lookup("port"))
viper.BindPFlag("http.bind", serveCmd.Flags().Lookup("bind"))
}
15 changes: 14 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
package main

import "github.com/danesparza/centralconfig/cmd"
import (
"log"
"os"

"github.com/danesparza/centralconfig/cmd"
"github.com/hashicorp/logutils"
)

func main() {
filter := &logutils.LevelFilter{
Levels: []logutils.LogLevel{"DEBUG", "INFO", "WARN", "ERROR"},
MinLevel: logutils.LogLevel("INFO"),
Writer: os.Stderr,
}
log.SetOutput(filter)

cmd.Execute()
}

0 comments on commit cd236dc

Please sign in to comment.