jasmine

package module
v0.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 8, 2025 License: GPL-3.0 Imports: 5 Imported by: 0

README

Jasmine 🫖

A simple framework to make building HTTP servers easier

What is Jasmine?

Jasmine is just a wrapper on Go's net/http library I made to stop having to rewrite the same code in every project.

Why is the project called Jasmine? I was thinking of a name, and saw my jasmine tea beside me. Yeah, no deeper thought.

Usage

go get github.com/ImCarsen/jasmine
package main

import (
    "github.com/ImCarsen/jasmine"
)

func main() {
    sig = make(chan os.Signal, 1)
    signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM,
    os.Interrupt)

    s := jasmine.Server{
        Address: ":8080",
        Routes: jasmine.Routes{
            Routes: map[string]http.HandlerFunc{
                "POST /test/{id}": test,
            }
            ProtectedRoutes: map[string]http.HandlerFunc{
                "POST /test/{id}": test,
            }
            AuthFunc: jasmine.DefaultAuthFunc
        }
    } 

   go s.Start()

   <-sig
   s.Stop()
}

func test(w http.ResponseWriter, r *http.Request) {
	id := r.PathValue("id")
	w.Write([]byte(fmt.Sprintf("ID: %v", id)))
}

Contributing

I welcome any contributions, as I don't do any perfomance testing, and the practices used for Jasmine are unlikely to be the best (it's just how I felt like writing it in the moment). So, if you feel like improving this project at all, feel free to open an issue, or PR.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultAuthFunc

func DefaultAuthFunc(next http.Handler) http.Handler

Default/Example AuthFunc. This doesn't actually do anything, you will want to create your own func for handling protected routes.

func NotImplemented added in v0.1.2

func NotImplemented(w http.ResponseWriter, r *http.Request)

The default handler for routes that are not implemented

Types

type AuthFunc

type AuthFunc func(http.Handler) http.Handler

type Routes

type Routes struct {
	Routes          map[string]http.HandlerFunc // path -> handler | Unprotected
	ProtectedRoutes map[string]http.HandlerFunc // path -> handler | Auto registers behind the AuthFunc
	AuthFunc        AuthFunc                    // func(http.HandlerFunc) http.Handler | ProtectedRoutes will not work without this
}

func (*Routes) RegisterRoutes

func (s *Routes) RegisterRoutes(mux *http.ServeMux, logger *zerolog.Logger)

Registers all routes supplied

type Server

type Server struct {
	Address string
	Routes  Routes
	Muxer   *http.ServeMux

	Logger *zerolog.Logger
	// contains filtered or unexported fields
}

func (*Server) Start

func (s *Server) Start()

func (*Server) Stop

func (s *Server) Stop()

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL