why

package module
v0.0.0-...-ebfccbf Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2019 License: MIT Imports: 21 Imported by: 0

README

why

Documentation

One night after having to write some PHP I was asking myself if it was possible to replicate the style of PHP with GO. This was the moment the idea for why was born. I wanted to create a PHP-Like server and scripting language that is extendable through GO and can be used to create websites. This is merely a fun project and shouldn’t be taken too seriously.

Scripting Language

Instead of writing my own scripting language from scratch I decided to use Tengo as a base. Tengo is easiliy extendable and the syntax is not too far from GO. I use the unstable tengo2 branch because I needed the possibility to call tengo functions from go and not just the other way around. This is important so you can build more advanced extensions.

Example

1. Download the latest Release (or build the Server)

Download the latest release for your platform from the Releases page (or get the package and build the main.go in cmd/)

2. Create a config.json

In the directory where the binary of the server is located create the following config:

{
  "PublicDir": "./public",
  "BindAddress": ":8765"
}
3. Create the /public directory

This directory will contain your scripts and static files.

4. Create /public/index.tengo

This will be the example script that we want to run. Let's imagine we want to print all the GET parameter and their values into our webpage. Paste the following script into the created file.

<!DOCTYPE html>
<html lang="en">
  <title>GET Example</title>
  <body>

    <!?
    
      http.write("Get Parameter:<br>")
      keys := http.GET.keys()
      for i := 0; i < len(keys); i++ {
          http.write("name=", keys[i], ", value=", http.GET.param(keys[i]), "<br>")
      }

    ?!>
    
  </body>
</html>
5. Start the Server

Start ./why (or ./cmd if you built it yourself) and visit http://127.0.0.1:8765/index?param_1=test&param_2=another_test. You should see the following in your Browser:

Get Parameter:
name=param_1, value=test
name=param_2, value=another_test

Default Variables & Functions

  • http.method: Contains the http method of the current request (e.g. POST, GET...).
  • http.full_uri: Contains the full url of the current request.
  • http.path: Contains only the path of the current request.
  • http.host: Contains the hostname or hostname:port of the current request.
  • http.ip: Contains the IP of the client that made the current request.
  • http.proto: HTTP protocol version of the current request.
  • http.status_code(<int>): This will set the status code of the response.
  • http.write(...): Variadic function that will write into the document. This is like echo in php.
  • http.overwrite(...): Variadic function that will overwrite all content that was written to the document before.
  • http.escape(<string>): Escapes the string. Can be used to avoid XSS.
  • http.body(): Will return the raw post body data.
  • http.die(): Will halt the execution of the script and finish the request.
GET
  • http.GET.keys(): Returns a list of all the present GET parameters.
  • http.GET.param(<string>): Returns the value of a GET parameter.
POST
  • http.POST.keys(): Returns a list of all the present POST parameters.
  • http.POST.param(<string>): Returns the value of a POST parameter.
HEADER
  • http.HEADER.keys(): Returns a list of all the present request headers.
  • http.HEADER.param(<string>): Returns the value of a header entry.
  • http.HEADER.set(<string>, <string>): Set's a response header.
COOKIES
  • http.COOKIES.all(): Returns all the cookies.
  • http.COOKIES.param(<string>): Returns a cookie by key.
  • http.COOKIES.set(<object>): Set's a cookie.

Extensibility

Adding custom variables and functions to the scripting engine can be done via the Extension interface. With the help of Extensions it's possible to add adapters for Databases and various other things.

Available Extensions

  • bbolt (docs): Key-Value Storage.
  • jwt (docs): Generate JWT's and validate and extract data from them.

More Examples?

Take a look at the examples/ folder. I will keep adding simple usage examples there to illustrate the functionality of why.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ToError

func ToError(err error) objects.Object

ToError creates a tango error object from a error. This can be used inside of extension functions to quickly create a error that can be returned.

func Transpile

func Transpile(in io.Reader, out io.Writer) error

Transpile will convert a html document that contains script tags <!? ... ?!> into a fully working tengo script. Html will be wrapped into http.write("...") calls.

Types

type Config

type Config struct {
	PublicDir   string
	EnableError bool
}

Config represents the configuration of a why server.

type Extension

type Extension interface {
	// Name should return the name of the plugin.
	Name() string

	// Init will be called one time before starting the server.
	Init() error

	// Shutdown will be called after server shutdown.
	Shutdown() error

	// Vars will return a array of global variables that the extension will insert.
	// If you try to set a variable inside the Hook function that isn't specified here
	// a error will occur. The names are needed so compiled scripts can be cached.
	Vars() []string

	// Hook will be called on each http request.
	Hook(sc *script.Compiled, w io.Writer, resp http.ResponseWriter, r *http.Request) error
}

Extension defines a why plugin. This can be used to add functionality like database access from inside the scripts.

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server represents a instance of the why server.

func New

func New(conf *Config) *Server

New creates a new why server.

func (*Server) AddExtension

func (s *Server) AddExtension(e Extension) error

AddExtension adds a new extension to the server. This function can only be called before when the server is not running.

func (*Server) Shutdown

func (s *Server) Shutdown() error

Shutdown will try to shut the server down.

func (*Server) Start

func (s *Server) Start(address string) error

Start starts the server and binds it to the given address.

Directories

Path Synopsis
extensions
bbolt
bbolt represents a bbolt extension for the why server.
bbolt represents a bbolt extension for the why server.
jwt
jwt represents a JSON Web Token extension for the why server.
jwt represents a JSON Web Token extension for the why server.

Jump to

Keyboard shortcuts

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