Compare commits
2 Commits
Author | SHA1 | Date |
---|---|---|
|
db142def3f | |
|
60bc6071f2 |
|
@ -0,0 +1,10 @@
|
|||
<component name="ProjectRunConfigurationManager">
|
||||
<configuration default="false" name="run" type="GoApplicationRunConfiguration" factoryName="Go Application">
|
||||
<module name="skimmer" />
|
||||
<working_directory value="$PROJECT_DIR$" />
|
||||
<kind value="DIRECTORY" />
|
||||
<directory value="$PROJECT_DIR$/src" />
|
||||
<filePath value="$PROJECT_DIR$" />
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
</component>
|
|
@ -0,0 +1,22 @@
|
|||
module git.tiburon.su/USEFULL/skimmer
|
||||
|
||||
go 1.18
|
||||
|
||||
require (
|
||||
github.com/codegangsta/martini v0.0.0-20170121215854-22fa46961aab
|
||||
github.com/codegangsta/martini-contrib v0.0.0-20140208234550-8ce6181c2609
|
||||
github.com/garyburd/redigo v1.6.3
|
||||
github.com/stretchr/testify v1.8.0
|
||||
github.com/ugorji/go/codec v1.2.7
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/codegangsta/inject v0.0.0-20150114235600-33e0aa1cb7c0 // indirect
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/gorilla/context v1.1.1 // indirect
|
||||
github.com/gorilla/securecookie v1.1.1 // indirect
|
||||
github.com/gorilla/sessions v1.2.1 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/stretchr/objx v0.4.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
|
@ -0,0 +1,33 @@
|
|||
github.com/codegangsta/inject v0.0.0-20150114235600-33e0aa1cb7c0 h1:sDMmm+q/3+BukdIpxwO365v/Rbspp2Nt5XntgQRXq8Q=
|
||||
github.com/codegangsta/inject v0.0.0-20150114235600-33e0aa1cb7c0/go.mod h1:4Zcjuz89kmFXt9morQgcfYZAYZ5n8WHjt81YYWIwtTM=
|
||||
github.com/codegangsta/martini v0.0.0-20170121215854-22fa46961aab h1:eFEFkK1s2OXNiIOvjNT5lk/AGXQodN2l8VgbkCK9r4c=
|
||||
github.com/codegangsta/martini v0.0.0-20170121215854-22fa46961aab/go.mod h1:0SkifPRh0YknjZR6LxtP+eMvgPwUI6DG8hE9U/3dW9E=
|
||||
github.com/codegangsta/martini-contrib v0.0.0-20140208234550-8ce6181c2609 h1:aRxx5sQikIjKQPDVpYbEjaUSrj58MM5kfxX2wS9nNNQ=
|
||||
github.com/codegangsta/martini-contrib v0.0.0-20140208234550-8ce6181c2609/go.mod h1:Hr/9ecwnTZD7izDjg7HoB5wOJ01ZbNo+ntSTza2hqR4=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/garyburd/redigo v1.6.3 h1:HCeeRluvAgMusMomi1+6Y5dmFOdYV/JzoRrrbFlkGIc=
|
||||
github.com/garyburd/redigo v1.6.3/go.mod h1:rTb6epsqigu3kYKBnaF028A7Tf/Aw5s0cqA47doKKqw=
|
||||
github.com/gorilla/context v1.1.1 h1:AWwleXJkX/nhcU9bZSnZoi3h/qGYqQAGhq6zZe/aQW8=
|
||||
github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg=
|
||||
github.com/gorilla/securecookie v1.1.1 h1:miw7JPhV+b/lAHSXz4qd/nN9jRiAFV5FwjeKyCS8BvQ=
|
||||
github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4=
|
||||
github.com/gorilla/sessions v1.2.1 h1:DHd3rPN5lE3Ts3D8rKkQ8x/0kqfeNmBAaiSi+o7FsgI=
|
||||
github.com/gorilla/sessions v1.2.1/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.4.0 h1:M2gUjqZET1qApGOWNSnZ49BAIMX4F/1plDv3+l31EJ4=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
github.com/ugorji/go v1.2.7/go.mod h1:nF9osbDWLy6bDVv/Rtoh6QgnvNDpmCalQV5urGCCS6M=
|
||||
github.com/ugorji/go/codec v1.2.7 h1:YPXUKf7fYbp/y8xloBqZOw2qaVggbfwMlI8WM3wZUJ0=
|
||||
github.com/ugorji/go/codec v1.2.7/go.mod h1:WGN1fab3R1fzQlVQTkfxVtIBhWDRqOviHU95kRgeqEY=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
11
src/main.go
11
src/main.go
|
@ -1,17 +1,19 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"skimmer"
|
||||
"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",
|
||||
RedisAddr: "127.0.0.1:6379",
|
||||
RedisPassword: "",
|
||||
RedisPrefix: "skimmer",
|
||||
RedisPrefix: "skimmer",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue