parent
d7b717feed
commit
1334a4adea
6
.drone.yml
Normal file
6
.drone.yml
Normal file
@ -0,0 +1,6 @@
|
||||
pipeline:
|
||||
build:
|
||||
image: golang
|
||||
commands:
|
||||
- go get github.com/oxtoacart/bpool
|
||||
- go build
|
118
gowebframework.go
Normal file
118
gowebframework.go
Normal file
@ -0,0 +1,118 @@
|
||||
/*
|
||||
* @Author: Bartuccio Antoine
|
||||
* @Date: 2018-07-14 11:32:11
|
||||
* @Last Modified by: klmp200
|
||||
* @Last Modified time: 2018-07-14 12:36:26
|
||||
*/
|
||||
|
||||
package gowebframework
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/oxtoacart/bpool"
|
||||
"html/template"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
var templates map[string]*template.Template
|
||||
var buffer_pool *bpool.BufferPool
|
||||
|
||||
type Config struct {
|
||||
TemplateIncludePath string
|
||||
TemplateLayoutPath string
|
||||
MainTemplate string
|
||||
StaticFolderPath string
|
||||
TemplateExtensionPattern string
|
||||
ServerPort string
|
||||
Domain string
|
||||
Debug bool
|
||||
}
|
||||
|
||||
var ServerConfig Config
|
||||
|
||||
func Configure(config_file_name string, custom_config_file_name string) {
|
||||
loadConfiguration(config_file_name, custom_config_file_name)
|
||||
loadTemplates()
|
||||
}
|
||||
|
||||
func loadConfiguration(config_file_name string, custom_config_file_name string) {
|
||||
default_settings, err := ioutil.ReadFile(config_file_name)
|
||||
if err != nil {
|
||||
log.Fatal("No " + config_file_name + " found, exiting")
|
||||
}
|
||||
log.Println("Importing settings")
|
||||
err = json.Unmarshal(default_settings, &ServerConfig)
|
||||
if err != nil {
|
||||
log.Fatal("Malformed " + config_file_name)
|
||||
}
|
||||
|
||||
custom_settings, err := ioutil.ReadFile(custom_config_file_name)
|
||||
if err != nil {
|
||||
log.Println("No settings_custom.json fonud, skipping")
|
||||
return
|
||||
}
|
||||
log.Println("Importing custom settings")
|
||||
err = json.Unmarshal(custom_settings, &ServerConfig)
|
||||
if err != nil {
|
||||
log.Fatal("Malformed " + custom_config_file_name)
|
||||
}
|
||||
}
|
||||
|
||||
func loadTemplates() {
|
||||
if templates == nil {
|
||||
templates = make(map[string]*template.Template)
|
||||
}
|
||||
|
||||
layoutFiles, err := filepath.Glob(ServerConfig.TemplateLayoutPath + ServerConfig.TemplateExtensionPattern)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
includeFiles, err := filepath.Glob(ServerConfig.TemplateIncludePath + ServerConfig.TemplateExtensionPattern)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
mainTemplate := template.New("main")
|
||||
mainTemplate, err = mainTemplate.Parse(ServerConfig.MainTemplate)
|
||||
|
||||
for _, file := range includeFiles {
|
||||
fileName := filepath.Base(file)
|
||||
log.Println("Loading template", fileName)
|
||||
files := append(layoutFiles, file)
|
||||
templates[fileName], err = mainTemplate.Clone()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
templates[fileName] = template.Must(templates[fileName].ParseFiles(files...))
|
||||
}
|
||||
log.Println("Templates loading successful")
|
||||
buffer_pool = bpool.NewBufferPool(64)
|
||||
log.Println("Buffer allocation sucessful")
|
||||
}
|
||||
|
||||
func RenderTemplate(w http.ResponseWriter, name string, data interface{}) {
|
||||
if ServerConfig.Debug {
|
||||
loadTemplates()
|
||||
}
|
||||
|
||||
tmpl, is_ok := templates[name]
|
||||
if !is_ok {
|
||||
http.Error(w, fmt.Sprintf("The template %s does not exist.", name),
|
||||
http.StatusInternalServerError)
|
||||
}
|
||||
buf := buffer_pool.Get()
|
||||
defer buffer_pool.Put(buf)
|
||||
|
||||
err := tmpl.Execute(buf, data)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
buf.WriteTo(w)
|
||||
}
|
Loading…
Reference in New Issue
Block a user