Exemple #1
0
func main() {

	my_api := golax.NewApi()
	my_api.Prefix = "/service/v1"

	my_api.Root.
		Interceptor(golax.InterceptorLog).
		Interceptor(golax.InterceptorError)

	users := my_api.Root.Node("users").
		Method("GET", get_users).
		Method("POST", post_users)

	users.Node("{user_id}").
		Interceptor(interceptor_user).
		Method("GET", get_user).
		Method("POST", post_user).
		Method("DELETE", delete_user)

	my_api.Serve()
}
Exemple #2
0
func main() {

	my_api := golax.NewApi()
	my_api.Prefix = "/service/v1"

	my_api.Root.
		Doc(golax.Doc{
			Description: `
				_Example_ is a demonstration REST API that implements a CRUD over a collection
				of users, stored in memory.

				All API calls:
				* are returning errors with the same JSON format and
				* are logging all request to standard output.
			`,
		}).
		Interceptor(golax.InterceptorLog).
		Interceptor(golax.InterceptorError)

	users := my_api.Root.Node("users").
		Doc(golax.Doc{
			Description: `
				Resource users to list and create elements. It does not support pagination,
				sorting or filtering.
			`}).
		Method("GET", get_users, golax.Doc{
			Description: `
				Return a list with a list of user ids:

				´´´json
				[1,2,3]
				´´´
			`}).
		Method("POST", post_users, golax.Doc{
			Description: `
				Create a user:
				´´´sh
				curl http://localhost:8000/service/v1/users --data '{"name": "John"}'
				´´´
				And return the user id:
				´´´json
				{"id":4}
				´´´
		`})

	users.Node("{user_id}").
		Doc(golax.Doc{
			Description: `
				Resource user to retrieve, modify and delete. A user has this structure:

				´´´json
				{
					"name": "Menganito Menganez",
					"age": 30,
					"introduction": "Hi, I like wheels and cars"
				}
				´´´
		`}).
		Interceptor(interceptor_user).
		Method("GET", get_user, golax.Doc{
			Description: `
				Return a user in JSON format. For example:
				´´´sh
				curl http://localhost:8000/service/v1/users/4
				´´´
				Will return this:
				´´´json
				{
					"name": "John",
					"age": 0,
					"introduction": ""
				}
				´´´
			`}).
		Method("POST", post_user, golax.Doc{
			Description: `
				Modify an existing user. You do not have to send all fields, for example, to
				change only the age of the user 4:

				´´´sh
				curl http://localhost:8000/service/v1/users/4 --data '{"age": 11}'
				´´´
			`}).
		Method("DELETE", delete_user, golax.Doc{
			Description: `
				Delete an existing user:

				´´´sh
				curl -X DELETE http://localhost:8000/service/v1/users/4
				´´´
			`})

	apidoc.Build(my_api)

	my_api.Serve()
}