Esempio n. 1
0
func readFiles(files ...string) []byte {
	lfs := len(files)
	chContent := make(chan []byte, lfs)

	for _, file := range files {
		go func(chContent chan []byte, configPath string) {
			chContent <- format(configPath)
		}(chContent, file)
	}

	bytess := make([][]byte, 0, lfs)

	for i := 1; i <= lfs; i++ {
		content := <-chContent

		if len(content) != 0 {
			bytess = append(bytess, content)
		}
	}

	buf := bytes.NewBufferString(`{`)
	buf.Write(bytes.Join(bytess, []byte(`,`)))
	buf.WriteString(`}`)

	var contentBuf bytes.Buffer
	err := json.Compact(&contentBuf, buf.Bytes())
	if err != nil {
		log.Debug("<readFiles> jsonData: ", buf.String())
		log.Fatal("<readFiles> error: ", err)
	}

	return contentBuf.Bytes()
}
Esempio n. 2
0
func Dir(configDir string) *Config {
	configDir = filepath.Clean(configDir)
	fis, err := ioutil.ReadDir(configDir)
	if err != nil {
		log.Fatal("<Dir> error: ", err)
	}

	var files []string
	for _, fi := range fis {
		fileName := fi.Name()
		if fi.IsDir() || fileName[0] == ignoreFirst {
			continue
		}

		files = append(files, filepath.Join(configDir, fileName))
	}

	conf := loadFiles(files...)
	mu.Lock()
	conf.dataType = "directory"
	conf.target = configDir
	mu.Unlock()

	return conf
}
Esempio n. 3
0
func (c *Config) format(configPath string) []byte {
	data, err := ioutil.ReadFile(configPath)
	if err != nil {
		log.Fatal("<Config.format> error: ", err)
	}

	return bytes.TrimSpace(regexpSpace.ReplaceAll(regexpNote.ReplaceAll(data, []byte(``)), []byte(``)))
}
Esempio n. 4
0
func (c *Config) Load(configDir string) {
	conf := config.Dir(configDir).Load(c)
	configDir = conf.Target()
	if dataFi, err := os.Stat(configDir); err == nil {
		c.set(configDir, dataFi.ModTime().Unix())
		c.preSet()
	} else {
		log.Fatal("<Config.Load> error:", err)
	}
}
Esempio n. 5
0
func (c *Config) load(data []byte) {
	err := json.Unmarshal(data, c)
	if err != nil {
		log.Debug("<Config.Load> jsonData: ", string(data))
		log.Fatal("<Config.Load> error: ", err)
	}

	c.UploadDirectory = c.AssetsDirectory + c.StaticDirectory + c.UploadDirectory
	c.ThemeDirectory = c.ThemeDirectory + c.Theme + "/"
	c.StaticCssDirectory = c.AssetsDirectory + c.StaticDirectory + c.ThemeDirectory + c.StaticCssDirectory
	c.StaticJsDirectory = c.AssetsDirectory + c.StaticDirectory + c.ThemeDirectory + c.StaticJsDirectory
	c.StaticImgDirectory = c.AssetsDirectory + c.StaticDirectory + c.ThemeDirectory + c.StaticImgDirectory
}
Esempio n. 6
0
func (c *Config) Load(i interface{}) *Config {
	mu.RLock()
	data := c.data
	mu.RUnlock()

	err := json.Unmarshal(data, i)
	if err != nil {
		log.Debug("<Config.Load> jsonData: ", c.String())
		log.Fatal("<Config.Load> error: ", err)
	}

	return c
}
Esempio n. 7
0
func Data(data string) *Config {
	data = `{` + data + `}`
	var buf bytes.Buffer
	err := json.Compact(&buf, []byte(data))
	if err != nil {
		log.Debug("<Data> jsonData: ", data)
		log.Fatal("<Data> error: ", err)
	}

	return &Config{
		dataType: "data",
		data:     buf.Bytes(),
	}
}
Esempio n. 8
0
func Glob(pattern string) *Config {
	files, err := filepath.Glob(pattern)
	if err != nil {
		log.Fatal("<Glob> error: ", err)
	}

	conf := loadFiles(files...)

	mu.Lock()
	conf.dataType = "glob"
	conf.target = pattern
	mu.Unlock()

	return conf
}
Esempio n. 9
0
func (p *Page) ListenAndServe(addr string, i interface{}) {
	if p.Config.SupportStatic {
		p.handleRootStatic(p.Config.RootStaticFiles)
		p.handleStatic()
	}

	if p.Config.AccessHtml {
		p.handleStaticHtml()
	}

	p.handleRoute(i)

	err := http.ListenAndServe(addr, nil)
	if err != nil {
		log.Fatal("<Page.ListenAndServe> ", err)
	}
}
Esempio n. 10
0
func (c *Config) readDir(configDir string) []byte {
	fis, err := ioutil.ReadDir(configDir)
	if err != nil {
		log.Fatal("<Config.readDir> error: ", err)
	}

	lfis := len(fis)
	chContent := make(chan []byte, lfis)

	for _, fi := range fis {
		fiName := fi.Name()
		if fi.IsDir() || fiName[0] == '.' {
			lfis--
			continue
		}

		go func(chContent chan []byte, configPath string) {
			chContent <- c.format(configPath)
		}(chContent, configDir+"/"+fiName)
	}

	contentBuf := bytes.NewBufferString(`{`)
	for i := 1; i <= lfis; i++ {
		content := <-chContent
		if len(content) == 0 {
			continue
		}

		contentBuf.Write(content)
		if i < lfis {
			contentBuf.WriteString(",")
		}
	}

	contentBuf.WriteString(`}`)

	return contentBuf.Bytes()
}