Skip to content
/ web Public

Web is a small helper library in Go for building web servers.

License

Notifications You must be signed in to change notification settings

SlyMarbo/web

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

web

Example use:

package main

import (
	"github.com/SlyMarbo/web"
	"io"
	"net/http"
	"os"
)

// Simple 404 handler.
func notFound(w http.ResponseWriter, r *http.Request) {
	http.NotFound(w, r)
}

// HTML-specific handler.
func serveHTML(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "text/html; charset=utf-8")
	serveContent(w, r)
}

// Solid handler for generic content.
func serveContent(w http.ResponseWriter, r *http.Request) {
	// Add GZIP-encoding if supported by the client.
	writer := web.NewGzipResponseWriter(w)
	defer writer.Close()

	// Ensure the file exists.
	f, err := os.Open("." + r.RequestURI)
	if err != nil {
		notFound(w, r)
	}
	stat, err := f.Stat()
	if err != nil {
		notFound(w, r)
	}

	// Add caching.
	web.Cache(writer, stat.ModTime(), web.OneYear)

	// Send the file data. This will be compressed if allowed.
	_, err = io.Copy(writer, f)
	if err != nil {
		notFound(w, r)
	}
}

func main() {
	// Redirect http://example.com requests to https://example.com.
	redirector := web.NewSite("example.com", 80, notFound)
	redirector.Always(web.RedirectToHttpsHandler)

	site := web.NewSite("example.com", 443, notFound)
	site.Equals(web.Handler(serveHTML), "/", "/index.html")
	site.HasPrefix(web.Handler(serveContent), "/")

	err := new(web.Server).Add(redirector).Add(site).Serve()
	if err != nil {
		panic(err)
	}
}

About

Web is a small helper library in Go for building web servers.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages