Archived
1

Initial commit

This commit is contained in:
2017-10-27 07:45:21 +04:00
commit 4672299afb
15 changed files with 791 additions and 0 deletions

View File

@@ -0,0 +1,74 @@
package appcontext
import (
"log"
// stdlib
"encoding/json"
"net/http"
"os"
// 3rd-party
"lab.pztrn.name/golibs/flagger"
"lab.pztrn.name/golibs/mogrus"
// local
"lab.pztrn.name/fat0troll/wind8_fetcher/lib/config"
)
// Context is an application context struct
type Context struct {
Cfg *config.Config
HTTPServerMux *http.ServeMux
Log *mogrus.LoggerHandler
StartupFlags *flagger.Flagger
}
// Init is an initialization function for context
func (c *Context) Init() {
l := mogrus.New()
l.Initialize()
c.Log = l.CreateLogger("stdout")
c.Log.CreateOutput("stdout", os.Stdout, true)
c.Cfg = config.New()
c.StartupFlags = flagger.New(c.Log)
c.StartupFlags.Initialize()
c.HTTPServerMux = http.NewServeMux()
}
// InitializeConfig fills config struct with data from given file
func (c *Context) InitializeConfig(configPath string) {
c.Cfg.Init(c.Log, configPath)
}
// InitializeStartupFlags gives information about available startup flags
func (c *Context) InitializeStartupFlags() {
configFlag := flagger.Flag{}
configFlag.Name = "config"
configFlag.Description = "Configuration file path"
configFlag.Type = "string"
configFlag.DefaultValue = "config.yaml"
err := c.StartupFlags.AddFlag(&configFlag)
if err != nil {
c.Log.Errorln(err)
}
}
// StartHTTPListener starts HTTP server on given port
func (c *Context) StartHTTPListener() {
response := make(map[string]string)
responseBody := make([]byte, 0)
c.HTTPServerMux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(404)
response["status"] = "error"
response["error_coder"] = "404"
response["descirption"] = "Not found."
responseBody, _ = json.Marshal(response)
w.Write(responseBody)
})
c.Log.Info("HTTP server started at http://" + c.Cfg.HTTPListener.Host + ":" + c.Cfg.HTTPListener.Port)
err := http.ListenAndServe(c.Cfg.HTTPListener.Host+":"+c.Cfg.HTTPListener.Port, c.HTTPServerMux)
log.Fatalln(err)
}

View File

@@ -0,0 +1,11 @@
package appcontext
var (
a *Context
)
// New is a Context creation function
func New() *Context {
c := &Context{}
return c
}

44
lib/config/config.go Normal file
View File

@@ -0,0 +1,44 @@
package config
import (
// stdlib
"io/ioutil"
"path/filepath"
// 3rd-party
"gopkg.in/yaml.v2"
"lab.pztrn.name/golibs/mogrus"
)
// HTTPServerConfiguration handles HTTP server configuration in config file
type HTTPServerConfiguration struct {
Host string `yaml:"host"`
Port string `yaml:"port"`
}
// HTTPClientConfiguration handles HTTP client configuration in config file
type HTTPClientConfiguration struct {
Host string `yaml:"host"`
Port string `yaml:"port"`
Signature string `yaml:"signature"`
}
// Config is a struct which represents config file structure
type Config struct {
HTTPListener HTTPServerConfiguration `yaml:"receiver"`
HTTPSender HTTPClientConfiguration `yaml:"sender"`
}
// Init is a configuration initializer
func (c *Config) Init(log *mogrus.LoggerHandler, configPath string) {
log.Info("Config file path: " + configPath)
fname, _ := filepath.Abs(configPath)
yamlFile, yerr := ioutil.ReadFile(fname)
if yerr != nil {
log.Fatal("Can't read config file")
}
yperr := yaml.Unmarshal(yamlFile, c)
if yperr != nil {
log.Fatal("Can't parse config file")
}
}

7
lib/config/exported.go Normal file
View File

@@ -0,0 +1,7 @@
package config
// New creates new empty Config object
func New() *Config {
c := &Config{}
return c
}