Skip to content

cccaaannn/gohst

Repository files navigation

gohst

Go HTTP Server Tool

A simple http server

Go Reference codecov GitHub go.mod Go version GitHub top language GitHub repo size GitHub


Features

  1. Simple usage
  2. Path parsing
  3. TLS support
  4. Middlewares

Usage

Install package

go get github.com/cccaaannn/gohst

Minimal example

package main

import (
	"fmt"

	"github.com/cccaaannn/gohst"
)

func main() {
	server := gohst.CreateServer()

	server.AddHandler("GET /hi/:name", func(req *gohst.Request, res *gohst.Response) {
		name := req.Params["name"]
		res.Body = fmt.Sprintf(`
		<body>
			<h1>Hello %s!</h1>
		</body>
		`, name)
	})

	server.AddHandler("/*", func(req *gohst.Request, res *gohst.Response) {
		res.StatusCode = 404
		res.Body = `
		<body>
			<h1>404</h1>
		</body>
		`
	})

	stop, _ := server.ListenAndServe(":8080")
	<-stop
}

Other examples

  1. example/crud/main.go
  2. example/tls/main.go
  3. example/middleware/main.go

Development

Test

go test -v

Coverage

go test -coverprofile=coverage.out
go tool cover -html=coverage.out