Skip to content

Instantly share code, notes, and snippets.

View madflojo's full-sized avatar
:octocat:
Writing code that might be useful

Benjamin Cane madflojo

:octocat:
Writing code that might be useful
View GitHub Profile
@madflojo
madflojo / app-viper.go
Last active May 16, 2021 21:49
Viper Article - Viper replacing Config
package app
import (
// imports go here
"github.com/spf13/viper"
)
// Common errors returned by this app.
var (
ErrShutdown = fmt.Errorf("application shutdown gracefully")
@madflojo
madflojo / main-viper.go
Created May 15, 2021 18:40
Viper Article - Main with Viper
package main
import (
"github.com/madflojo/go-quick/app"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
func main() {
// Initiate a simple logger
@madflojo
madflojo / main-viper-files.go
Last active May 15, 2021 18:55
Viper Article - Adding Files
// Load Config
cfg.AddConfigPath("./conf")
cfg.SetEnvPrefix("app")
cfg.AllowEmptyEnv(true)
cfg.AutomaticEnv()
err := cfg.ReadInConfig()
if err != nil {
switch err.(type) {
case viper.ConfigFileNotFoundError:
log.Warnf("No Config file found, loaded config from Environment - Default path ./conf")
@madflojo
madflojo / consulator.yml
Created May 16, 2021 18:28
Viper Article - Consulator config
go-quick:
config: '{"from_consul": true, "debug": false, "trace": false}'
@madflojo
madflojo / main-consul.go
Created May 16, 2021 18:39
Viper Article - Loading Config from Consul
// Load Config
cfg.AddConfigPath("./conf")
cfg.SetEnvPrefix("app")
cfg.AllowEmptyEnv(true)
cfg.AutomaticEnv()
err := cfg.ReadInConfig()
if err != nil {
switch err.(type) {
case viper.ConfigFileNotFoundError:
log.Warnf("No Config file found, loaded config from Environment - Default path ./conf")
@madflojo
madflojo / main-imports.go
Created May 16, 2021 18:46
Viper Article - Main Import
package main
import (
"github.com/madflojo/go-quick/app"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
// Add remote provider support to Viper
_ "github.com/spf13/viper/remote"
)
@madflojo
madflojo / task-reload.go
Created May 16, 2021 19:28
Viper Article - Watch Viper config
// Setup Scheduler
scheduler = tasks.New()
defer scheduler.Stop()
// Config Reload
if cfg.GetInt("config_watch_interval") > 0 {
_, err := scheduler.Add(&tasks.Task{
Interval: time.Duration(cfg.GetInt("config_watch_interval")) * time.Second,
TaskFunc: func() error {
// Reload config using Viper's Watch capabilities
@madflojo
madflojo / http.go
Created July 31, 2021 03:22
httprouter Article - Basic HTTP Example (no router)
package main
import (
"fmt"
"log"
"net/http"
)
// handler is a basic HTTP handler that prints hello world.
func handler(w http.ResponseWriter, r *http.Request) {
@madflojo
madflojo / httprouter.go
Created July 31, 2021 03:52
httprouter Article - Basic HTTP Example (w/httprouter)
package main
import (
"fmt"
"github.com/julienschmidt/httprouter"
"log"
"net/http"
)
// handler is a basic HTTP handler that prints hello world.
@madflojo
madflojo / httprouter-params.go
Last active August 1, 2021 02:12
httprouter Article - Parameterized Handler
// handler is a basic HTTP handler that prints hello world.
func handler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
fmt.Fprintf(w, "Hello %s", ps.ByName("name"))
}