1
0
Fork 0
mirror of https://github.com/Luzifer/mondash.git synced 2024-12-23 04:21:18 +00:00

Remove S3

S3 storage becomes really expensive. The network latency is also worse
than local file latency.
This commit is contained in:
zainhoda 2015-03-12 01:13:45 -04:00
parent 3f7e71c0c9
commit ba4ccee98c
3 changed files with 21 additions and 17 deletions

23
main.go
View file

@ -5,13 +5,13 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"os" //"os"
"time" "time"
"log" //"log"
"launchpad.net/goamz/aws" //"launchpad.net/goamz/aws"
"launchpad.net/goamz/s3" //"launchpad.net/goamz/s3"
"github.com/flosch/pongo2" "github.com/flosch/pongo2"
"github.com/go-martini/martini" "github.com/go-martini/martini"
@ -20,18 +20,19 @@ import (
) )
var templates = make(map[string]*pongo2.Template) var templates = make(map[string]*pongo2.Template)
var s3Storage *s3.Bucket
//var s3Storage *s3.Bucket
func main() { func main() {
preloadTemplates() preloadTemplates()
// Initialize S3 storage // Initialize S3 storage
awsAuth, err := aws.EnvAuth() //awsAuth, err := aws.EnvAuth()
if err != nil { //if err != nil {
log.Fatal(err) // log.Fatal(err)
} //}
s3Conn := s3.New(awsAuth, aws.USEast) //s3Conn := s3.New(awsAuth, aws.USEast)
s3Storage = s3Conn.Bucket(os.Getenv("S3Bucket")) //s3Storage = s3Conn.Bucket(os.Getenv("S3Bucket"))
m := martini.Classic() m := martini.Classic()

View file

@ -3,8 +3,10 @@ package main
import ( import (
"encoding/json" "encoding/json"
"errors" "errors"
"launchpad.net/goamz/s3" "io/ioutil"
//"launchpad.net/goamz/s3"
"log" "log"
//"os"
"sort" "sort"
"strconv" "strconv"
"time" "time"
@ -17,7 +19,7 @@ type dashboard struct {
} }
func loadDashboard(dashid string) (*dashboard, error) { func loadDashboard(dashid string) (*dashboard, error) {
data, err := s3Storage.Get(dashid) data, err := ioutil.ReadFile(dashid + ".txt")
if err != nil { if err != nil {
return &dashboard{}, errors.New("Dashboard not found") return &dashboard{}, errors.New("Dashboard not found")
} }
@ -34,7 +36,8 @@ func (d *dashboard) Save() {
log.Printf("Error while marshalling dashboard: %s", err) log.Printf("Error while marshalling dashboard: %s", err)
return return
} }
err = s3Storage.Put(d.DashboardID, data, "application/json", s3.Private) err = ioutil.WriteFile(d.DashboardID+".txt", data, 0600)
if err != nil { if err != nil {
log.Printf("Error while storing dashboard: %s", err) log.Printf("Error while storing dashboard: %s", err)
} }

View file

@ -19,10 +19,10 @@ func handleRedirectWelcome(res http.ResponseWriter, req *http.Request) {
func handleCreateRandomDashboard(res http.ResponseWriter, req *http.Request) { func handleCreateRandomDashboard(res http.ResponseWriter, req *http.Request) {
urlProposal := generateAPIKey()[0:20] urlProposal := generateAPIKey()[0:20]
_, err := s3Storage.Get(urlProposal) _, err := ioutil.ReadFile(urlProposal + ".txt")
for err == nil { for err == nil {
urlProposal = generateAPIKey()[0:20] urlProposal = generateAPIKey()[0:20]
_, err = s3Storage.Get(urlProposal) _, err = ioutil.ReadFile(urlProposal + ".txt")
} }
http.Redirect(res, req, fmt.Sprintf("/%s", urlProposal), http.StatusTemporaryRedirect) http.Redirect(res, req, fmt.Sprintf("/%s", urlProposal), http.StatusTemporaryRedirect)
} }
@ -62,7 +62,7 @@ func handleDeleteDashboard(params martini.Params, req *http.Request, res http.Re
return return
} }
_ = s3Storage.Del(params["dashid"]) //_ = s3Storage.Del(params["dashid"])
http.Error(res, "OK", http.StatusOK) http.Error(res, "OK", http.StatusOK)
} }