Categorygithub.com/davidvaini/web
modulepackage
0.0.0-20210302160212-7988c9115fd7
Repository: https://github.com/davidvaini/web.git
Documentation: pkg.go.dev

# README

web.go

web.go is the simplest way to write web applications in the Go programming language. It's ideal for writing simple, performant backend web services.

Overview

web.go should be familiar to people who've developed websites with higher-level web frameworks like sinatra or web.py. It is designed to be a lightweight web framework that doesn't impose any scaffolding on the user. Some features include:

  • Routing to url handlers based on regular expressions
  • Secure cookies
  • Support for fastcgi and scgi
  • Web applications are compiled to native code. This means very fast execution and page render speed
  • Efficiently serving static files

Installation

Make sure you have the a working Go environment. See the install instructions. web.go targets the Go release branch.

To install web.go, simply run:

go get github.com/hoisie/web

To compile it from source:

git clone git://github.com/hoisie/web.git
cd web && go build

Example

package main
    
import (
    "github.com/hoisie/web"
)
    
func hello(val string) string { return "hello " + val } 
    
func main() {
    web.Get("/(.*)", hello)
    web.Run("0.0.0.0:9999")
}

To run the application, put the code in a file called hello.go and run:

go run hello.go

You can point your browser to http://localhost:9999/world .

Getting parameters

Route handlers may contain a pointer to web.Context as their first parameter. This variable serves many purposes -- it contains information about the request, and it provides methods to control the http connection. For instance, to iterate over the web parameters, either from the URL of a GET request, or the form data of a POST request, you can access ctx.Params, which is a map[string]string:

package main

import (
    "github.com/hoisie/web"
)
    
func hello(ctx *web.Context, val string) { 
    for k,v := range ctx.Params {
		println(k, v)
	}
}   
    
func main() {
    web.Get("/(.*)", hello)
    web.Run("0.0.0.0:9999")
}

In this example, if you visit http://localhost:9999/?a=1&b=2, you'll see the following printed out in the terminal:

a 1
b 2

Documentation

API docs are hosted at http://webgo.io

If you use web.go, I'd greatly appreciate a quick message about what you're building with it. This will help me get a sense of usage patterns, and helps me focus development efforts on features that people will actually use.

About

web.go was written by Michael Hoisie.

# Functions

Close stops the main server.
No description provided by the author
Delete adds a handler for the 'DELETE' http method in the main server.
Get adds a handler for the 'GET' http method in the main server.
No description provided by the author
Adds a custom handler.
Match adds a handler for an arbitrary http method in the main server.
No description provided by the author
NewCookie is a helper method that returns a new http.Cookie object.
No description provided by the author
No description provided by the author
Post
Post adds a handler for the 'POST' http method in the main server.
Process invokes the main server's routing system.
Put adds a handler for the 'PUT' http method in the main server.
Run starts the web application and serves HTTP requests for the main server.
RunFcgi starts the web application and serves FastCGI requests for the main server.
RunScgi starts the web application and serves SCGI requests for the main server.
RunTLS starts the web application and serves HTTPS requests for the main server.
SetLogger sets the logger for the main server.
No description provided by the author
Slug is a helper function that returns the URL slug for string s.
Urlencode is a helper method that converts a map into URL-encoded form data.
Adds a handler for websockets.

# Variables

Config is the configuration of the main server.

# Structs

A Context object is created for every incoming HTTP request, and is passed to handlers as an optional first argument.
Server represents a web.go server.
ServerConfig is configuration for server objects.
No description provided by the author