diff --git a/.idea/runConfigurations/run.xml b/.idea/runConfigurations/run.xml new file mode 100644 index 0000000..97b1d3f --- /dev/null +++ b/.idea/runConfigurations/run.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/src/main.go b/src/main.go index b9ccc86..0726762 100644 --- a/src/main.go +++ b/src/main.go @@ -2,11 +2,13 @@ package main import ( "flag" + "fmt" "git.tiburon.su/USEFULL/skimmer/skimmer" ) var ( config = skimmer.Config{ + Address: "localhost:3000", SessionSecret: "secret123", RedisConfig: skimmer.RedisConfig{ RedisAddr: "127.0.0.1:6379", @@ -27,5 +29,6 @@ func init() { func main() { flag.Parse() api := skimmer.GetApi(&config) - api.Run() + fmt.Printf("Open in brouser http://%s\n", config.Address) + api.RunOnAddr(config.Address) } diff --git a/src/skimmer/api.go b/src/skimmer/api.go index d04c62b..6dbc610 100644 --- a/src/skimmer/api.go +++ b/src/skimmer/api.go @@ -1,43 +1,41 @@ package skimmer - import ( + "fmt" "github.com/codegangsta/martini" "github.com/codegangsta/martini-contrib/render" + "github.com/codegangsta/martini-contrib/sessions" "net/http" "strconv" - "github.com/codegangsta/martini-contrib/sessions" - "fmt" ) - - -type ErrorMsg struct{ +type ErrorMsg struct { Error string `json:"error"` } const ( REQUEST_BODY_SIZE = 1024 * 30 MAX_REQUEST_COUNT = 20 - BIN_LIFETIME = 60 * 60 * 24 * 2 + BIN_LIFETIME = 60 * 60 * 24 * 2 ) type RedisConfig struct { - RedisAddr string - RedisPassword string - RedisPrefix string + RedisAddr string + RedisPassword string + RedisPrefix string } type Config struct { - SessionSecret string - Storage string + Address string + SessionSecret string + Storage string RedisConfig } func GetApi(config *Config) *martini.ClassicMartini { var storage Storage - switch config.Storage{ + switch config.Storage { case "redis": redisStorage := NewRedisStorage(config.RedisAddr, config.RedisPassword, config.RedisPassword, MAX_REQUEST_COUNT, BIN_LIFETIME) redisStorage.StartCleaning(60) @@ -47,7 +45,6 @@ func GetApi(config *Config) *martini.ClassicMartini { memoryStorage.StartCleaning(60) storage = memoryStorage - } store := sessions.NewCookieStore([]byte(config.SessionSecret)) @@ -55,104 +52,103 @@ func GetApi(config *Config) *martini.ClassicMartini { api.MapTo(storage, (*Storage)(nil)) api.Use(render.Renderer(render.Options{ - Directory: "public/static/views", + Directory: "public/static/views", Extensions: []string{".html"}, - Delims: render.Delims{"{[{", "}]}"}, + Delims: render.Delims{"{[{", "}]}"}, })) api.Use(sessions.Sessions("my_session", store)) api.Use(NewSessionHistoryHandler(20, "binHistory")) + api.Post("/api/v1/bins/", func(r render.Render, storage Storage, history History, session sessions.Session, req *http.Request) { + payload := Bin{} + if err := DecodeJsonPayload(req, &payload); err != nil { + r.JSON(400, ErrorMsg{fmt.Sprintf("Decoding payload error: %s", err)}) + return + } + bin := NewBin() + if payload.Private { + bin.SetPrivate() + } + if err := storage.CreateBin(bin); err == nil { + history.Add(bin.Name) + if bin.Private { + session.Set(fmt.Sprintf("pr_%s", bin.Name), bin.SecretKey) + } + r.JSON(http.StatusCreated, bin) + } else { + r.JSON(http.StatusInternalServerError, ErrorMsg{err.Error()}) + } + }) - api.Post("/api/v1/bins/", func(r render.Render, storage Storage, history History, session sessions.Session, req *http.Request){ - payload := Bin{} - if err := DecodeJsonPayload(req, &payload); err != nil { - r.JSON(400, ErrorMsg{fmt.Sprintf("Decoding payload error: %s", err)}) - return - } - bin := NewBin() - if payload.Private { - bin.SetPrivate() - } - if err := storage.CreateBin(bin); err == nil { - history.Add(bin.Name) - if bin.Private { - session.Set(fmt.Sprintf("pr_%s", bin.Name), bin.SecretKey) - } - r.JSON(http.StatusCreated, bin) - } else { - r.JSON(http.StatusInternalServerError, ErrorMsg{err.Error()}) - } - }) + api.Get("/api/v1/bins/", func(r render.Render, storage Storage, history History) { + if bins, err := storage.LookupBins(history.All()); err == nil { + r.JSON(http.StatusOK, bins) + } else { + r.JSON(http.StatusInternalServerError, ErrorMsg{err.Error()}) + } + }) - api.Get("/api/v1/bins/", func(r render.Render, storage Storage, history History){ - if bins, err := storage.LookupBins(history.All()); err == nil { - r.JSON(http.StatusOK, bins) + api.Get("/api/v1/bins/:bin", func(r render.Render, params martini.Params, session sessions.Session, storage Storage) { + if bin, err := storage.LookupBin(params["bin"]); err == nil { + if bin.Private && bin.SecretKey != session.Get(fmt.Sprintf("pr_%s", bin.Name)) { + r.JSON(http.StatusForbidden, ErrorMsg{"The bin is private"}) } else { - r.JSON(http.StatusInternalServerError, ErrorMsg{err.Error()}) + r.JSON(http.StatusOK, bin) } - }) - - api.Get("/api/v1/bins/:bin", func(r render.Render, params martini.Params, session sessions.Session, storage Storage){ - if bin, err := storage.LookupBin(params["bin"]); err == nil{ - if bin.Private && bin.SecretKey != session.Get(fmt.Sprintf("pr_%s", bin.Name)){ - r.JSON(http.StatusForbidden, ErrorMsg{"The bin is private"}) - } else { - r.JSON(http.StatusOK, bin) - } - } else { - r.JSON(http.StatusNotFound, ErrorMsg{err.Error()}) - } - }) + } else { + r.JSON(http.StatusNotFound, ErrorMsg{err.Error()}) + } + }) api.Get("/api/v1/bins/:bin/requests/", func(r render.Render, storage Storage, session sessions.Session, - params martini.Params, req *http.Request){ - if bin, error := storage.LookupBin(params["bin"]); error == nil { - if bin.Private && bin.SecretKey != session.Get(fmt.Sprintf("pr_%s", bin.Name)){ - r.JSON(http.StatusForbidden, ErrorMsg{"The bin is private"}) - } else { - from := 0 - to := 20 - if fromVal, err := strconv.Atoi(req.FormValue("from")); err == nil { - from = fromVal - } - if toVal, err := strconv.Atoi(req.FormValue("to")); err == nil { - to = toVal - } - if requests, err := storage.LookupRequests(bin.Name, from, to); err == nil { - r.JSON(http.StatusOK, requests) - } else { - r.JSON(http.StatusInternalServerError, ErrorMsg{err.Error()}) - } + params martini.Params, req *http.Request) { + if bin, error := storage.LookupBin(params["bin"]); error == nil { + if bin.Private && bin.SecretKey != session.Get(fmt.Sprintf("pr_%s", bin.Name)) { + r.JSON(http.StatusForbidden, ErrorMsg{"The bin is private"}) + } else { + from := 0 + to := 20 + if fromVal, err := strconv.Atoi(req.FormValue("from")); err == nil { + from = fromVal } - } else { - r.Error(http.StatusNotFound) - } - }) - - api.Get("/api/v1/bins/:bin/requests/:request", func(r render.Render, storage Storage, params martini.Params){ - if request, err := storage.LookupRequest(params["bin"], params["request"]); err == nil { - r.JSON(http.StatusOK, request) - } else { - r.JSON(http.StatusNotFound, ErrorMsg{err.Error()}) - } - }) - - api.Any("/bins/:name", func(r render.Render, storage Storage, params martini.Params, - req *http.Request){ - if bin, error := storage.LookupBin(params["name"]); error == nil { - request := NewRequest(req, REQUEST_BODY_SIZE) - if err := storage.CreateRequest(bin, request); err == nil { - r.JSON(http.StatusOK, request) + if toVal, err := strconv.Atoi(req.FormValue("to")); err == nil { + to = toVal + } + if requests, err := storage.LookupRequests(bin.Name, from, to); err == nil { + r.JSON(http.StatusOK, requests) } else { r.JSON(http.StatusInternalServerError, ErrorMsg{err.Error()}) } - } else { - r.Error(http.StatusNotFound) } - }) + } else { + r.Error(http.StatusNotFound) + } + }) - api.Get("**", func(r render.Render){ - r.HTML(200, "index", nil) - }) + api.Get("/api/v1/bins/:bin/requests/:request", func(r render.Render, storage Storage, params martini.Params) { + if request, err := storage.LookupRequest(params["bin"], params["request"]); err == nil { + r.JSON(http.StatusOK, request) + } else { + r.JSON(http.StatusNotFound, ErrorMsg{err.Error()}) + } + }) + + api.Any("/bins/:name", func(r render.Render, storage Storage, params martini.Params, + req *http.Request) { + if bin, error := storage.LookupBin(params["name"]); error == nil { + request := NewRequest(req, REQUEST_BODY_SIZE) + if err := storage.CreateRequest(bin, request); err == nil { + r.JSON(http.StatusOK, request) + } else { + r.JSON(http.StatusInternalServerError, ErrorMsg{err.Error()}) + } + } else { + r.Error(http.StatusNotFound) + } + }) + + api.Get("**", func(r render.Render) { + r.HTML(200, "index", nil) + }) return api }