Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

github.com/naivary/api-layout

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/naivary/api-layout

  • v0.0.0-20240720124125-3bde39e173cb
  • Source
  • Go
  • Socket score

Version published
Created
Source

API Layout

This is an api-layout which can be used to implement a RESTful API in Go. The layout is suggesting tools and methods to be used based on best pratices but is allowing to expand however the user likes it.

TODOs

The following list states the requirements which are gradually implemented in the api-layout.

  • Error handling
  • Route adding
  • Graceful shutdown
  • HTTP/S Certificates
  • Configuration of the API using ENV variables, CLI and config files (12 Factor app)
  • General Logging approach based upon the 12 Factor app method
  • Default Values
  • Static validation via JSON Schema
  • Complex Validation using a common interface
  • Decoding and Encoding

Error handling

Handling errors in a HandlerFunc is challenging. One can't follow the usual pattern in Go which is to return the error from the function. Having this type of behavior for the HandlerFunc too would be really convenient which is why the api-layout is introducing the type HandlerFuncErr func(w http.ResponseWriter, r *http.Request) error type. You may know this type from frameworks like echo. Having this type allows for the usual pattern of Go.

Another challenge is the actual error type. HTTP services return different type of errors compared to usual Go functions. For example HTTP services have to return a status code to inform the client how the request went. Fortunately Go is allowing to easily introduce your own error type. This feature is used to introduce the HTTPError which is a specific error type for HTTP services.

Introcducing both of the types don't change the fact that errors have to be reported back to client using some kind of http response. Using the HandlerFuncErr type allows to implement a custom error handler for all the registered endpoints.

Route adding

Ideally all routes of an API can be overseen in one file. Which the update of http multiplexer in the standard library it can be used for complex routing. The api-layout is using the standard library and is allowing to define all the routes

Static validation via JSON Schema

FAQs

Package last updated on 20 Jul 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc