Skip to content

MordFustang21/supernova

Repository files navigation

Supernova

GoDoc Go Report Card Build Status

An express like router for fasthttp

Provides a lot of the same methods and functionality as Expressjs

Example

package main

import (
	"fmt"

	"github.com/MordFustang21/supernova"
)

func main() {
	// Get new instance of server
	s := supernova.New()

	//Middleware Example
	s.Use(func(req *supernova.Request, next func()) {
		req.Response.Header.Set("Powered-By", "supernova")
		next()
	})

	//Route Examples
	s.Post("/test/taco/:apple", func(req *supernova.Request) {

		// Get query parameters
		limit := req.QueryParam("limit")

		type test struct {
			Apple string
		}

		// Read JSON into struct from body
		var testS test
		err := req.ReadJSON(&testS)
		if err != nil {
			fmt.Println("Error:", err)
		}

		req.Send("Received data " + limit)
	})

	// Example Get route with route params
	s.Get("/test/:taco/:apple", func(req *supernova.Request) {
		tacoType := req.RouteParam("taco")
		req.Send(tacoType)
	})

	// Resticted routes are used to restict methods other than GET,PUT,POST,DELETE
	s.Restricted("OPTIONS", "/test/stuff", func(req *supernova.Request) {
		req.Send("OPTIONS Request received")
	})

	// Example post returning error
	s.Post("/register", func(req *supernova.Request) {
		if len(req.Request.Body()) == 0 {
			// response code, error message, and any struct you want put into the errors array
			req.Error(500, "Body is empty")
		}
	})

	err := s.ListenAndServe(":8080")
	if err != nil {
		println(err.Error())
	}
}

About

An Express like router for fasthttp

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages