Exemplo n.º 1
0
Arquivo: main.go Projeto: nihao/goweb
/*

	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)

}
Exemplo n.º 2
0
func init() {
	// Articles
	goweb.MapRest("/article/category/{category_id}", new(controller.ArticleController))

	// Articles
	goweb.MapRest("/article", new(controller.ArticleController))

	// Comments
	goweb.MapRest("/comment", new(controller.CommentController))

	// Categories
	goweb.MapRest("/category", new(controller.CategoryController))

	// Logs
	goweb.MapRest("/log", new(controller.LogController))

	// Search
	goweb.MapRest("/search", new(controller.SearchController))

	// Documentation wadl
	goweb.MapFunc("/documentation/wadl", func(c *goweb.Context) {
		var docTemplate = template.Must(template.ParseFile("webservice/views/wadl.wadl"))

		if err := docTemplate.Execute(c.ResponseWriter, nil); err != nil {
			c.RespondWithErrorMessage(err.String(), http.StatusInternalServerError)
		}
	})

	// WADL in XML voor XML viewer
	goweb.MapFunc("/documentation/wadl/xml", func(c *goweb.Context) {
		var docTemplate = template.Must(template.ParseFile("webservice/views/wadl.xml"))

		if err := docTemplate.Execute(c.ResponseWriter, nil); err != nil {
			c.RespondWithErrorMessage(err.String(), http.StatusInternalServerError)
		}
	})

	// Documentation
	goweb.MapFunc("/documentation", func(c *goweb.Context) {
		var docTemplate = template.Must(template.ParseFile("webservice/views/documentation.html"))

		if err := docTemplate.Execute(c.ResponseWriter, nil); err != nil {
			c.RespondWithErrorMessage(err.String(), http.StatusInternalServerError)
		}
	})

	// Index
	goweb.MapFunc("/", func(c *goweb.Context) {
		var indexTemplate = template.Must(template.ParseFile("webservice/views/index.html"))

		if err := indexTemplate.Execute(c.ResponseWriter, nil); err != nil {
			c.RespondWithErrorMessage(err.String(), http.StatusInternalServerError)
		}
	})

	// use the default Go handler
	http.Handle("/", goweb.DefaultHttpHandler)
}
Exemplo n.º 3
0
func mapGowebControllers() {

	// create a 'PeopleController'
	peopleController := new(PeopleController)

	// map the People resources
	goweb.MapFunc("/people/new", func(c *goweb.Context) {
		peopleController.New(c)
	})
	goweb.MapFunc("/people/{{id}}/edit", func(c *goweb.Context) {
		peopleController.Edit(c.PathParams["id"], c)
	})
	goweb.MapFunc("/people/{id}/confirm-delete", func(c *goweb.Context) {
		peopleController.DeleteConfirm(c.PathParams["id"], c)
	})
	goweb.MapRest("/people", peopleController)

}