Skip to content

Instantly share code, notes, and snippets.

View presmihaylov's full-sized avatar

Pres Mihaylov presmihaylov

View GitHub Profile
...
// ProvideLogger to fx
func ProvideLogger() *zap.SugaredLogger {
logger, _ := zap.NewProduction()
slogger := logger.Sugar()
return slogger
}
func main() {
...
func main() {
fx.New(
fx.Provide(ProvideConfig),
).Run()
...
}
...
// ProvideConfig provides the standard configuration to fx
func ProvideConfig() *Config {
conf := Config{}
data, err := ioutil.ReadFile("config/base.yaml")
// handle error
err = yaml.Unmarshal([]byte(data), &conf)
// handle error
...
func main() {
fx.New().Run()
...
}
func main() {
conf := &Config{}
data, err := ioutil.ReadFile("config/base.yaml")
// handle error
err = yaml.Unmarshal([]byte(data), &conf)
// handle error
...
http.ListenAndServe(conf.ApplicationConfig.Address, mux)
...
// ApplicationConfig ...
type ApplicationConfig struct {
Address string `yaml:"address"`
}
// Config ...
type Config struct {
ApplicationConfig `yaml:"application"`
}
application:
address: :8080
func main() {
logger, _ := zap.NewProduction()
defer logger.Sync()
slogger := logger.Sugar()
mux := http.NewServeMux()
httphandler.New(mux, slogger)
http.ListenAndServe(":8080", mux)
}
@presmihaylov
presmihaylov / main.go
Last active April 12, 2020 07:25
Main function at the start
package main
import (
"net/http"
"github.com/preslavmihaylov/fxappexample/httphandler"
)
func main() {
mux := http.NewServeMux()
@presmihaylov
presmihaylov / httphandler.go
Last active April 12, 2020 07:24
First try at Go http handler
package httphandler
import "net/http"
// Handler for http requests
type Handler struct {
mux *http.ServeMux
}
// New http handler