first commit
This commit is contained in:
258
config/config.go
Normal file
258
config/config.go
Normal file
@ -0,0 +1,258 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"arno/skeletor/entity"
|
||||
"arno/skeletor/tool"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/teacat/noire"
|
||||
ini "gopkg.in/ini.v1"
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
AppDebug bool
|
||||
AppVersion int
|
||||
AppPrivate bool
|
||||
AppServer string
|
||||
AppWeburl string
|
||||
AppAlias string
|
||||
AppSecret string
|
||||
AppName string
|
||||
AppSubname string
|
||||
AppDescription string
|
||||
AppTheme string
|
||||
AppLogodark string
|
||||
AppLogolight string
|
||||
AppColorbgbodydark string
|
||||
AppColorbgbodylight string
|
||||
AppColorftbodydark string
|
||||
AppColorftbodylight string
|
||||
AppColorfttitledark string
|
||||
AppColorfttitlelight string
|
||||
AppFontbody string
|
||||
AppFonttitle string
|
||||
AppFontsizeh1 string
|
||||
AppFontsizeh2 string
|
||||
AppFontsizeh3 string
|
||||
AppFontsizeh4 string
|
||||
AppFontsize string
|
||||
|
||||
AppColorbgbodylightdarker string
|
||||
AppColorbgbodydarkdarker string
|
||||
AppColorfttitlelightdarker string
|
||||
AppColorfttitledarkdarker string
|
||||
|
||||
AppRoutes map[string]string
|
||||
AppDbpath string
|
||||
}
|
||||
|
||||
func NewConfig(filepath string, withlog bool) (*Config, *gorm.DB, error) {
|
||||
// CHARGEMENT
|
||||
if withlog {
|
||||
tool.LogInfo("Chargement du fichier de configuration")
|
||||
}
|
||||
|
||||
myconfig := NewConfigdefault()
|
||||
cfg, err := ini.Load(filepath)
|
||||
if err != nil {
|
||||
tool.LogFatal(err.Error())
|
||||
panic("exit")
|
||||
}
|
||||
cfg.ValueMapper = os.ExpandEnv
|
||||
if err := cfg.MapTo(myconfig); err != nil {
|
||||
tool.LogFatal(err.Error())
|
||||
panic("exit")
|
||||
}
|
||||
|
||||
// DECLARATION ROUTES
|
||||
if withlog {
|
||||
tool.LogInfo("Déclaration des routes")
|
||||
}
|
||||
myconfig.AppRoutes["home"] = "/"
|
||||
myconfig.AppRoutes["homeconfig"] = "/admin"
|
||||
|
||||
myconfig.AppRoutes["securitylogin"] = "/login"
|
||||
myconfig.AppRoutes["securitylogout"] = "/logout"
|
||||
|
||||
myconfig.AppRoutes["upload"] = "/user/upload/"
|
||||
myconfig.AppRoutes["uploaded"] = "/user/uploaded/"
|
||||
myconfig.AppRoutes["uploadcrop"] = "/user/uploadcrop/"
|
||||
myconfig.AppRoutes["uploadcropped"] = "/user/uploadcropped/"
|
||||
|
||||
myconfig.AppRoutes["configlist"] = "/admin/config"
|
||||
myconfig.AppRoutes["configrefresh"] = "/admin/config/refresh"
|
||||
myconfig.AppRoutes["configupdate"] = "/admin/config/update/"
|
||||
myconfig.AppRoutes["configdelete"] = "/admin/config/delete/"
|
||||
|
||||
myconfig.AppRoutes["userlist"] = "/admin/user"
|
||||
myconfig.AppRoutes["usersubmit"] = "/admin/user/submit"
|
||||
myconfig.AppRoutes["userupdate"] = "/admin/user/update/"
|
||||
myconfig.AppRoutes["userdelete"] = "/admin/user/delete/"
|
||||
myconfig.AppRoutes["userprofil"] = "/user/profil"
|
||||
|
||||
// CONNEXION A LA BASE DONNEES
|
||||
if withlog {
|
||||
tool.LogInfo("Connexion à la base de données")
|
||||
}
|
||||
mydb, err := gorm.Open(mysql.Open(myconfig.AppDbpath), &gorm.Config{Logger: logger.Default.LogMode(logger.Silent)})
|
||||
if err != nil {
|
||||
tool.LogFatal(err.Error())
|
||||
panic("exit")
|
||||
}
|
||||
|
||||
// Migrate the schema
|
||||
if withlog {
|
||||
tool.LogInfo("Migration des schémas")
|
||||
}
|
||||
mydb.AutoMigrate(&entity.Config{})
|
||||
mydb.AutoMigrate(&entity.User{})
|
||||
mydb.AutoMigrate(&entity.Command{})
|
||||
mydb.AutoMigrate(&entity.Cron{})
|
||||
|
||||
if withlog {
|
||||
tool.LogInfo("Initialisation de la table de configuration")
|
||||
}
|
||||
newConfig(mydb, "AppName", "Titre de votre site", "", myconfig.AppName, 1, true, true, false, "string", "", "site", "Titre de votre site")
|
||||
newConfig(mydb, "AppSubname", "Sous-titre de votre site", "", "", 2, true, true, false, "string", "", "site", "Sous-titre de votre site")
|
||||
newConfig(mydb, "AppDescription", "Description de votre site", "", "", 3, true, true, false, "editor", "", "site", "Description de votre site")
|
||||
newConfig(mydb, "AppTheme", "Thème de votre site", "", "", 100, true, true, false, "themes", "", "site", "Thème de votre site")
|
||||
newConfig(mydb, "AppLogodark", "Logo sur fond fonçé", "", "logo.png", 200, true, true, false, "logo", "", "logo", "Logo sur fond fonçé")
|
||||
newConfig(mydb, "AppLogolight", "Logo sur fond clair", "", "logo.png", 201, true, true, false, "logo", "", "logo", "Logo sur fond clair")
|
||||
newConfig(mydb, "AppColorbgbodydark", "Couleur de fond fonçée", "", "#27848e", 300, true, true, false, "color", "", "colorbgbody", "La couleur de fond quand le site a besoin d'avoir une couleur de fond foncée")
|
||||
newConfig(mydb, "AppColorbgbodylight", "Couleur de fond claire", "", "#ffffff", 301, true, true, false, "color", "", "colorbgbody", "La couleur de fond quand le site a besoin d'avoir une couleur de fond claire")
|
||||
newConfig(mydb, "AppColorftbodydark", "Couleur de la police sur fond fonçé", "", "#ffffff", 302, true, true, false, "color", "", "colorftbody", "La couleur de la police sur fond fonçé")
|
||||
newConfig(mydb, "AppColorftbodylight", "Couleur de la police sur fond claire", "", "#343a40", 303, true, true, false, "color", "", "colorftbody", "La couleur de la police sur fond claire")
|
||||
newConfig(mydb, "AppColorfttitledark", "Couleur des titres sur fond fonçé", "", "#ffffff", 304, true, true, false, "color", "", "colorfttitle", "La couleur des titres sur fond fonçé")
|
||||
newConfig(mydb, "AppColorfttitlelight", "Couleur des titres sur fond claire", "", "#27848e", 305, true, true, false, "color", "", "colorfttitle", "La couleur des titres sur fond claire")
|
||||
newConfig(mydb, "AppFontbody", "Police principale", "", "Roboto-Regular", 400, true, true, false, "font", "", "font", "Nom de la police principale")
|
||||
newConfig(mydb, "AppFonttitle", "Police pour les titres", "", "FredokaOne-Regular", 401, true, true, false, "font", "", "font", "La couleur de la police de votre site")
|
||||
newConfig(mydb, "AppFontsizeh1", "Taille des titres h1", "", "40", 402, true, true, false, "integer", "", "font", "Taille des titres h1 en px")
|
||||
newConfig(mydb, "AppFontsizeh2", "Taille des titres h2", "", "32", 403, true, true, false, "integer", "", "font", "Taille des titres h2 en px")
|
||||
newConfig(mydb, "AppFontsizeh3", "Taille des titres h3", "", "28", 404, true, true, false, "integer", "", "font", "Taille des titres h3 en px")
|
||||
newConfig(mydb, "AppFontsizeh4", "Taille des titres h4", "", "24", 405, true, true, false, "integer", "", "font", "Taille des titres h4 en px")
|
||||
newConfig(mydb, "AppFontsize", "Taille du texte", "", "16", 406, true, true, false, "integer", "", "font", "Taille du texte px")
|
||||
|
||||
if withlog {
|
||||
tool.LogInfo("Chargement de la table de configuration en mémoire")
|
||||
}
|
||||
myconfig.Refresh(mydb)
|
||||
|
||||
if withlog {
|
||||
tool.LogInfo("Initialisation des Commandes")
|
||||
}
|
||||
newCommand(mydb, "AppSetPassword", "Définir un password pour un utilisateur\ngo run cmd/AppSetPassword loginuser newpaspassord")
|
||||
newCommand(mydb, "AppClear", "Nettoyage des fichiers obsolètes\ngo run cmd/AppClear")
|
||||
|
||||
if withlog {
|
||||
tool.LogInfo("Initialisation des Jobs Cron")
|
||||
}
|
||||
newCron(mydb, "AppClear", "@every 4h00m")
|
||||
|
||||
return myconfig, mydb, nil
|
||||
}
|
||||
|
||||
func NewConfigdefault() *Config {
|
||||
return &Config{
|
||||
AppDebug: false,
|
||||
AppPrivate: true,
|
||||
AppServer: ":3000",
|
||||
AppWeburl: "",
|
||||
AppAlias: "/",
|
||||
AppSecret: "changeme",
|
||||
AppName: "Skeletor",
|
||||
AppRoutes: make(map[string]string),
|
||||
}
|
||||
}
|
||||
|
||||
func newConfig(mydb *gorm.DB, id string, title string, value string, defaultvalue string, roworder int, visible bool, changeable bool, required bool, typefield string, grouped string, category string, help string) {
|
||||
var config entity.Config
|
||||
result := mydb.First(&config, "id = ?", id)
|
||||
if result.RowsAffected == 0 {
|
||||
config.Id = id
|
||||
config.Value = value
|
||||
mydb.Create(&config)
|
||||
}
|
||||
|
||||
mydb.First(&config, "id = ?", id)
|
||||
config.Title = title
|
||||
config.Defaultvalue = defaultvalue
|
||||
config.Roworder = roworder
|
||||
config.Visible = visible
|
||||
config.Changeable = changeable
|
||||
config.Required = required
|
||||
config.Typefield = typefield
|
||||
config.Grouped = grouped
|
||||
config.Category = category
|
||||
config.Help = help
|
||||
mydb.Save(&config)
|
||||
}
|
||||
|
||||
func GetConfig(mydb *gorm.DB, id string) string {
|
||||
var myconfig entity.Config
|
||||
mydb.First(&myconfig, "id = ?", id)
|
||||
if myconfig.Value == "" {
|
||||
return myconfig.Defaultvalue
|
||||
} else {
|
||||
return myconfig.Value
|
||||
}
|
||||
}
|
||||
|
||||
func (myconfig *Config) Refresh(mydb *gorm.DB) {
|
||||
myconfig.AppName = GetConfig(mydb, "AppName")
|
||||
myconfig.AppSubname = GetConfig(mydb, "AppSubname")
|
||||
myconfig.AppDescription = GetConfig(mydb, "AppDescription")
|
||||
myconfig.AppTheme = GetConfig(mydb, "AppTheme")
|
||||
myconfig.AppLogodark = GetConfig(mydb, "AppLogodark")
|
||||
myconfig.AppLogolight = GetConfig(mydb, "AppLogolight")
|
||||
myconfig.AppColorbgbodydark = GetConfig(mydb, "AppColorbgbodydark")
|
||||
myconfig.AppColorbgbodylight = GetConfig(mydb, "AppColorbgbodylight")
|
||||
myconfig.AppColorftbodydark = GetConfig(mydb, "AppColorftbodydark")
|
||||
myconfig.AppColorftbodylight = GetConfig(mydb, "AppColorftbodylight")
|
||||
myconfig.AppColorfttitledark = GetConfig(mydb, "AppColorfttitledark")
|
||||
myconfig.AppColorfttitlelight = GetConfig(mydb, "AppColorfttitlelight")
|
||||
myconfig.AppFontbody = GetConfig(mydb, "AppFontbody")
|
||||
myconfig.AppFonttitle = GetConfig(mydb, "AppFonttitle")
|
||||
myconfig.AppFontsizeh1 = GetConfig(mydb, "AppFontsizeh1")
|
||||
myconfig.AppFontsizeh2 = GetConfig(mydb, "AppFontsizeh2")
|
||||
myconfig.AppFontsizeh3 = GetConfig(mydb, "AppFontsizeh3")
|
||||
myconfig.AppFontsizeh4 = GetConfig(mydb, "AppFontsizeh4")
|
||||
myconfig.AppFontsize = GetConfig(mydb, "AppFontsize")
|
||||
|
||||
myconfig.AppColorbgbodydarkdarker = "#" + noire.NewHex(strings.Replace(myconfig.AppColorbgbodydark, "#", "", -1)).Darken(0.10).Hex()
|
||||
myconfig.AppColorfttitledarkdarker = "#" + noire.NewHex(strings.Replace(myconfig.AppColorfttitledark, "#", "", -1)).Darken(0.30).Hex()
|
||||
|
||||
myconfig.AppColorbgbodylightdarker = "#" + noire.NewHex(strings.Replace(myconfig.AppColorbgbodylight, "#", "", -1)).Darken(0.10).Hex()
|
||||
myconfig.AppColorfttitlelightdarker = "#" + noire.NewHex(strings.Replace(myconfig.AppColorfttitlelight, "#", "", -1)).Darken(0.10).Hex()
|
||||
|
||||
}
|
||||
|
||||
func newCommand(mydb *gorm.DB, name string, description string) {
|
||||
var command entity.Command
|
||||
result := mydb.First(&command, "name = ?", name)
|
||||
if result.RowsAffected == 0 {
|
||||
command.Name = name
|
||||
mydb.Create(&command)
|
||||
}
|
||||
|
||||
mydb.First(&command, "name = ?", name)
|
||||
command.Description = description
|
||||
mydb.Save(&command)
|
||||
}
|
||||
|
||||
func newCron(mydb *gorm.DB, name string, every string) {
|
||||
var cron entity.Cron
|
||||
var command entity.Command
|
||||
|
||||
mydb.First(&command, "name = ?", name)
|
||||
|
||||
result := mydb.First(&cron, "command_id = ?", command.Id)
|
||||
if result.RowsAffected == 0 {
|
||||
cron.CommandId = command.Id
|
||||
cron.Every = every
|
||||
mydb.Create(&cron)
|
||||
}
|
||||
}
|
8
config/config.ini
Normal file
8
config/config.ini
Normal file
@ -0,0 +1,8 @@
|
||||
AppDebug = false
|
||||
AppPrivate = true
|
||||
AppServer = ninegate.ac-arno.fr:3000
|
||||
AppWeburl = http://ninegate.ac-arno.fr:3000
|
||||
AppAlias = "/"
|
||||
AppSecret = changeme
|
||||
AppName = Janus
|
||||
AppDbpath = admin:eole@tcp(localhost:3306)/mygobdd
|
38
config/provider.go
Normal file
38
config/provider.go
Normal file
@ -0,0 +1,38 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"arno/skeletor/service"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
const ServiceName service.Name = "config"
|
||||
|
||||
func ServiceProvider(config *Config) service.Provider {
|
||||
return func(ctn *service.Container) (interface{}, error) {
|
||||
return config, nil
|
||||
}
|
||||
}
|
||||
|
||||
func From(container *service.Container) (*Config, error) {
|
||||
service, err := container.Service(ServiceName)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "error while retrieving '%s' service", ServiceName)
|
||||
}
|
||||
|
||||
srv, ok := service.(*Config)
|
||||
if !ok {
|
||||
return nil, errors.Errorf("retrieved service is not a valid '%s' service", ServiceName)
|
||||
}
|
||||
|
||||
return srv, nil
|
||||
}
|
||||
|
||||
func Must(container *service.Container) *Config {
|
||||
srv, err := From(container)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return srv
|
||||
}
|
Reference in New Issue
Block a user