Skip to content

Lightweight library for extracting path params using default net/http

License

Notifications You must be signed in to change notification settings

urec56/pathparams

Repository files navigation

Path Params

This library allows you to extract path parameters from HTTP requests if you are using the standard net/http package. The extracted parameters will be available in the request's context.

By default, it uses the pp- prefix for context variables to avoid collisions if you are using some additional middlewares. You can specify your own prefix or delete it at all by using the SetConfig function.

By default, all values have string type. To convert them to the correct type, you need to check the Validation and type conversion title.

The values are extracted before any other middleware, allowing you to use them within the middleware.

To use this library, simply use pathparams.HandleFunc instead of the default http.HandleFunc. For code examples, check the example folder.

If you want to add handler only for one HTTP method (for example, GET), you can use the Get, Post, Put, Patch and Delete functions.

This library was tested on the 1.26.6 version of golang. It may work on any other version, but it is not guaranteed.

Validation and type conversion

You can specify your own validation rules for extracted values by using pathparams.VHandleFunc with a new argument of pathparams.Validators type (simply an alias for map[string]func(string) (any, error) type, so you can use it instead).

As the key, you should use the name (that value in the {} template) of the parameter you want to validate (don't forget about the prefix), and as the value - your custom validation function. You can not only validate but convert types of values in it and return custom error.

Be careful with the text in the error, it will be used in the response body.

For example, you can use any validator in the validators.go file. If an error occurs during validation, the cfg.validationErrorStatusCode will be written as the response status code. You can specify it by using the pathparams.SetConfig function (by default it uses the 422 Unprocessable Entity status code. Yes, I like FastAPI).

ServeMux

This object implements the same interface as http.ServeMux but with some additional rules and opportunities.

You can get the new mux by using the pathparams.NewServeMux function or just using the pathparams.DefaultServeMux. Any routes that were added by functions like pathparams.HandleFunc(from file handlers.go) are using the default one.

Also, you can add some validation rules to all of your handlers by creating it with pathparams.NewVServeMux. If you want to add some specific rule for one or more paths, you can use the mux.VHandleFunc to register it. Rules can be nil, no panics or errors will occur in that case.

To start the server, you can use the http.ListenAndServe function or the mux.ListenAndServe method.

If you forget what methods pathparams.ServeMux implements, you can check the interfaces.go file.

Router

If you want to use some prefix for multiple handlers, you can use the pathparams.Router object. It can be created by calling the pathparams.NewRouter function or the pathparams.NewVRouter function if you want to use some validation rules. Then you can add some prefixes for some routes by calling the router.PathPrefix method. Also, you can call PathPrefix on the object returned from it to make another group with a more complex prefix or simple "" path for a particular router.

r := NewRouter()
prefixRouter := r.PathPrefix("/some/prefix").PathPrefix("/user") 

The new router will inherit all the rules of its parents.

To set some middleware to your router, you can use the router.Use method. It will set new middleware to the end of the middleware stack of this router. So all handlers of this router or its subrouters initialized after setting new middleware will use it.

Middlewares that were added with router.Use will be called in the order you added them.

If you want there to be no middlewares for this specific handler, you should use router.HandleFunc or router.VHandleFunc directly.

In other cases, Router will behave like ServeMux with the same interface.

TODO (maybe)

  • Add optional typing (so that the values are immediately of the correct type)
  • Add support to the http.ServeMux object

About

Lightweight library for extracting path params using default net/http

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages