diff --git a/main.go b/main.go index 6e68d7f..d297301 100644 --- a/main.go +++ b/main.go @@ -5,13 +5,13 @@ import ( "fmt" "io/ioutil" "net/http" - //"os" + "os" "time" - //"log" + "log" - //"launchpad.net/goamz/aws" - //"launchpad.net/goamz/s3" + "launchpad.net/goamz/aws" + "launchpad.net/goamz/s3" "github.com/flosch/pongo2" "github.com/go-martini/martini" @@ -20,19 +20,18 @@ import ( ) var templates = make(map[string]*pongo2.Template) - -//var s3Storage *s3.Bucket +var s3Storage *s3.Bucket func main() { preloadTemplates() // Initialize S3 storage - //awsAuth, err := aws.EnvAuth() - //if err != nil { - // log.Fatal(err) - //} - //s3Conn := s3.New(awsAuth, aws.USEast) - //s3Storage = s3Conn.Bucket(os.Getenv("S3Bucket")) + awsAuth, err := aws.EnvAuth() + if err != nil { + log.Fatal(err) + } + s3Conn := s3.New(awsAuth, aws.USEast) + s3Storage = s3Conn.Bucket(os.Getenv("S3Bucket")) m := martini.Classic() diff --git a/structs.go b/structs.go index ff2c417..f8c9c2d 100644 --- a/structs.go +++ b/structs.go @@ -3,10 +3,8 @@ package main import ( "encoding/json" "errors" - "io/ioutil" - //"launchpad.net/goamz/s3" + "launchpad.net/goamz/s3" "log" - //"os" "sort" "strconv" "time" @@ -19,7 +17,7 @@ type dashboard struct { } func loadDashboard(dashid string) (*dashboard, error) { - data, err := ioutil.ReadFile(dashid + ".txt") + data, err := s3Storage.Get(dashid) if err != nil { return &dashboard{}, errors.New("Dashboard not found") } @@ -36,8 +34,7 @@ func (d *dashboard) Save() { log.Printf("Error while marshalling dashboard: %s", err) return } - err = ioutil.WriteFile(d.DashboardID+".txt", data, 0600) - + err = s3Storage.Put(d.DashboardID, data, "application/json", s3.Private) if err != nil { log.Printf("Error while storing dashboard: %s", err) } diff --git a/web_handlers.go b/web_handlers.go index b19e843..f7f2ad4 100644 --- a/web_handlers.go +++ b/web_handlers.go @@ -19,10 +19,10 @@ func handleRedirectWelcome(res http.ResponseWriter, req *http.Request) { func handleCreateRandomDashboard(res http.ResponseWriter, req *http.Request) { urlProposal := generateAPIKey()[0:20] - _, err := ioutil.ReadFile(urlProposal + ".txt") + _, err := s3Storage.Get(urlProposal) for err == nil { urlProposal = generateAPIKey()[0:20] - _, err = ioutil.ReadFile(urlProposal + ".txt") + _, err = s3Storage.Get(urlProposal) } 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 } - //_ = s3Storage.Del(params["dashid"]) + _ = s3Storage.Del(params["dashid"]) http.Error(res, "OK", http.StatusOK) }