Created
September 24, 2012 07:22
-
-
Save tenntenn/3774748 to your computer and use it in GitHub Desktop.
Handling OPTIONS methods in goweb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"code.google.com/p/goweb/goweb" | |
"net/http" | |
) | |
type MyController struct{} | |
// for GET /api | |
func (c *MyController) Read(id string, cx *goweb.Context) { | |
cx.ResponseWriter.Header().Set("Access-Control-Allow-Origin", "*") | |
cx.ResponseWriter.Header().Set("Access-Control-Allow-Headers","*"); | |
cx.ResponseWriter.Header().Set("Access-Control-Allow-Methods", "GET") | |
cx.RespondWithOK() | |
} | |
// for GET /api/{id} | |
func (c *MyController) ReadMany(cx *goweb.Context) { | |
cx.ResponseWriter.Header().Set("Access-Control-Allow-Origin", "*") | |
cx.ResponseWriter.Header().Set("Access-Control-Allow-Headers","*"); | |
cx.ResponseWriter.Header().Set("Access-Control-Allow-Methods", "GET") | |
cx.RespondWithOK() | |
} | |
// for POST /api | |
func (c *MyController) Create(cx *goweb.Context) { | |
cx.ResponseWriter.Header().Set("Access-Control-Allow-Origin", "*") | |
cx.ResponseWriter.Header().Set("Access-Control-Allow-Headers","*"); | |
cx.ResponseWriter.Header().Set("Access-Control-Allow-Methods", "POST") | |
cx.RespondWithOK() | |
} | |
// for PUT /api/{id} | |
func (c *MyController) Update(id string, cx *goweb.Context) { | |
cx.ResponseWriter.Header().Set("Access-Control-Allow-Origin", "*") | |
cx.ResponseWriter.Header().Set("Access-Control-Allow-Headers","*"); | |
cx.ResponseWriter.Header().Set("Access-Control-Allow-Methods", "PUT") | |
cx.RespondWithOK() | |
} | |
// for PUT /api | |
func (c *MyController) UpdateMany(cx *goweb.Context) { | |
cx.ResponseWriter.Header().Set("Access-Control-Allow-Origin", "*") | |
cx.ResponseWriter.Header().Set("Access-Control-Allow-Headers","*"); | |
cx.ResponseWriter.Header().Set("Access-Control-Allow-Methods", "PUT") | |
cx.RespondWithOK() | |
} | |
// for DELETE /api/{id} | |
func (c *MyController) Delete(id string, cx *goweb.Context) { | |
cx.ResponseWriter.Header().Set("Access-Control-Allow-Origin", "*") | |
cx.ResponseWriter.Header().Set("Access-Control-Allow-Headers","*"); | |
cx.ResponseWriter.Header().Set("Access-Control-Allow-Methods", "DELETE") | |
cx.RespondWithOK() | |
} | |
// for DELETE /api | |
func (c *MyController) DeleteMany(cx *goweb.Context) { | |
cx.ResponseWriter.Header().Set("Access-Control-Allow-Origin", "*") | |
cx.ResponseWriter.Header().Set("Access-Control-Allow-Headers","*"); | |
cx.ResponseWriter.Header().Set("Access-Control-Allow-Methods", "DELTE") | |
cx.RespondWithOK() | |
} | |
// for OPTIONS /api | |
func (c *MyController) Options(cx *goweb.Context) { | |
cx.ResponseWriter.Header().Set("Access-Control-Allow-Origin", "*") | |
cx.ResponseWriter.Header().Set("Access-Control-Allow-Headers","*"); | |
cx.ResponseWriter.Header().Set("Access-Control-Allow-Methods", "GET,POST,PUT,DELTE,OPTIONS") | |
cx.RespondWithOK() | |
} | |
const OPTIONS_HTTP_METHOD string = "OPTIONS" | |
func OptionsMethod(cx *goweb.Context) goweb.RouteMatcherFuncValue { | |
if cx.Request.Method == OPTIONS_HTTP_METHOD { | |
return goweb.Match | |
} | |
return goweb.DontCare | |
} | |
func main() { | |
controller := new(MyController) | |
goweb.MapRest("/api", controller) | |
goweb.MapFunc("/api", controller.Options, OptionsMethod) | |
goweb.ConfigureDefaultFormatters() | |
http.Handle("/", goweb.DefaultHttpHandler) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment