Socket
Socket
Sign inDemoInstall

github.com/enorith/http

Package Overview
Dependencies
14
Maintainers
0
Alerts
File Explorer

Install Socket

Protect your apps from supply chain attacks

Install

github.com/enorith/http

    v1.1.14

Version published
Maintainers
0

Readme

# Http component for [Enorith](https://github.com/enorith/framework)

## Usage

### Basic example

```golang
package main

import (
	"github.com/enorith/container"
	"github.com/enorith/http"
	"github.com/enorith/http/content"
	"github.com/enorith/http/contracts"
	"github.com/enorith/http/router"
	"log"
	"fmt"
)

type FooRequest struct {
	content.Request
    // input injection and validation
	Foo string `input:"foo" validate:"required"`
	File contracts.UploadFile `file:"file"`
}

func main() {
	s := http.NewServer(func() *container.Container {
		return container.New() // runtime IoC container
	}, true)
	
	e := s.Serve(":10800", func(ro *router.Wrapper, k *http.Kernel) {
		//k.OutputLog = true
		ro.HandleGet("/", func(r contracts.RequestContract) contracts.ResponseContract {

			return content.TextResponse("ok", 200)
		})

		// request injection
		ro.Get("/foo", func(r FooRequest) contracts.ResponseContract {

			return content.TextResponse("input foo: " + r.Foo, 200)
		})

		// param injection
		ro.Get("/:id", func(id content.ParamUint64) string {

			return fmt.Sprintf("input id: %d", id)
		})
	})

	if e != nil {
		log.Fatalf("serve error: %v", e)
	}
}
```
### Middleware

```golang
package foo

import (
	"github.com/enorith/http"
	"github.com/enorith/http/contracts"
	"github.com/enorith/http/router"
)

type FooMiddleware struct {

}

func (FooMiddleware) Handle(r contracts.RequestContract, next http.PipeHandler) contracts.ResponseContract {
	//TODO: before request handled

	resp := next(r)

	//TOD: after request handled
	return resp
}


func Handler(ro *router.Wrapper, k *http.Kernel) {
	k.SetMiddleware([]http.RequestMiddleware{ // global middleware
		FooMiddleware{},
	})

	k.SetMiddlewareGroup(map[string][]http.RequestMiddleware{
		"foo.mid": {FooMiddleware{}}
	})

	ro.Get("foo", fun() string { return "bar"}).Middleware("foo.mid")
}
```
### Working with Container

```golang
package main

import (
	"reflect"

	"github.com/enorith/container"
	"github.com/enorith/http"
	"github.com/enorith/http/contracts"
	"github.com/enorith/http/router"
)

type FooStruct struct {
	Bar string
}

type FooService struct {
	Foo FooStruct // construct injection
}


func (fs FooService) GetBar() string {
	return fs.Foo.Bar
}

func main() {
	srv := http.NewServer(func(request contracts.RequestContract) container.Interface {
		con := container.New()

		con.BindFunc(FooStruct{}, func(c container.Interface) (reflect.Value, error) { // bind instance
			return reflect.ValueOf(FooStruct{Bar: "baz"}), nil
		}, false)
		return con
	}, true)

	srv.Serve(":8000", func(rw *router.Wrapper, k *http.Kernel) {


		rw.Get("foo", func(fs FooService) string { // parameter injection
			return fs.GetBar()
		})
	})
}

```

## TODO

- [x] Get client ip behand proxy
- [ ] Validation (incomplete)
- [x] Logging

FAQs

Last updated on 12 Feb 2024

Did you know?

Socket installs a GitHub app to automatically flag issues on every pull request and report the health of your dependencies. Find out what is inside your node modules and prevent malicious activity before you update the dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc