ProductPromotion
Logo

Go.Lang

made by https://0x3d.site

GitHub - twharmon/goweb: Lightweight web framework based on net/http.
Lightweight web framework based on net/http. Contribute to twharmon/goweb development by creating an account on GitHub.
Visit Site

GitHub - twharmon/goweb: Lightweight web framework based on net/http.

GitHub - twharmon/goweb: Lightweight web framework based on net/http.

Goweb

Light weight web framework based on net/http.

Includes

  • routing
  • middleware
  • logging

Goweb aims to

  1. rely only on the standard library as much as possible
  2. be flexible
  3. perform well

Usage

See examples.

Basic

package main

import (
	"github.com/twharmon/goweb"
)

func main() {
    app := goweb.New()
    app.GET("/hello/{name}", hello)
    app.Run(":8080")
}

func hello(c *goweb.Context) goweb.Responder {
    return c.JSON(http.StatusOK, goweb.Map{
        "hello": c.Param("name"),
    })
}

Logging

package main

import (
	"github.com/twharmon/goweb"
)

func main() {
    app := goweb.New()
	app.RegisterLogger(newLogger(goweb.LogLevelInfo))
    app.GET("/hello/{name}", hello)
    app.Run(":8080")
}

func hello(c *goweb.Context) goweb.Responder {
    c.LogInfo("param name:", c.Param("name"))
    // logs "[INFO] /hello/Gopher param name: Gopher"
    return c.JSON(http.StatusOK, goweb.Map{
        "hello": c.Param("name"),
    })
}

type logger struct{
	level goweb.LogLevel
}

func newLogger(level goweb.LogLevel) goweb.Logger {
	return &logger{level: level}
}

func (l *logger) Log(c *goweb.Context, logLevel goweb.LogLevel, messages ...interface{}) {
	if l.level > logLevel {
		return
	}
	prefix := fmt.Sprintf("[%s] %s", logLevel, c.Request.URL.Path)
	messages = append([]interface{}{prefix}, messages...)
	log.Println(messages...)
}

Auto TLS

package main

import (
	"context"
	"errors"
	"log"
	"net/http"

	"github.com/twharmon/goweb"
	"golang.org/x/crypto/acme/autocert"
)

func main() {
	app := goweb.New()
	app.GET("/", func(c *goweb.Context) goweb.Responder {
		return c.JSON(http.StatusOK, goweb.Map{
			"hello": "world",
		})
	})
	serveTLS(app)
}

func serveTLS(app *goweb.Engine) {
	m := &autocert.Manager{
		Cache:  autocert.DirCache(".certs"),
		Prompt: autocert.AcceptTOS,
		HostPolicy: func(_ context.Context, host string) error {
			if host == "example.com" {
				return nil
			}
			return errors.New("host not configured")
		},
	}
	go http.ListenAndServe(":http", m.HTTPHandler(nil))
	s := &http.Server{
		Addr:      ":https",
		TLSConfig: m.TLSConfig(),
		Handler:   app,
	}
	log.Fatalln(s.ListenAndServeTLS("", ""))
}

Easily extendable

See serving files, template rendering, tls, and logging for examples.

Documentation

For full documentation see pkg.go.dev.

Benchmarks

BenchmarkGinPlaintext-10         	 2706439	       440.0 ns/op	    1040 B/op	       9 allocs/op
BenchmarkEchoPlaintext-10        	 2549317	       470.7 ns/op	    1024 B/op	      10 allocs/op
BenchmarkGowebPlaintext-10       	 1584044	       756.6 ns/op	    1456 B/op	      16 allocs/op
BenchmarkGorillaPlaintext-10     	 1000000	      1027 ns/op	    1744 B/op	      17 allocs/op
BenchmarkMartiniPlaintext-10     	  223416	      5364 ns/op	    1789 B/op	      39 allocs/op

BenchmarkGowebJSON-10            	   25945	     46359 ns/op	   50905 B/op	      15 allocs/op
BenchmarkEchoJSON-10             	   25664	     46571 ns/op	   50641 B/op	      10 allocs/op
BenchmarkGorillaJSON-10          	   25716	     46857 ns/op	   51115 B/op	      16 allocs/op
BenchmarkGinJSON-10              	   23697	     50697 ns/op	  100836 B/op	      10 allocs/op
BenchmarkMartiniJSON-10          	   22746	     52613 ns/op	   52665 B/op	      41 allocs/op

BenchmarkGinPathParams-10        	  914139	      1273 ns/op	    1849 B/op	      25 allocs/op
BenchmarkEchoPathParams-10       	  889014	      1309 ns/op	    1865 B/op	      25 allocs/op
BenchmarkGowebPathParams-10      	  627306	      1902 ns/op	    2570 B/op	      33 allocs/op
BenchmarkGorillaPathParams-10    	  552852	      2144 ns/op	    2874 B/op	      32 allocs/op
BenchmarkMartiniPathParams-10    	  188500	      6215 ns/op	    2641 B/op	      47 allocs/op

Contribute

Create a pull request to contribute to Goweb.

Articles
to learn more about the golang concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here ๐Ÿ”ฅ.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here ๐Ÿ”ฅ.

Queries
or most google FAQ's about GoLang.

mail [email protected] to add more queries here ๐Ÿ”.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory