Simple, secure and modern Go HTTP server to serve static sites, single-page applications or a file with ease

Overview

srv

Build Status Go Report Card GoDoc Conventional Commits License

srv is a simple, secure and modern HTTP server, written in Go, to serve static sites, single-page applications or a file with ease. You can use it through its command-line interface, Docker image or programmatically. As it's an http.Handler implementation, it should be easy to integrate it into your application. The following key features make srv unique and differentiable from the existing solutions and the http.FileServer implementation.

  • Programmatic API.
  • HTTP/2 and TLS support.
  • Custom error handler and pages.
  • Basic HTTP authentication.
  • Hide dot files by default.
  • Directory listing is disabled by default.
  • Encoding negotiation with support of gzip, Deflate and Brotli compression algorithms.

Table of Contents

Install

go get github.com/kevinpollet/srv                  # get dependency
go install -i github.com/kevinpollet/srv/cmd/srv   # build and install command-line interface bin

Usage

Command-line

srv can be use through its provided command-line. The following text is the output of the srv -help command.

srv [options]

Options:
-addr      The server address, "127.0.0.1:8080" by default.
-auth      The basic auth credentials (password must be hashed with bcrypt and escaped with '').
-authfile  The basic auth credentials file following the ".htpasswd" format.
-dir       The directory containing the files to serve, "." by default.
-cert      The TLS certificate.
-key       The TLS private key.
-help      Prints this text.

Docker

An official docker image is available on Docker Hub. The following Dockerfile shows how to use the provided base image to serve your static sites or files through a running Docker container. By default, the base image will serve all files available in the /var/www/ directory and listen for TCP connections on 8080.

FROM kevinpollet/srv:latest
COPY . /var/www/

Then, you can build and run your Docker image with the following commands. Your static site or files will be available on http://localhost:8080.

docker build . -t moby:latest
docker run -d -p 8080:8080 moby:latest

API

package main

import (
	"log"
	"net/http"

	"github.com/kevinpollet/srv"
	"github.com/kevinpollet/srv/middlewares"
)

func main() {
	customErrorHandler := func(fs http.FileSystem, rw http.ResponseWriter, err error) {
			log.Print(err)
			rw.WriteHeader(http.StatusInternalServerError)
	}

	http.Handle("/static", srv.NewFileServer("examples/hello",
		srv.WithAutoIndex(),
		srv.WithMiddlewares(middlewares.NewStripPrefixHandler("/static")),
		srv.WithErrorHandler(customErrorHandler),
	))

	log.Fatal(http.ListenAndServe(":8080", nil))
}

Examples

The examples directory contains the following examples:

  • hello — A simple static site that can be served from the command-line.
  • docker — A simple static site that can be served from a docker container.

Contributing

Contributions are welcome!

Want to file a bug, request a feature or contribute some code?

  1. Check out the Code of Conduct.
  2. Check for an existing issue corresponding to your bug or feature request.
  3. Open an issue to describe your bug or feature request.

License

MIT

Issues
  • fix(deps): bump github.com/andybalholm/brotli from 1.0.0 to 1.0.1

    fix(deps): bump github.com/andybalholm/brotli from 1.0.0 to 1.0.1

    Bumps github.com/andybalholm/brotli from 1.0.0 to 1.0.1.

    Commits
    • 729edfb Add documentation link.
    • 1b06c56 Check size of split.lengths instead of a nil check
    • c3da72a Faster bit writing.
    • ef7a421 Use a 64-bit store in writeBits.
    • 097c1c5 Use 32-bit loads in isMatch1 and isMatch5.
    • 8f8b186 Read multiple bytes in findMatchLengthWithLimit
    • a01a7b1 Reuse buffers and objects using sync.Pool
    • e2c5f21 Use len and cap instead of num_commands_ and cmd_alloc_size_.
    • 4b2775e Fix some ugly compound literals.
    • b2497e8 Revert "Use sort.Sort to sort Huffman trees."
    • Additional commits viewable in compare view

    Dependabot compatibility score

    Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


    Dependabot commands and options

    You can trigger Dependabot actions by commenting on this PR:

    • @dependabot rebase will rebase this PR
    • @dependabot recreate will recreate this PR, overwriting any edits that have been made to it
    • @dependabot merge will merge this PR after your CI passes on it
    • @dependabot squash and merge will squash and merge this PR after your CI passes on it
    • @dependabot cancel merge will cancel a previously requested merge and block automerging
    • @dependabot reopen will reopen this PR if it is closed
    • @dependabot close will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
    • @dependabot ignore this major version will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this minor version will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
    • @dependabot badge me will comment on this PR with code to add a "Dependabot enabled" badge to your readme

    Additionally, you can set the following in the .dependabot/config.yml file in this repo:

    • Update frequency
    • Out-of-range updates (receive only lockfile updates, if desired)
    • Security updates (receive only security updates, if desired)
    dependencies 
    opened by dependabot-preview[bot] 1
  • fix(deps): bump github.com/sirupsen/logrus from 1.6.0 to 1.7.0

    fix(deps): bump github.com/sirupsen/logrus from 1.6.0 to 1.7.0

    Bumps github.com/sirupsen/logrus from 1.6.0 to 1.7.0.

    Release notes

    Sourced from github.com/sirupsen/logrus's releases.

    Add new BufferPool and LogFunction APIs

    • a new buffer pool management API has been added
    • a set of <LogLevel>Fn() functions have been added
    • the dependency toward a windows terminal library has been removed
    Changelog

    Sourced from github.com/sirupsen/logrus's changelog.

    1.7.0

    Fixes:

    • the dependency toward a windows terminal library has been removed

    Features:

    • a new buffer pool management API has been added
    • a set of <LogLevel>Fn() functions have been added
    Commits

    Dependabot compatibility score

    Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


    Dependabot commands and options

    You can trigger Dependabot actions by commenting on this PR:

    • @dependabot rebase will rebase this PR
    • @dependabot recreate will recreate this PR, overwriting any edits that have been made to it
    • @dependabot merge will merge this PR after your CI passes on it
    • @dependabot squash and merge will squash and merge this PR after your CI passes on it
    • @dependabot cancel merge will cancel a previously requested merge and block automerging
    • @dependabot reopen will reopen this PR if it is closed
    • @dependabot close will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
    • @dependabot ignore this major version will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this minor version will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
    • @dependabot badge me will comment on this PR with code to add a "Dependabot enabled" badge to your readme

    Additionally, you can set the following in the .dependabot/config.yml file in this repo:

    • Update frequency
    • Out-of-range updates (receive only lockfile updates, if desired)
    • Security updates (receive only security updates, if desired)
    dependencies 
    opened by dependabot-preview[bot] 0
  • fix(deps): bump github.com/sirupsen/logrus from 1.5.0 to 1.6.0

    fix(deps): bump github.com/sirupsen/logrus from 1.5.0 to 1.6.0

    Bumps github.com/sirupsen/logrus from 1.5.0 to 1.6.0.

    Release notes

    Sourced from github.com/sirupsen/logrus's releases.

    Release v1.6.0

    Changelog

    Sourced from github.com/sirupsen/logrus's changelog.

    1.6.0

    Fixes:

    • end of line cleanup
    • revert the entry concurrency bug fix whic leads to deadlock under some circumstances
    • update dependency on go-windows-terminal-sequences to fix a crash with go 1.14

    Features:

    • add an option to the TextFormatter to completely disable fields quoting
    Commits
    • 60c74ad update CHANGELOG.md with 1.5.0 and 1.6.0 version contents
    • e8e563a Merge remote-tracking branch 'origin/master' into thlacroix-disable-quotes
    • 0fd458a complete documetation on TextFormatter.DisableQuote
    • 4d96c60 Merge branch 'disable-quotes' of https://github.com/thlacroix/logrus into thl...
    • a5b0247 Merge pull request #1136 from ialidzhikov/nit/line-endings
    • 163c051 Merge pull request #1137 from sirupsen/fix_crash_windows
    • e79215d update github.com/konsorten/go-windows-terminal-sequences dependency to v1.0.3
    • 4989a3f Change CRLF line endings to LF
    • aff00fe Adds additional test cases for DisableQuote
    • c7455de Adds flag to disable quotes in TextFormatter
    • Additional commits viewable in compare view

    Dependabot compatibility score

    Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


    Dependabot commands and options

    You can trigger Dependabot actions by commenting on this PR:

    • @dependabot rebase will rebase this PR
    • @dependabot recreate will recreate this PR, overwriting any edits that have been made to it
    • @dependabot merge will merge this PR after your CI passes on it
    • @dependabot squash and merge will squash and merge this PR after your CI passes on it
    • @dependabot cancel merge will cancel a previously requested merge and block automerging
    • @dependabot reopen will reopen this PR if it is closed
    • @dependabot close will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
    • @dependabot ignore this major version will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this minor version will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
    • @dependabot badge me will comment on this PR with code to add a "Dependabot enabled" badge to your readme

    Additionally, you can set the following in the .dependabot/config.yml file in this repo:

    • Update frequency
    • Out-of-range updates (receive only lockfile updates, if desired)
    • Security updates (receive only security updates, if desired)
    dependencies 
    opened by dependabot-preview[bot] 0
  • fix(deps): bump github.com/sirupsen/logrus from 1.4.2 to 1.5.0

    fix(deps): bump github.com/sirupsen/logrus from 1.4.2 to 1.5.0

    Bumps github.com/sirupsen/logrus from 1.4.2 to 1.5.0.

    Release notes

    Sourced from github.com/sirupsen/logrus's releases.

    v1.5.0

    This new release introduces:

    • Ability to DisableHTMLEscape when using the JSON formatter: sirupsen/logrus#524
    • Support/fixes for go 1.14
    • Many many bugfixes
    Commits

    Dependabot compatibility score

    Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


    Note: This repo was added to Dependabot recently, so you'll receive a maximum of 5 PRs for your first few update runs. Once an update run creates fewer than 5 PRs we'll remove that limit.

    You can always request more updates by clicking Bump now in your Dependabot dashboard.

    Dependabot commands and options

    You can trigger Dependabot actions by commenting on this PR:

    • @dependabot rebase will rebase this PR
    • @dependabot recreate will recreate this PR, overwriting any edits that have been made to it
    • @dependabot merge will merge this PR after your CI passes on it
    • @dependabot squash and merge will squash and merge this PR after your CI passes on it
    • @dependabot cancel merge will cancel a previously requested merge and block automerging
    • @dependabot reopen will reopen this PR if it is closed
    • @dependabot close will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
    • @dependabot ignore this major version will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this minor version will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
    • @dependabot badge me will comment on this PR with code to add a "Dependabot enabled" badge to your readme

    Additionally, you can set the following in the .dependabot/config.yml file in this repo:

    • Update frequency
    • Out-of-range updates (receive only lockfile updates, if desired)
    • Security updates (receive only security updates, if desired)
    dependencies 
    opened by dependabot-preview[bot] 0
Owner
Kevin Pollet
Software Engineer ∙ OSS Enthusiast ∙ Passionate learner and coder ∙ Go, TypeScript, Java, Node.js, Containers, Cloud and DevOps addict
Kevin Pollet
serve a static website as a .onion hidden service

hidden service server A CLI that will host a static website as a .onion hidden service. Comes with an additional binary that can be used to generate v

null 9 Apr 27, 2022
TFTP and HTTP server specifically designed to serve iPXE ROMs and scripts.

pixie TFTP and HTTP server specifically designed to serve iPXE ROMs and scripts. pixie comes embedded with the following ROMs provided by the iPXE pro

Adrian L Lange 13 Jun 27, 2022
HTTP proxy written in Go. COW can automatically identify blocked sites and use parent proxies to access.

COW (Climb Over the Wall) proxy COW 是一个简化穿墙的 HTTP 代理服务器。它能自动检测被墙网站,仅对这些网站使用二级代理。 English README. 当前版本:0.9.8 CHANGELOG 欢迎在 develop branch 进行开发并发送 pull

Chen Yufei 8.3k Jun 28, 2022
GoHTTPdirlist - Serve files and directories with Golang based http server

List Files and Directories with GoLang based HTTP Server TO MAKE IT WORK: go run

EMRE ASLAN 0 May 18, 2022
Pape-server - A small server written in golang to serve a random wallpaper.

pape-server I like to inject custom CSS themes into a lot of websites and electron apps, however browsers don't let websites access local disk through

null 0 Dec 31, 2021
Transparent TLS and HTTP proxy serve and operate on all 65535 ports, with domain regex whitelist and rest api control

goshkan Transparent TLS and HTTP proxy serve & operating on all 65535 ports, with domain regex whitelist and rest api control tls and http on same por

Sina Ghaderi 9 Mar 16, 2022
Parse any web page for URLs and return the HTTP response code of each one.

ParseWebPage - Fully Functional WebPage Parser Parse any web page for URLs and return the HTTP response code of each one. Creators ?? Steven Williams

null 0 Oct 25, 2021
`kawipiko` -- blazingly fast static HTTP server -- focused on low latency and high concurrency, by leveraging Go, `fasthttp` and the CDB embedded database

kawipiko -- blazingly fast static HTTP server kawipiko is a lightweight static HTTP server written in Go; focused on serving static content as fast an

Volution 14 Jun 22, 2022
Serve traffic (HTTP/gRPC) over SSH using Domain Sockets

Serve On SSH Introduction There is often a need to offer services for administrative purposes on servers or even for microservices that are running on

John Doak 7 Jan 9, 2022
Echo-server - An HTTP echo server designed for testing applications and proxies

echo-server An HTTP echo server designed for testing applications and proxies. R

Erik Cavalcanti 4 Jan 5, 2022
Static file server that service content required by dan's services

Static file server that service content required by dan's services.

Danang Galuh Tegar Prasetyo 0 Jan 20, 2022
Http-recorder - Application for record http response as static files

http-recorder This is a application for record http response as static files. Th

null 1 Mar 21, 2022
A quick and dirty but useful tool to download each text/html page from the wayback machine for a specific domain and search for keywords within the saved content

wayback-keyword-search A quick and dirty but useful tool to download each text/html page from the wayback machine for a specific domain and search for

null 41 Apr 20, 2022
🤖 Automatically scrape PortableApps.com (or official release page) and convert into Edgeless plugin package

Edgeless 自动插件机器人 2 简介 该项目是为了使用 Golang 重新实现 Edgeless 自动插件机器人 特性 (WIP) 完全兼容 Edgeless 自动插件机器人,包括 Tasks,以实现无缝迁移 更快的构建速度 更好的代码结构 更高的拓展性 工作进度 截止至 2021/11/28

Hydrogen 2 Dec 7, 2021
Http-server - A HTTP server and can be accessed via TLS and non-TLS mode

Application server.go runs a HTTP/HTTPS server on the port 9090. It gives you 4

Vedant Pareek 0 Feb 3, 2022
fetch and serve papers in p2p network

sci-hub P2P A project aims to fetch paper from the BitTorrent network. This is not a cli client of sci-hub website. English Introduction 中文简介 Contribu

Sci-Hub on P2P 181 Jun 17, 2022
A simple UDP server to make a virtual secure channel with the clients

udpsocket I made this package to make a virtual stateful connection between the client & server using the UDP protocol for a golang game server (as yo

TheRedRad 5 Jun 18, 2022
Fast Static File Analysis Framework

Florentino; Fast Static File Analysis Framework Story Florentino is named after a fiction warrior. Flarentino: "I'd wear a fedora but they haven't inv

null 99 Jun 22, 2022