Skip to content

Commit

Permalink
Merge pull request #10 from lanvard/develop
Browse files Browse the repository at this point in the history
Boot routing and match the request
  • Loading branch information
Reindert authored Dec 3, 2019
2 parents e376c52 + 7650c4d commit af965f2
Show file tree
Hide file tree
Showing 27 changed files with 468 additions and 53 deletions.
9 changes: 9 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions Gopkg.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


[[constraint]]
name = "github.com/joho/godotenv"
version = "^1.3.0"
Expand All @@ -11,3 +9,7 @@
[[constraint]]
branch = "master"
name = "github.com/jinzhu/copier"

[[constraint]]
name = "github.com/gorilla/mux"
version = "^1.7.0"
63 changes: 50 additions & 13 deletions config/app.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
package config

import (
"lanvard/interface/decorator"
"lanvard/src/app/providers"
. "lanvard/support"
"path/filepath"
"runtime"
)

var App = struct {
Name string
Env string
Debug bool
Url string
AssetUrl string
LineSeparator string
BasePath string
Timezone string
Locale string
FallbackLocale string
FakerLocale string
Key string
Cipher string
Name string
Env string
Debug bool
Url string
AssetUrl string
LineSeparator string
BasePath string
Timezone string
Locale string
FallbackLocale string
FakerLocale string
Key string
Cipher string
RegisterProviders []decorator.RegisterServiceProvider
BootProviders []decorator.BootServiceProvider
}{

/*
Expand Down Expand Up @@ -173,6 +177,39 @@ var App = struct {
|
*/
Cipher: "AES-256-CBC",

/*
|--------------------------------------------------------------------------
| Autoloaded Register Service Providers
|--------------------------------------------------------------------------
|
| The service providers listed here will be automatically loaded on the
| request to your application. Within the register method, you should only
| bind things into the service container. You should never attempt to
| register any event listeners, routes, or any other piece of functionality
| within the register method. Otherwise, you may accidentally use a service
| that is provided by a service provider which has not loaded yet.
|
*/
RegisterProviders: []decorator.RegisterServiceProvider{},

/*
|--------------------------------------------------------------------------
| Autoloaded Boot Service Providers
|--------------------------------------------------------------------------
|
| This method is called after all other service providers have been
| registered, meaning you have access to all other services that have been
| registered by the framework. Feel free to add your own services to this
| slice to grant expanded functionality to your applications.
|
| You can have a service provider with a register and a boot method. Than
| you have to add this service to RegisterProviders and BootProviders
|
*/
BootProviders: []decorator.BootServiceProvider{
providers.RouteServiceProvider{},
},
}

func getBasePath() string {
Expand Down
5 changes: 0 additions & 5 deletions foundation/bootstrap/load_environment_variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,12 @@ package bootstrap
import (
"github.com/joho/godotenv"
"lanvard/foundation"
"lanvard/interface/decorator"
)

type LoadEnvironmentVariables struct {
environmentVariables map[string]string
}

func BootLoadEnvironmentVariables() decorator.Bootstrap {
return LoadEnvironmentVariables{}
}

func (l LoadEnvironmentVariables) Bootstrap(app foundation.Application) foundation.Application {
file := app.BasePath.EnvironmentFile()
err := godotenv.Load(file)
Expand Down
25 changes: 24 additions & 1 deletion foundation/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,33 @@ type Container struct {
func NewContainer() Container {
containerStruct := Container{}
containerStruct.bindings = make(bindings)
containerStruct.instances = make(instances)

return containerStruct
}

func (c Container) Copy() Container {
container := NewContainer()

for key, value := range c.bindings {
container.bindings[key] = value
}

for key, value := range c.aliases {
container.aliases[key] = value
}

for key, value := range c.abstractAliases {
container.abstractAliases[key] = value
}

for key, value := range c.instances {
container.instances[key] = value
}

return container
}

// Determine if the given abstract type has been bound.
func (c *Container) Bound(abstract string) bool {
_, bind := c.bindings[abstract]
Expand Down Expand Up @@ -63,7 +86,7 @@ func (c *Container) Singleton(abstract interface{}, concrete interface{}) {
}

// Register an existing instance as shared in the container.
func (c Container) Instance(abstract interface{}, instance interface{}) {
func (c *Container) Instance(abstract interface{}, instance interface{}) {
abstractName := support.Name(abstract)

c.removeAbstractAlias(abstractName)
Expand Down
24 changes: 24 additions & 0 deletions foundation/http/controllers/user_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package controllers

import (
"lanvard/http"
"lanvard/routing"
)

var User = struct {
Index routing.ControllerMethod
Store routing.ControllerMethod
Destroy routing.ControllerMethod
}{
Index: func(request http.Request) http.Response {
return http.Json("{\"test Index\": 123}")
},

Store: func(request http.Request) http.Response {
return http.Json("{\"test Store\": 123}")
},

Destroy: func(request http.Request) http.Response {
return http.Json("{\"test Destroy\": 123}")
},
}
9 changes: 5 additions & 4 deletions foundation/http/kernel.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package http
import (
"lanvard/foundation"
"lanvard/http"
"lanvard/routing/router"
"lanvard/src/app/http/decorator"
"lanvard/src/app/http/middleware"
)

type Kernel struct {
App foundation.Application
Router router.Router
Middleware []middleware.PipeInterface
}

Expand All @@ -35,9 +37,8 @@ func (k Kernel) Bootstrap() foundation.Application {
return k.App
}

func (k Kernel) dispatchToRouter() func(request http.Request) http.Response {

return func(r http.Request) http.Response {
return http.NewResponse().SetContent("ResponseTest")
func (k Kernel) dispatchToRouter() middleware.Destination {
return func(request http.Request) http.Response {
return router.NewRouter(k.App).DispatchToRoute(request)
}
}
14 changes: 7 additions & 7 deletions http/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,17 @@ import (

type Request struct {
App foundation.Application
original http.Request
Original http.Request
body []byte
}

func NewRequest(app foundation.Application, request http.Request) Request {
return Request{App: app, original: request}
}

func (r Request) Original() http.Request {
return r.original
return Request{App: app, Original: request}
}

func (r Request) Content() string {
if r.body == nil {
bodyBytes, err := ioutil.ReadAll(r.original.Body)
bodyBytes, err := ioutil.ReadAll(r.Original.Body)
r.body = bodyBytes
if err != nil {
panic(err)
Expand All @@ -37,3 +33,7 @@ func (r Request) SetContent(content string) Request {

return r
}

func (r Request) GetMethod() string {
return r.Original.Method
}
4 changes: 4 additions & 0 deletions http/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ func NewResponse() Response {
return Response{}
}

func Json(content string) Response {
return Response{content: content}
}

func (r Response) Content() string {
return r.content
}
Expand Down
2 changes: 1 addition & 1 deletion interface/application/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ type Container interface {

// Register a shared binding in the container.
singleton()
}
}
7 changes: 7 additions & 0 deletions interface/decorator/boot_service_provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package decorator

import "lanvard/foundation"

type BootServiceProvider interface {
Boot(app foundation.Application) foundation.Application
}
7 changes: 7 additions & 0 deletions interface/decorator/register_service_provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package decorator

import "lanvard/foundation"

type RegisterServiceProvider interface {
Register(app foundation.Application) foundation.Application
}
8 changes: 4 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package main

import (
"lanvard/foundation/http"
http2 "lanvard/http"
foundation "lanvard/foundation/http"
appHttp "lanvard/http"
httpInterface "lanvard/interface/http"
"lanvard/src/bootstrap"
"log"
Expand Down Expand Up @@ -60,8 +60,8 @@ func handleKernel(response net.ResponseWriter, request *net.Request) {
| and wonderful application we have prepared for them.
|
*/
kernel := app.Make((*httpInterface.Kernel)(nil)).(http.Kernel)
appResponse := kernel.Handle(http2.NewRequest(app, *request))
kernel := app.Make((*httpInterface.Kernel)(nil)).(foundation.Kernel)
appResponse := kernel.Handle(appHttp.NewRequest(app, *request))

println("appResponse")
println(appResponse.Content())
Expand Down
16 changes: 16 additions & 0 deletions routing/creators.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package routing

func createRoutes(methods []string, uri string, controller ControllerMethod) RouteCollection {
routes := NewRouteCollection()
for _, method := range methods {
route := NewRoute(uri, method, controller)
routes.Push(route)
}

return routes
}

func createRoute(method string, uri string, controller ControllerMethod) RouteCollection {
methods := []string{method}
return createRoutes(methods, uri, controller)
}
55 changes: 55 additions & 0 deletions routing/methods.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package routing

import (
"lanvard/http"
net "net/http"
)

type ControllerMethod func(http.Request) http.Response

// All of the methods supported by the router.
var allMethods = []string{
net.MethodGet,
net.MethodHead,
net.MethodPost,
net.MethodPut,
net.MethodPatch,
net.MethodDelete,
net.MethodOptions,
}

// Register new GET routes
func Get(uri string, controller ControllerMethod) RouteCollection {
methods := []string{net.MethodGet, net.MethodHead}
return createRoutes(methods, uri, controller)
}

// Register new POST routes
func Post(uri string, controller ControllerMethod) RouteCollection {
return createRoute(net.MethodPost, uri, controller)
}

// Register new PUT routes
func Put(uri string, controller ControllerMethod) RouteCollection {
return createRoute(net.MethodPut, uri, controller)
}

// Register new PATCH routes
func Patch(uri string, controller ControllerMethod) RouteCollection {
return createRoute(net.MethodPatch, uri, controller)
}

// Register new DELETE routes
func Delete(uri string, controller ControllerMethod) RouteCollection {
return createRoute(net.MethodDelete, uri, controller)
}

// Register new OPTIONS routes
func Options(uri string, controller ControllerMethod) RouteCollection {
return createRoute(net.MethodOptions, uri, controller)
}

// Register a new route responding to all methods
func Any(uri string, controller ControllerMethod) RouteCollection {
return createRoutes(allMethods, uri, controller)
}
Loading

0 comments on commit af965f2

Please sign in to comment.