2018-04-30 22:37:36 +05:00
|
|
|
// Fast Paste Bin - uberfast and easy-to-use pastebin.
|
|
|
|
//
|
|
|
|
// Copyright (c) 2018, Stanislav N. aka pztrn and Fast Paste Bin
|
|
|
|
// developers.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
// a copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
// permit persons to whom the Software is furnished to do so, subject
|
|
|
|
// to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be
|
|
|
|
// included in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
|
|
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
|
|
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
|
|
|
|
// OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2018-04-30 18:42:17 +05:00
|
|
|
package context
|
|
|
|
|
|
|
|
import (
|
|
|
|
// stdlib
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
// local
|
|
|
|
"github.com/pztrn/fastpastebin/config"
|
|
|
|
"github.com/pztrn/fastpastebin/database/interface"
|
|
|
|
|
|
|
|
// other
|
|
|
|
"github.com/labstack/echo"
|
|
|
|
"github.com/pztrn/flagger"
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
2018-04-30 22:31:48 +05:00
|
|
|
// Context is a some sort of singleton. Basically it's a structure that
|
|
|
|
// initialized once and then passed to all parts of application. It
|
|
|
|
// contains everything every part of application need, like configuration
|
|
|
|
// access, logger, etc.
|
2018-04-30 18:42:17 +05:00
|
|
|
type Context struct {
|
|
|
|
Config *config.ConfigStruct
|
|
|
|
Database databaseinterface.Interface
|
|
|
|
Echo *echo.Echo
|
|
|
|
Flagger *flagger.Flagger
|
|
|
|
Logger zerolog.Logger
|
|
|
|
}
|
|
|
|
|
2018-04-30 22:31:48 +05:00
|
|
|
// Initialize initializes context.
|
2018-04-30 18:42:17 +05:00
|
|
|
func (c *Context) Initialize() {
|
2018-05-01 02:37:51 +05:00
|
|
|
c.Logger = zerolog.New(zerolog.ConsoleWriter{Out: os.Stdout}).With().Timestamp().Logger()
|
2018-04-30 18:42:17 +05:00
|
|
|
|
|
|
|
c.Flagger = flagger.New(nil)
|
|
|
|
c.Flagger.Initialize()
|
|
|
|
|
|
|
|
c.Flagger.AddFlag(&flagger.Flag{
|
|
|
|
Name: "config",
|
|
|
|
Description: "Configuration file path. Can be overridded with FASTPASTEBIN_CONFIG environment variable (this is what used in tests).",
|
|
|
|
Type: "string",
|
|
|
|
DefaultValue: "NO_CONFIG",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-04-30 22:31:48 +05:00
|
|
|
// LoadConfiguration loads configuration and executes right after Flagger
|
|
|
|
// have parsed CLI flags, because it depends on "-config" defined in
|
|
|
|
// Initialize().
|
2018-04-30 18:42:17 +05:00
|
|
|
func (c *Context) LoadConfiguration() {
|
|
|
|
c.Logger.Info().Msg("Loading configuration...")
|
|
|
|
|
|
|
|
var configPath = ""
|
|
|
|
|
2018-04-30 22:31:48 +05:00
|
|
|
// We're accepting configuration path from "-config" CLI parameter
|
|
|
|
// and FASTPASTEBIN_CONFIG environment variable. Later have higher
|
|
|
|
// weight and can override "-config" value.
|
2018-04-30 18:42:17 +05:00
|
|
|
configPathFromCLI, err := c.Flagger.GetStringValue("config")
|
|
|
|
configPathFromEnv, configPathFromEnvFound := os.LookupEnv("FASTPASTEBIN_CONFIG")
|
|
|
|
|
|
|
|
if err != nil && configPathFromEnvFound || err == nil && configPathFromEnvFound {
|
|
|
|
configPath = configPathFromEnv
|
|
|
|
} else if err != nil && !configPathFromEnvFound || err == nil && configPathFromCLI == "NO_CONFIG" {
|
|
|
|
c.Logger.Panic().Msg("Configuration file path wasn't passed via '-config' or 'FASTPASTEBIN_CONFIG' environment variable. Cannot continue.")
|
|
|
|
} else if err == nil && !configPathFromEnvFound {
|
|
|
|
configPath = configPathFromCLI
|
|
|
|
}
|
|
|
|
|
|
|
|
// Normalize file path.
|
|
|
|
normalizedConfigPath, err1 := filepath.Abs(configPath)
|
|
|
|
if err1 != nil {
|
|
|
|
c.Logger.Fatal().Msgf("Failed to normalize path to configuration file: %s", err1.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Logger.Debug().Msgf("Configuration file path: %s", configPath)
|
|
|
|
|
|
|
|
c.Config = &config.ConfigStruct{}
|
|
|
|
|
2018-04-30 22:31:48 +05:00
|
|
|
// Read configuration file.
|
2018-04-30 18:42:17 +05:00
|
|
|
fileData, err2 := ioutil.ReadFile(normalizedConfigPath)
|
|
|
|
if err2 != nil {
|
|
|
|
c.Logger.Panic().Msgf("Failed to read configuration file: %s", err2.Error())
|
|
|
|
}
|
|
|
|
|
2018-04-30 22:31:48 +05:00
|
|
|
// Parse it into structure.
|
2018-04-30 18:42:17 +05:00
|
|
|
err3 := yaml.Unmarshal(fileData, c.Config)
|
|
|
|
if err3 != nil {
|
|
|
|
c.Logger.Panic().Msgf("Failed to parse configuration file: %s", err3.Error())
|
|
|
|
}
|
|
|
|
|
2018-04-30 22:31:48 +05:00
|
|
|
// Yay! See what it gets!
|
2018-04-30 18:42:17 +05:00
|
|
|
c.Logger.Debug().Msgf("Parsed configuration: %+v", c.Config)
|
2018-05-26 12:05:37 +05:00
|
|
|
|
|
|
|
// Set log level.
|
|
|
|
c.Logger.Info().Msgf("Setting logger level: %s", c.Config.Logging.LogLevel)
|
|
|
|
switch c.Config.Logging.LogLevel {
|
|
|
|
case "DEBUG":
|
|
|
|
zerolog.SetGlobalLevel(zerolog.DebugLevel)
|
|
|
|
case "INFO":
|
|
|
|
zerolog.SetGlobalLevel(zerolog.InfoLevel)
|
|
|
|
case "WARN":
|
|
|
|
zerolog.SetGlobalLevel(zerolog.WarnLevel)
|
|
|
|
case "ERROR":
|
|
|
|
zerolog.SetGlobalLevel(zerolog.ErrorLevel)
|
|
|
|
case "FATAL":
|
|
|
|
zerolog.SetGlobalLevel(zerolog.FatalLevel)
|
|
|
|
case "PANIC":
|
|
|
|
zerolog.SetGlobalLevel(zerolog.PanicLevel)
|
|
|
|
}
|
2018-04-30 18:42:17 +05:00
|
|
|
}
|
|
|
|
|
2018-04-30 22:31:48 +05:00
|
|
|
// RegisterDatabaseInterface registers database interface for later use.
|
2018-04-30 18:42:17 +05:00
|
|
|
func (c *Context) RegisterDatabaseInterface(di databaseinterface.Interface) {
|
|
|
|
c.Database = di
|
|
|
|
}
|
|
|
|
|
2018-04-30 22:31:48 +05:00
|
|
|
// RegisterEcho registers Echo instance for later usage.
|
2018-04-30 18:42:17 +05:00
|
|
|
func (c *Context) RegisterEcho(e *echo.Echo) {
|
|
|
|
c.Echo = e
|
|
|
|
}
|
|
|
|
|
2018-04-30 22:31:48 +05:00
|
|
|
// Shutdown shutdowns entire application.
|
2018-04-30 18:42:17 +05:00
|
|
|
func (c *Context) Shutdown() {
|
|
|
|
c.Logger.Info().Msg("Shutting down Fast Pastebin...")
|
2018-05-27 12:25:01 +05:00
|
|
|
|
|
|
|
c.Database.Shutdown()
|
2018-04-30 18:42:17 +05:00
|
|
|
}
|