Categorygithub.com/netr/napi
modulepackage
1.1.0
Repository: https://github.com/netr/napi.git
Documentation: pkg.go.dev

# README

What ChatGPT has to say

The Netr API Framework is an opinionated API framework designed specifically for use with the Fiber web framework, a popular open-source web framework for the Go programming language. This framework draws inspiration from Laravel's end-to-end testing capabilities, enabling developers to easily and effectively test their applications.

It's important to note that the Netr API Framework currently only supports Gorm as a database driver. This feature provides developers with a reliable and efficient way of accessing data within their applications. Additionally, the framework's testing suite leverages a similar RefreshDB() as Laravel, utilizing sqllite under the hood. This ensures that developers can thoroughly test their applications before deployment.

While the Netr API Framework was initially developed for the personal use of its creator, it is open to suggestions and contributions from other developers via pull requests. As such, the developer is committed to continuously improving the framework and its accompanying documentation.

Currently, the Netr API Framework is still in the development stage. However, it is usable for small-scale projects and experimentation. Additionally, the development team is committed to ensuring that any new features added to the framework are designed in a way that does not break existing code and does not impact the underlying structure of the package. The team is also open to any suggestions and feedback from the community and will update the documentation accordingly.

What Bard has to say

Netr API Framework is an opinionated API framework for GoFiber. It takes inspiration from E2E testing in Laravel, and currently supports Gorm as a database driver.

The framework provides a way to test APIs using a similar RefreshDB() function as Laravel, which uses sqllite under the hood. This makes it easy to test APIs without having to worry about setting up a database server.

Netr API Framework is primarily for the author's own use, but they are open to suggestions and PRs. They can write a better README and documentation if there is interest.

Currently, the framework is still in development, but it is usable for small projects/experimenting. Features that are being added do not break existing code, and do not impact the underlying structure of the package.

Examples

  • /app API Server with Gorm and Controller Testing

Server

Usage

srv := NewServer(
    DefaultFiberConfig("App Name"),

    // Default is 1337 if you leave this option out
    WithPort(1338)
	
    // QoL
    WithBaseMiddlewares(),
    WithCatchAll(),
	
    // CORS
    WithDefaultCORS(),
    WithCORS(cors.Config{}),
	
    // Metrics
    WithPrometheus("app_name"),
	
    // Profiling
    WithPprof(),
	
    // Loggers
    WithDefaultLogger(),
    WithLogger(logger.Config{}),
    WithLoggerOutput(&bytes.Buffer{}),
    WithLoggerDoneCallback(func(c *fiber.Ctx, logString []byte) {}),

    // Limiter
    WithDefaultLimiter(),
    WithLimiter(limiter.Config{}),
)
srv.Run()
srv := NewServer(
	    DefaultFiberConfig("App Name"),
	    WithCatchAll(), // Figure this out eventually.
	).
	Port(1338).
	UseBaseMiddlewares().
	UsePrometheus("app_name").
	UsePprof().
	UseDefaultCORS().
	UseCORS(cors.Config{}).
	UseDefaultLogger().
	UseLogger(logger.Config{}).
	UseLoggerOutput(&bytes.Buffer{}).
	UseLoggerDoneCallback(func(c *fiber.Ctx, logString []byte) {}).
	UseDefaultLimiter().
	UseLimiter(limiter.Config{})
    )
srv.Run()

Testing controllers

type accountSuite struct {
    ControllerSuite
}

func (s *accountSuite) TestIndex_ExpectedBehavior() {
    // Create account using factory [see /examples/db/models/suite.go & /factory]
    a := s.CreateAccount()
    
    trex.New(s).
        Get(s.Route("accounts.index"), nil).
        AssertOk().
        AssertDataCount(2).
        AssertJsonEqual("data[0].username", a.Username).
        AssertJsonEqual("data[1].username", b.Username)
}

rprint

Easily print your routes.

// see Test_PrintPretty for more information
napi.NewRoutePrinter(fiberApp).PrintDebug()
napi.NewRoutePrinter(fiberApp).PrintPretty()

// Sugar
napi.PrintRoutes(fiberApp, isDebugMode)

# Packages

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Functions

DefaultFiberConfig basic fiber configuration with write and read timeouts set under the hood to 30 seconds.
No description provided by the author
NewRoutePrinter create a new route printer.
NewServer Creates a new fiber instance with a fiber.Config and stackable options using the options pattern.
PrintRoutes this is the most useful function in our programs.
ToCamelCase converts a string to CamelCase.
ToDelimited converts a string to delimited.snake.case (in this case `delimiter = '.'`).
ToScreamingDelimited converts a string to SCREAMING.DELIMITED.SNAKE.CASE (in this case `delimiter = '.'; screaming = true`) or delimited.snake.case (in this case `delimiter = '.'; screaming = false`).
ToSnakeCase converts a string to snake_case.
WithBaseMiddlewares a set of middlewares that are essentially plug and play.
WithCache uses cache control headers to set the cache control header to public and max age to 1 year.
WithCatchAll sets up a simple catch all handler.
WithCORS use the CORS middleware with the a custom configuration.
WithDefaultCache uses cache control headers to set the cache control header to public and max age to 1 year.
WithDefaultCORS use the CORS middleware with the default configuration.
WithDefaultLimiter use the Limiter middleware with the default configuration.
WithDefaultLogger use the logger middleware with the default configuration.
WithHealth opens up a health ping endpoint to be used for uptime monitoring.
WithLimiter use the Limiter middleware with the a custom configuration.
WithLogger use the logger middleware with a custom logger.Config struct.
WithLoggerDoneCallback use the logger middleware with a custom done callback after a log is written.
WithLoggerOutput use the logger middleware with a custom output writer.
WithPort set the web server port.
WithPprof adds the middleware for running pprof.
WithPrometheus adds the middleware for prometheus.

# Structs

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
RoutePrinter route printer struct.
Server fiber app instance.
No description provided by the author

# Interfaces

No description provided by the author
No description provided by the author

# Type aliases

ServerOption type used for option pattern.
No description provided by the author