Use specific content-type options for each route

This commit is contained in:
Tobie Morgan Hitchcock 2017-02-20 01:56:29 +00:00
parent b304c9f3f4
commit b1165a6180
2 changed files with 42 additions and 12 deletions

View file

@ -19,6 +19,7 @@ import (
"time"
"github.com/abcum/fibre"
"github.com/abcum/fibre/mw"
"github.com/abcum/surreal/db"
"github.com/abcum/surreal/sql"
)
@ -107,6 +108,15 @@ func routes(s *fibre.Fibre) {
return signup(c)
})
s.Use(mw.Type(&mw.TypeOpts{
AllowedContent: map[string]bool{
"application/json": true,
"application/cbor": true,
"application/msgpack": true,
"application/x-www-form-urlencoded": true,
},
}).PathIs("/signup"))
// --------------------------------------------------
// Endpoints for authentication signin
// --------------------------------------------------
@ -119,6 +129,15 @@ func routes(s *fibre.Fibre) {
return signin(c)
})
s.Use(mw.Type(&mw.TypeOpts{
AllowedContent: map[string]bool{
"application/json": true,
"application/cbor": true,
"application/msgpack": true,
"application/x-www-form-urlencoded": true,
},
}).PathIs("/signin"))
// --------------------------------------------------
// Endpoints for import and exporting data
// --------------------------------------------------
@ -131,6 +150,12 @@ func routes(s *fibre.Fibre) {
return importer(c)
})
s.Use(mw.Type(&mw.TypeOpts{
AllowedContent: map[string]bool{
"application/octet-stream": true,
},
}).PathIs("/export", "/import"))
// --------------------------------------------------
// Endpoints for submitting sql queries
// --------------------------------------------------
@ -147,6 +172,14 @@ func routes(s *fibre.Fibre) {
return c.Send(200, res)
})
s.Use(mw.Type(&mw.TypeOpts{
AllowedContent: map[string]bool{
"application/json": true,
"application/cbor": true,
"application/msgpack": true,
},
}).PathIs("/sql"))
s.Get("/sql", func(c *fibre.Context) error {
if err := c.Upgrade(); err != nil {
@ -327,4 +360,13 @@ func routes(s *fibre.Fibre) {
})
s.Use(mw.Type(&mw.TypeOpts{
AllowedContent: map[string]bool{
"application/json": true,
"application/cbor": true,
"application/msgpack": true,
"application/vnd.api+json": true,
},
}).PathBegsWith("/key/"))
}

View file

@ -54,18 +54,6 @@ func Setup(opts *cnf.Options) (err error) {
AllowedLength: 1 << 20,
}))
// Check body type
s.Use(mw.Type(&mw.TypeOpts{
AllowedContent: map[string]bool{
"application/json": true,
"application/cbor": true,
"application/msgpack": true,
"application/octet-stream": true,
"application/x-www-form-urlencoded": true,
},
}))
// Run the server
if len(opts.Cert.Crt) == 0 || len(opts.Cert.Key) == 0 {