very basic thoughts on module and file structure

basic configuration
basic logger
This commit is contained in:
Jakob Schrettenbrunner 2017-06-19 00:01:44 +02:00
parent 2568d9dd1a
commit 326fdcae6e
8 changed files with 156 additions and 0 deletions

1
api/api.go Normal file
View File

@ -0,0 +1 @@
package api

32
config/config.go Normal file
View File

@ -0,0 +1,32 @@
package config
import (
"github.com/spf13/viper"
)
// Config contains the configuration of the Pterodactyl Daemon
type Config struct {
// Log contains configuration related to logging
Log struct {
// DeleteAfterDays is the time in days after which logfiles are deleted
// If set to <= 0 logs are kept forever
DeleteAfterDays int
} `json:"log"`
}
func LoadConfiguration() error {
viper.SetConfigName("config")
viper.AddConfigPath(".")
// Find and read the config file
if err := viper.ReadInConfig(); err != nil {
return err
}
return nil
}
func setDefaults() {
}

18
environments/docker.go Normal file
View File

@ -0,0 +1,18 @@
package environments
type DockerEnvironment struct {
BaseEnvironment
}
func NewDockerEnvironment() *DockerEnvironment {
return &DockerEnvironment{}
}
func (env *DockerEnvironment) Exec() error {
}
func (env *DockerEnvironment) Create() error {
return nil
}

View File

@ -0,0 +1,43 @@
package environments
// Environment provides abstraction of different environments
type Environment interface {
// Execute a command in the environment
Exec() error
// Create creates the environment
Create() error
// Destroy destroys the environment
Destroy() error
// Exists checks wether the Environment exists or not
Exists() bool
// ReCreate recreates the environment by first Destroying and then Creating
ReCreate() error
}
type BaseEnvironment struct {
}
func (env *BaseEnvironment) Create() error {
return nil
}
func (env *BaseEnvironment) Destroy() error {
return nil
}
func (env *BaseEnvironment) Exists() bool {
return false
}
func (env *BaseEnvironment) ReCreate() error {
if env.Exists() {
if err := env.Destroy(); err != nil {
return err
}
}
return env.Create()
}

0
logs/.gitkeep Normal file
View File

24
main.go Normal file
View File

@ -0,0 +1,24 @@
package main
import (
log "github.com/Sirupsen/logrus"
"github.com/schrej/pterodactyld/config"
"github.com/schrej/pterodactyld/tools"
)
const (
// Version of pterodactyld
Version = "0.0.1-alpha"
)
func main() {
tools.ConfigureLogging()
log.Info("Starting pterodactyld version ", Version)
// Load configuration
log.Info("Loading configuration...")
if err := config.LoadConfiguration(); err != nil {
log.WithError(err).Fatal("Failed to find configuration file")
}
}

7
services/service.go Normal file
View File

@ -0,0 +1,7 @@
package services
import "github.com/schrej/pterodactyld/environments"
type Service struct {
Environment environments.Environment
}

31
tools/logging.go Normal file
View File

@ -0,0 +1,31 @@
package tools
import (
"time"
log "github.com/Sirupsen/logrus"
rotatelogs "github.com/lestrrat/go-file-rotatelogs"
"github.com/rifflock/lfshook"
)
func ConfigureLogging() {
log.SetFormatter(&log.TextFormatter{
DisableTimestamp: true,
})
path := "logs/"
writer := rotatelogs.New(
path+"pterodactyld.%Y%m%d-%H%M.log",
rotatelogs.WithLinkName(path),
rotatelogs.WithMaxAge(time.Duration(86400)*time.Second),
rotatelogs.WithRotationTime(time.Duration(604800)*time.Second),
)
log.AddHook(lfshook.NewHook(lfshook.WriterMap{
log.InfoLevel: writer,
log.ErrorLevel: writer,
log.FatalLevel: writer,
log.PanicLevel: writer,
}))
}