Some fixes
parent
60bc6071f2
commit
db142def3f
|
@ -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>
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -1,18 +1,15 @@
|
|||
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"`
|
||||
}
|
||||
|
||||
|
@ -29,6 +26,7 @@ type RedisConfig struct {
|
|||
}
|
||||
|
||||
type Config struct {
|
||||
Address string
|
||||
SessionSecret string
|
||||
Storage string
|
||||
RedisConfig
|
||||
|
@ -37,7 +35,7 @@ type Config struct {
|
|||
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))
|
||||
|
||||
|
@ -62,8 +59,7 @@ func GetApi(config *Config) *martini.ClassicMartini {
|
|||
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){
|
||||
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)})
|
||||
|
@ -84,7 +80,7 @@ func GetApi(config *Config) *martini.ClassicMartini {
|
|||
}
|
||||
})
|
||||
|
||||
api.Get("/api/v1/bins/", func(r render.Render, storage Storage, history History){
|
||||
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 {
|
||||
|
@ -92,9 +88,9 @@ func GetApi(config *Config) *martini.ClassicMartini {
|
|||
}
|
||||
})
|
||||
|
||||
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)){
|
||||
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)
|
||||
|
@ -105,9 +101,9 @@ func GetApi(config *Config) *martini.ClassicMartini {
|
|||
})
|
||||
|
||||
api.Get("/api/v1/bins/:bin/requests/", func(r render.Render, storage Storage, session sessions.Session,
|
||||
params martini.Params, req *http.Request){
|
||||
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)){
|
||||
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
|
||||
|
@ -129,7 +125,7 @@ func GetApi(config *Config) *martini.ClassicMartini {
|
|||
}
|
||||
})
|
||||
|
||||
api.Get("/api/v1/bins/:bin/requests/:request", func(r render.Render, storage Storage, params martini.Params){
|
||||
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 {
|
||||
|
@ -138,7 +134,7 @@ func GetApi(config *Config) *martini.ClassicMartini {
|
|||
})
|
||||
|
||||
api.Any("/bins/:name", func(r render.Render, storage Storage, params martini.Params,
|
||||
req *http.Request){
|
||||
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 {
|
||||
|
@ -151,7 +147,7 @@ func GetApi(config *Config) *martini.ClassicMartini {
|
|||
}
|
||||
})
|
||||
|
||||
api.Get("**", func(r render.Render){
|
||||
api.Get("**", func(r render.Render) {
|
||||
r.HTML(200, "index", nil)
|
||||
})
|
||||
return api
|
||||
|
|
Loading…
Reference in New Issue