1
0
Fork 0
mirror of https://github.com/Luzifer/mondash.git synced 2024-11-10 00:20:02 +00:00
mondash/main.go

81 lines
1.7 KiB
Go
Raw Normal View History

2015-02-07 18:32:44 +00:00
package main
import (
"crypto/md5"
"fmt"
"io/ioutil"
"net/http"
"os"
2015-02-07 18:32:44 +00:00
"time"
"log"
2015-02-07 18:32:44 +00:00
"launchpad.net/goamz/aws"
"launchpad.net/goamz/s3"
2015-02-07 18:32:44 +00:00
"github.com/flosch/pongo2"
"github.com/go-martini/martini"
_ "github.com/flosch/pongo2-addons"
)
var templates = make(map[string]*pongo2.Template)
var s3Storage *s3.Bucket
2015-02-07 18:32:44 +00:00
func main() {
preloadTemplates()
// Initialize S3 storage
awsAuth, err := aws.EnvAuth()
if err != nil {
log.Fatal(err)
}
s3Conn := s3.New(awsAuth, aws.EUWest)
s3Storage = s3Conn.Bucket(os.Getenv("S3Bucket"))
2015-02-07 18:32:44 +00:00
m := martini.Classic()
// Assets are in assets folder
m.Use(martini.Static("assets", martini.StaticOptions{Prefix: "/assets"}))
// Real handlers
m.Get("/", handleRedirectWelcome)
m.Get("/create", handleCreateRandomDashboard)
m.Get("/:dashid", handleDisplayDashboard)
m.Put("/:dashid/:metricid", handlePutMetric)
m.Delete("/:dashid", handleDeleteDashboard)
m.Delete("/:dashid/:metricid", handleDeleteMetric)
2015-02-07 18:32:44 +00:00
2015-02-20 18:41:36 +00:00
go runWelcomePage()
2015-02-07 18:32:44 +00:00
// GO!
m.Run()
}
func generateAPIKey() string {
t := time.Now().String()
sum := md5.Sum([]byte(t))
return fmt.Sprintf("%x", sum)
}
func renderTemplate(templateName string, context pongo2.Context, res http.ResponseWriter) {
if tpl, ok := templates[templateName]; ok {
2015-02-20 18:47:36 +00:00
_ = tpl.ExecuteWriter(context, res)
2015-02-07 18:32:44 +00:00
} else {
res.WriteHeader(http.StatusInternalServerError)
2015-02-20 18:47:36 +00:00
_, _ = res.Write([]byte(fmt.Sprintf("Template %s not found!", templateName)))
2015-02-07 18:32:44 +00:00
}
}
func preloadTemplates() {
templateNames, err := ioutil.ReadDir("templates")
if err != nil {
panic("Templates directory not available!")
}
for _, tplname := range templateNames {
templates[tplname.Name()] = pongo2.Must(pongo2.FromFile(fmt.Sprintf("templates/%s", tplname.Name())))
}
}