Example #1
0
// mapRoutes contains lots of examples of how to map things in
// Goweb.  It is in its own function so that test code can call it
// without having to run main().
func mapRoutes() {

	/*
	   Add a pre-handler to save the referrer
	*/
	goweb.MapBefore(func(c context.Context) error {

		// add a custom header
		c.HttpResponseWriter().Header().Set("X-Custom-Header", "Goweb")

		return nil
	})

	/*
	   Add a post-handler to log something
	*/
	goweb.MapAfter(func(c context.Context) error {
		// TODO: log this
		return nil
	})

	/*
	   Map the homepage...
	*/
	goweb.Map("/", func(c context.Context) error {
		return goweb.Respond.With(c, 200, []byte("Welcome to the Goweb example app - see the terminal for instructions."))
	})

	/*
	   Map a specific route that will redirect
	*/
	goweb.Map("GET", "people/me", func(c context.Context) error {
		hostname, _ := os.Hostname()
		return goweb.Respond.WithRedirect(c, fmt.Sprintf("/people/%s", hostname))
	})

	/*
	   /people (with optional ID)
	*/
	goweb.Map("GET", "people/[id]", func(c context.Context) error {

		if c.PathParams().Has("id") {
			return goweb.API.Respond(c, 200, fmt.Sprintf("Yes, this worked and your ID is %s", c.PathParams().Get("id")), nil)
		} else {
			return goweb.API.Respond(c, 200, "Yes, this worked but you didn't specify an ID", nil)
		}

	})

	/*
	   /status-code/xxx
	   Where xxx is any HTTP status code.
	*/
	goweb.Map("/status-code/{code}", func(c context.Context) error {

		// get the path value as an integer
		statusCodeInt, statusCodeIntErr := strconv.Atoi(c.PathValue("code"))
		if statusCodeIntErr != nil {
			return goweb.Respond.With(c, http.StatusInternalServerError, []byte("Failed to convert 'code' into a real status code number."))
		}

		// respond with the status
		return goweb.Respond.WithStatusText(c, statusCodeInt)
	})

	// /errortest should throw a system error and be handled by the
	// DefaultHttpHandler().ErrorHandler() Handler.
	goweb.Map("/errortest", func(c context.Context) error {
		return errors.New("This is a test error!")
	})

	/*
	   Map a RESTful controller
	   (see the ThingsController for all the methods that will get
	    mapped)
	*/
	thingsController := new(ThingsController)
	goweb.MapController(thingsController)

	/*
	   Map a handler for if they hit just numbers using the goweb.RegexPath
	   function.

	   e.g. GET /2468

	   NOTE: The goweb.RegexPath is a MatcherFunc, and so comes _after_ the
	         handler.
	*/
	goweb.Map(func(c context.Context) error {
		return goweb.API.RespondWithData(c, "Just a number!")
	}, goweb.RegexPath(`^[0-9]+$`))

	/*
	   Catch-all handler for everything that we don't understand
	*/
	goweb.Map(func(c context.Context) error {

		// just return a 404 message
		return goweb.API.Respond(c, 404, nil, []string{"File not found"})

	})

}
// mapRoutes contains lots of examples of how to map Books in
// Goweb.  It is in its own function so that test code can call it
// without having to run main().
func mapRoutes() {

	/*
	   Add a pre-handler to save the referrer
	*/
	goweb.MapBefore(func(c context.Context) error {

		// add a custom header
		c.HttpResponseWriter().Header().Set("X-Custom-Header", "Goweb")

		return nil
	})

	/*
	   Add a post-handler to log someBook
	*/
	goweb.MapAfter(func(c context.Context) error {
		// TODO: log this
		return nil
	})

	/*
	   Map the homepage...
	*/
	goweb.Map("/", func(c context.Context) error {
		return goweb.Respond.With(c, 200, []byte("Welcome to the Goweb example app - see the terminal for instructions."))
	})

	/*
	   /status-code/xxx
	   Where xxx is any HTTP status code.
	*/
	goweb.Map("/status-code/{code}", func(c context.Context) error {

		// get the path value as an integer
		statusCodeInt, statusCodeIntErr := strconv.Atoi(c.PathValue("code"))
		if statusCodeIntErr != nil {
			return goweb.Respond.With(c, http.StatusInternalServerError, []byte("Failed to convert 'code' into a real status code number."))
		}

		// respond with the status
		return goweb.Respond.WithStatusText(c, statusCodeInt)
	})

	// /errortest should throw a system error and be handled by the
	// DefaultHttpHandler().ErrorHandler() Handler.

	goweb.Map("/errortest", func(c context.Context) error {
		return errors.New("This is a test error!")
	})

	/*
	   Map a RESTful controller
	   (see the BooksController for all the methods that will get
	    mapped)
	*/
	BooksController := new(BooksController)
	goweb.MapController(BooksController)

	goweb.Map(func(c context.Context) error {
		return goweb.API.RespondWithData(c, "Just a number!")
	}, goweb.RegexPath(`^[0-9]+$`))

	/*
	   Catch-all handler for everything that we don't understand
	*/
	goweb.Map(func(c context.Context) error {

		// just return a 404 message
		return goweb.API.Respond(c, 404, nil, []string{"File not found"})

	})

}
Example #3
0
// mapRoutes contains lots of examples of how to map things in
// Goweb.  It is in its own function so that test code can call it
// without having to run main().
func mapRoutes() {
	goweb.MapBefore(func(c context.Context) error {
		c.HttpResponseWriter().Header().Set("X-Custom-Header", "WebGit")
		c.HttpResponseWriter().Header().Set("Connection", "keep-alive")
		return nil
	})

	goweb.MapAfter(func(c context.Context) error {
		// TODO: log this
		return nil
	})

	/*
		Map the homepage...
	*/
	goweb.Map("/", func(c context.Context) error {
		return goweb.Respond.With(c, 200, []byte("Welcome to the WebGit app - see the terminal for instructions."))
	})

	/*
		search git developers
	*/
	goweb.Map("GET", "gitsearch", func(c context.Context) error {
		if c.QueryParams().Has("q") {
			res, err := http.Get("https://api.github.com/search/users?q=" + c.QueryValue("q"))
			if err != nil {
				log.Fatal(err)
				return goweb.API.Respond(c, 200, fmt.Sprintf("Erro: %s", err), nil)
			}
			response, err := ioutil.ReadAll(res.Body)
			res.Body.Close()
			if err != nil {
				log.Fatal(err)
				return goweb.API.Respond(c, 200, fmt.Sprintf("Erro: %s", err), nil)
			}
			return goweb.API.Respond(c, 200, fmt.Sprintf("%s", response), nil)
		} else {
			return goweb.API.Respond(c, 200, "Pesquisa vazia", nil)
		}
	})

	/*
		repo git developer
	*/
	goweb.Map("GET", "reposdeveloper", func(c context.Context) error {
		if c.QueryParams().Has("user") {
			res, err := http.Get("https://api.github.com/users/" + c.QueryValue("user") + "/repos")
			if err != nil {
				log.Fatal(err)
				return goweb.API.Respond(c, 200, fmt.Sprintf("Erro: %s", err), nil)
			}
			response, err := ioutil.ReadAll(res.Body)
			res.Body.Close()
			if err != nil {
				log.Fatal(err)
				return goweb.API.Respond(c, 200, fmt.Sprintf("Erro: %s", err), nil)
			}
			return goweb.API.Respond(c, 200, fmt.Sprintf("%s", response), nil)
		} else {
			return goweb.API.Respond(c, 200, "No user send", nil)
		}
	})

	/*
		followers git developer
	*/
	goweb.Map("GET", "followersdevelopers", func(c context.Context) error {
		if c.QueryParams().Has("user") {
			res, err := http.Get("https://api.github.com/users/" + c.QueryValue("user") + "/followers")
			if err != nil {
				log.Fatal(err)
				return goweb.API.Respond(c, 200, fmt.Sprintf("Erro: %s", err), nil)
			}
			response, err := ioutil.ReadAll(res.Body)
			res.Body.Close()
			if err != nil {
				log.Fatal(err)
				return goweb.API.Respond(c, 200, fmt.Sprintf("Erro: %s", err), nil)
			}
			return goweb.API.Respond(c, 200, fmt.Sprintf("%s", response), nil)
		} else {
			return goweb.API.Respond(c, 200, "Pesquisa vazia", nil)
		}
	})

	/*
		/status-code/xxx
		Where xxx is any HTTP status code.
	*/
	goweb.Map("/status-code/{code}", func(c context.Context) error {

		// get the path value as an integer
		statusCodeInt, statusCodeIntErr := strconv.Atoi(c.PathValue("code"))
		if statusCodeIntErr != nil {
			return goweb.Respond.With(c, http.StatusInternalServerError, []byte("Failed to convert 'code' into a real status code number."))
		}

		// respond with the status
		return goweb.Respond.WithStatusText(c, statusCodeInt)
	})

	// /errortest should throw a system error and be handled by the
	// DefaultHttpHandler().ErrorHandler() Handler.
	goweb.Map("/errortest", func(c context.Context) error {
		return errors.New("This is a test error!")
	})

	/*
		Map a RESTful controller
		(see the DevelopersController for all the methods that will get
		 mapped)
	*/
	developersController := new(DevelopersController)
	goweb.MapController(developersController)

	/*
		Map a handler for if they hit just numbers using the goweb.RegexPath
		function.
		e.g. GET /2468
		NOTE: The goweb.RegexPath is a MatcherFunc, and so comes _after_ the
		      handler.
	*/
	goweb.Map(func(c context.Context) error {
		return goweb.API.RespondWithData(c, "Just a number!")
	}, goweb.RegexPath(`^[0-9]+$`))

	/*
		Map the static-files directory so it's exposed as /static
	*/
	goweb.MapStatic("/static", "static-files")

	/*
		Map the a favicon
	*/
	goweb.MapStaticFile("/favicon.ico", "static-files/favicon.ico")

	/*
		Catch-all handler for everything that we don't understand
	*/
	goweb.Map(func(c context.Context) error {

		// just return a 404 message
		return goweb.API.Respond(c, 404, nil, []string{"File not found"})

	})
}