goexpress

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2017 License: MIT Imports: 5 Imported by: 0

README

GoDoc

goexpress

An Express JS Style HTTP server implementation in Golang. The package make use of similar framework convention as they are in express-js. People switching from NodeJS to Golang often end up in a bad learning curve to start building their webapps, this project is meant to ease things up, its a light weight framework which can be extended to do add any number of functionality.

Hello World

package main
import (
  express "github.com/DronRathore/goexpress"
  request "github.com/DronRathore/goexpress/request"
  response "github.com/DronRathore/goexpress/response"
)

func main (){
  var app = express.Express()
  app.Get("/", func(req *request.Request, res *response.Response, next func()){
    res.Write("Hello World")
    // you can skip closing connection
  })
  app.Start("8080")
}

Router

The router works in the similar way as it does in the express-js. You can have named parameters in the URL or named + regex combo.

func main (){
  var app = express.Express()
  app.Get("/:service/:object([0-9]+)", func(req *request.Request, res *response.Response, next func()){
    res.JSON(req.Params)
  })
  app.Start("8080")
}

Note: You can also adhoc an express.Router() instance too much like it is done in expressjs

package main
import (
  express "github.com/DronRathore/goexpress"
  request "github.com/DronRathore/goexpress/request"
  response "github.com/DronRathore/goexpress/response"
)
var LibRoutes = func (){
  // create a new Router instance which works in similar way as app.Get/Post etc
  var LibRouter = express.Router()
  LibRouter.Get("/lib/:api_version", func(req *request.Request, res *response.Response, next func()){
    res.Json(req.Params["api_version"])
  })
  return *LibRoutes
}() // immediate invocation
func main(){
  var app = express.Express()
  app.Use(LibRoutes) // attaches the Library Routes
  app.Start("8080")
}

Middleware

You can write custom middlewares, wrappers in the similar fashion. Middlewares can be used to add websocket upgradation lib, session handling lib, static assets server handler

func main (){
  var app = express.Express()
  app.Use(func(req *request.Request, res *response.Response, next func()){
    req.Params["I-Am-Adding-Something"] = "something"
    next()
  })
  app.Get("/:service/:object([0-9]+)", func(req *request.Request, res *response.Response, next func()){
    // json will have the key added
    res.JSON(req.Params)
  })
  app.Start("8080")
}

ExpressInterface

You can pass around the instance of express struct across packages using this interface.

func main(){
  var app = express.Express()
  attachHandlers(app)
}
func attachHandlers(instance express.ExpressInterface){
  instance.Use(someMiddleware)
  instance.Set("logging", true)
}

Cookies

import (
  express "github.com/DronRathore/goexpress"
  request "github.com/DronRathore/goexpress/request"
  response "github.com/DronRathore/goexpress/response"
  http "net/http"
  Time "time"
)
func main (){
  var app = express.Express()
  app.Use(func(req *request.Request, res *response.Response, next func()){
    var cookie = &http.Cookie{
      Name: "name",
      Value: "value",
      Expires: Time.Unix(0, 0)
    }
    res.Cookie.Add(cookie)
    req.Params["session_id"] = req.Cookies.Get("session_id")
  })
  app.Get("/", func(req *request.Request, res *response.Response, next func()){
    res.Write("Hello World")
  })
  app.Start("8080")
}

Sending File

You can send a file by using the helper res.SendFile(url string, doNotSendCachedData bool)

func main (){
  var app = express.Express()
  
  app.Get("/public/:filename", func(req *request.Request, res *response.Response, next func()){
	res.SendFile(filename, false)
  })
  app.Start("8080")
}

Note: You can now also send an auto downloadable file too using res.Download api

/*
  @params:
    path: Full path to the file in local machine
    filename: The name to be sent to the client
*/
res.Download(path string, filename string)

Post Body

func main (){
  var app = express.Express()
  app.Use(func(req *request.Request, res *response.Response, next func()){
    res.Params["I-Am-Adding-Something"] = "something"
    next()
  })
  app.Post("/user/new", func(req *request.Request, res *response.Response, next func()){
    type User struct {
			Name string `json:"name"`
			Email string `json:"email"`
		}
		var list = &User{Name: req.Body["name"], Email: req.Body["email"]}
		res.JSON(list)
  })
  app.Start("8080")
}

JSON Post

JSON Post data manipulation in golang is slightly different from JS. You have to pass a filler to the decoder, the decoder assumes the data to be in the same format as the filler, if it is not, it throws an error.

func main (){
  var app = express.Express()
  app.Use(func(req *request.Request, res *response.Response, next func()){
    res.Params["I-Am-Adding-Something"] = "something"
    next()
  })
  app.Post("/user/new", func(req *request.Request, res *response.Response, next func()){
    type User struct {
			Name string `json:"name"`
			Email string `json:"email"`
		}
		var list User
		err := req.JSON.Decode(&list) 
		if err != nil {
			res.Error(400, "Invalid JSON")
		} else {
			res.JSON(list)
		}
  })
  app.Start("8080")
}

File Uploading

The response.Response struct has a GetFile() method, which reads a single file at a time, you can make looped calls to retrieve all the files, however this feature is not thoroughly tested, bugs can be reported for the same.

Testing

There are no testing added to this package yet, I am hoping to get my hands dirty in testing, if anyone can help me out with this, feel free to open a PR.

Contribution

  • If you want some common must have middlewares support, please open an issue, will add them.
  • Feel free to contribute. :)

Documentation

Overview

Package goexpress provides the actual hook that enables you to start building your application.

The basic Express() functions returns an instance for the express which can be further be used as an express hook.

app.Use(), app.Get(), app.Post(), app.Delete(), app.Push() app.Put() are the top level functions that can be used in the same fashion as the express-js ones are.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Express

func Express() *express

Returns a new instance of express

func Router

func Router() *router.Router

Returns a new instance of express Router

Types

type ExpressInterface added in v1.0.2

type ExpressInterface interface {
	Use(interface{}) *express
	Get(string, router.Middleware) *express
	Post(string, router.Middleware) *express
	Put(string, router.Middleware) *express
	Patch(string, router.Middleware) *express
	Delete(string, router.Middleware) *express
	SetProp(string, interface{}) *express
	GetProp(string, interface{}) interface{}
	Start(string) *express
}

Public Interface to allow access to express struct's member functions

Directories

Path Synopsis
Cookies Package helps reading and setting the cookie The cookie struct's instance is availaible to both goexpress.Request and goexpress.Response
Cookies Package helps reading and setting the cookie The cookie struct's instance is availaible to both goexpress.Request and goexpress.Response
Header Package, handles the Response & Request Header The package is responsible for setting Response headers and pushing the same on the transport buffer
Header Package, handles the Response & Request Header The package is responsible for setting Response headers and pushing the same on the transport buffer
Request package provides the request structure The package provides access to Headers, Cookies Query Params, Post Body and Upload Files
Request package provides the request structure The package provides access to Headers, Cookies Query Params, Post Body and Upload Files
Response package provides the core functionality of handling the client connection, chunked response and other features
Response package provides the core functionality of handling the client connection, chunked response and other features
Package Router, returns instance for express Router Functions defined here are extended by express.go itself Express Router takes the url regex as similar to the js one Router.Get("/:param") will return the param in Response.Params["param"]
Package Router, returns instance for express Router Functions defined here are extended by express.go itself Express Router takes the url regex as similar to the js one Router.Get("/:param") will return the param in Response.Params["param"]

Jump to

Keyboard shortcuts

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