Giter Site home page Giter Site logo

galaxy's Introduction

Galaxy - Simple web framework for Go

GoDoc

Galaxy is a simple web framework for Go. This is inspired by Maritini but this does not rely on Dependency Injection at all. Go web applications can be built in a succinct manner by using Galaxy.

Getting Started

Here is a very small example code of a web application using Galaxy.

package main

import (
	"fmt"

	"github.com/yosssi/galaxy/core"
)

func main() {
	app := core.NewApplication()
	app.Get("/", func(ctx *core.Context) error {
		fmt.Fprintf(ctx.Res, "Hello world!")
		return ctx.Next()
	})
	app.Run(":3000")
}

Features

  • Simple to use
  • Inspired by Martini but does not reply on Dependency Injection
  • Modular design - Every package has a single function. You can construct your own framework by choosing and importing packages as you like.
  • Handler based framework - You can define the flow of the framework processes by stacking handlers.

Basics

Handler

A handler is a function whose type is core.Handler. This is the smallest unit of the processing flow.

Context

A context is a request context whose type is core.Context. It is generated in every request. It is passed to every handlers. A request (and application) context data is passed to every handlers via this struct.

Routing

app.Get("/", func(ctx *core.Context) error {
	return ctx.Next()
})

app.Patch("/", func(ctx *core.Context) error {
	return ctx.Next()
})

app.Post("/", func(ctx *core.Context) error {
	return ctx.Next()
})

app.Put("/", func(ctx *core.Context) error {
	return ctx.Next()
})

app.Delete("/", func(ctx *core.Context) error {
	return ctx.Next()
})

app.Head("/", func(ctx *core.Context) error {
	return ctx.Next()
})

app.Options("/", func(ctx *core.Context) error {
	return ctx.Next()
})

Route handlers can be stacked.

app.Get(
	"/",
	func(ctx *core.Context) error {
		fmt.Println("handler #1")
		return ctx.Next()
	},
	func(ctx *core.Context) error {
		fmt.Println("handler #2")
		return ctx.Next()
	},
)

Pre & post handlers

You can define pre & post handlers which are invoked before / after route handlers. These handlers are invoke in any request.

app.UsePre(func(ctx *core.Context) error {
	fmt.Println("pre handler")
	return ctx.Next()
})

app.Get("/", func(ctx *core.Context) error {
	fmt.Println("route handler")
	return ctx.Next()
})

app.UsePost(func(ctx *core.Context) error {
	fmt.Println("post handler")
	return ctx.Next()
})

Provide the application / requext context data to every handler

You can provide the application / requext context data to every handler.

app := core.NewApplication()

// Set the data to the application context.
if err := app.SetData("text1", "this is an application context data"); err != nil {
	panic(err)
}

app.UsePre(func(ctx *core.Context) error {
	// Get the data from the application context.
	v, ok := app.GetData("text1")

	if ok {
		fmt.Println(v.(string))
	}

	// Set the data to the request context.
	if err := ctx.SetData("text1", "this is a request context data"); err != nil {
		return err
	}

	return ctx.Next()
})

app.UsePre(func(ctx *core.Context) error {
	// Get the data from the request context.
	v, ok := ctx.GetData("text1")

	if ok {
		fmt.Println(v.(string))
	}

	return ctx.Next()
})

Packages

  • core - core functions of Galaxy web framework
  • logger - handler for logging
package main

import (
	"fmt"

	"github.com/yosssi/galaxy/core"
	"github.com/yosssi/galaxy/logger"
)

func main() {
	app := core.NewApplication()
	app.UsePre(logger.Logger())
	app.Get("/", func(ctx *core.Context) error {
		fmt.Fprintf(ctx.Res, "Hello world")
		return ctx.Next()
	})
	app.Run(":3000")
}
  • static - handler for serving static files
package main

import (
	"github.com/yosssi/galaxy/core"
	"github.com/yosssi/galaxy/static"
)

func main() {
	app := core.NewApplication()
	app.UsePre(static.Static("public"))
	app.Run(":3000")
}

galaxy's People

Contributors

yosssi avatar

Stargazers

 avatar Oz Tiram avatar Attila Oláh avatar Baldur Kristinsson avatar Rogerio Marques avatar Flynn Wang avatar Jake Scott avatar Damon Zhao avatar John avatar Peter Nguyen avatar  avatar Satish Puranam avatar Jonathan Pentecost avatar Brian Ketelsen avatar  avatar

Watchers

 avatar James Cloos avatar  avatar  avatar

Forkers

mirannda

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.