Пример #1
0
func init() {

	// map the controllers
	mapGowebControllers()

	goweb.ConfigureDefaultFormatters()

	// ask Goweb to kindly handle all requests
	http.Handle("/", goweb.DefaultHttpHandler)

}
Пример #2
0
/*

	The main function will register the relevant controllers
	and start the web server

*/
func init() {

	/*
		Controller type
	*/

	// Create a 'people' controller instance (defined above)
	var peopleController *PeopleController = new(PeopleController)

	// bind the people controller to the /people/{id} route
	goweb.Map("/people/{id}", peopleController)

	/*
		ControllerFunc type
	*/

	goweb.MapFunc("/animals/{animal_name}", func(c *goweb.Context) {
		fmt.Fprintf(c.ResponseWriter, "Your favourite animal is a %s", c.PathParams["animal_name"])
	})

	/*
		Different GET and POST controllers
	*/
	goweb.MapFunc("/specific-method/{id}", func(c *goweb.Context) {
		fmt.Fprintf(c.ResponseWriter, "I will return resource %s.", c.PathParams["id"])
	}, goweb.GetMethod)
	goweb.MapFunc("/specific-method/{id}", func(c *goweb.Context) {
		fmt.Fprintf(c.ResponseWriter, "I will create a new resource for %s.", c.PathParams["id"])
	}, goweb.PostMethod)

	/*
		RESTful resources
	*/
	controller := new(MyResourceController)
	goweb.MapRest("/restful-resource", controller)

	/*
		API controller
	*/
	apiController := new(MyAPIController)
	goweb.MapRest("/api", apiController)

	goweb.ConfigureDefaultFormatters()
	http.Handle("/", goweb.DefaultHttpHandler)

}