forked from saturn4er/beego-assets
-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
81 lines (69 loc) · 2.45 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package beegoAssets
import (
"fmt"
"strings"
"github.com/beego/beego"
"github.com/beego/beego/config"
)
type assetPipelineConfig struct {
Runmode string
// Paths to assets
AssetsLocations []string
// Paths to js/css files
PublicDirs []string
// Path to store compiled assets
TempDir string
// Flags
MinifyCSS bool
MinifyJS bool
CombineCSS bool
CombineJS bool
ProductionMode bool
// Association of assetsType->Array of extensions
extensions map[assetsType][]string
// callbacks
preLoadCallbacks map[assetsType][]preLoadCallback
preBuildCallbacks map[assetsType][]preAfterBuildCallback
minifyCallbacks map[string]minifyFileCallback
afterBuildCallbacks map[assetsType][]preAfterBuildCallback
}
// Config - assets pipeline configuration
var Config assetPipelineConfig
func init() {
Config.Parse("./conf/asset-pipeline.conf")
Config.extensions = map[assetsType][]string{}
Config.preBuildCallbacks = map[assetsType][]preAfterBuildCallback{}
Config.minifyCallbacks = map[string]minifyFileCallback{}
Config.afterBuildCallbacks = map[assetsType][]preAfterBuildCallback{}
Config.preLoadCallbacks = map[assetsType][]preLoadCallback{}
}
func (a *assetPipelineConfig) Parse(filename string) {
config, err := config.NewConfig("ini", filename)
if err != nil {
fmt.Println(err)
return
}
Config.Runmode = beego.AppConfig.DefaultString("runmode", "dev")
locations := config.DefaultString("assets_dirs", "")
Config.AssetsLocations = strings.Split(locations, ",")
publicDirs := config.DefaultString("public_dirs", "")
Config.PublicDirs = strings.Split(publicDirs, ",")
Config.TempDir = config.DefaultString("temp_dir", "static/assets")
runmodeParams, err := config.GetSection(Config.Runmode)
if err != nil {
logger.Warn("Can't get section \"%v\" from config Asset-pipeline.conf. Using default params", Config.Runmode)
}
getBoolFromMap(&runmodeParams, "minify_css", &Config.MinifyCSS, false)
getBoolFromMap(&runmodeParams, "minify_js", &Config.MinifyJS, false)
getBoolFromMap(&runmodeParams, "combine_css", &Config.CombineCSS, false)
getBoolFromMap(&runmodeParams, "combine_js", &Config.CombineJS, false)
getBoolFromMap(&runmodeParams, "production_mode", &Config.ProductionMode, false)
}
func getBoolFromMap(array *map[string]string, key string, variable *bool, defaultValue bool) {
if val, ok := (*array)[key]; ok {
_val := strings.ToLower(val)
*variable = _val == "true" || _val == "1"
} else {
*variable = defaultValue
}
}