feat: removing framework, pure go implementation

This commit is contained in:
Pierre Zemb 2017-05-28 14:37:34 +02:00
parent 63c52f19e2
commit f643438420
No known key found for this signature in database
GPG key ID: E48ABD1BBB7FEC23
5 changed files with 24 additions and 90 deletions

View file

@ -1,17 +1,20 @@
# goStatic # goStatic
A really small static web server for Docker A really small static web server for Docker
[![](https://badge.imagelayers.io/pierrezemb/gostatic:latest.svg)](https://imagelayers.io/?images=pierrezemb/gostatic:latest 'Get your own badge on imagelayers.io')
[![GoDoc](https://godoc.org/github.com/PierreZ/goStatic?status.svg)](https://godoc.org/github.com/PierreZ/goStatic) [![GoDoc](https://godoc.org/github.com/PierreZ/goStatic?status.svg)](https://godoc.org/github.com/PierreZ/goStatic)
### The goal ### The goal
My goal is to create to smallest docker container for my web static files. The advantage of Go is that you can generate a fully static binary, so that you don't need anything else. My goal is to create to smallest docker container for my web static files. The advantage of Go is that you can generate a fully static binary, so that you don't need anything else.
### Wait, I've been using old versions of GoStatic and things have changed!
Yeah, decided to drop support of unsecured HTTPS. Two-years ago, when I started GoStatic, there was no automatic HTTPS available. Nowadays, thanks to Let's Encrypt, it's really easy to do so. If you need HTTPS, I recommend [caddy](https://caddyserver.com).
### Features ### Features
* A fully static web server in 5MB * A fully static web server in 6MB
* No frameworkw
* Web server build for Docker * Web server build for Docker
* HTTPS by default * Can generate certificate on his own
* Can generate certificate on his onw
* Light container * Light container
* More security than official images (see below) * More security than official images (see below)
* Log enabled * Log enabled
@ -31,20 +34,22 @@ Many links should provide you with additionnal info to see my point of view:
### How to use ### How to use
``` ```
// HTTPS server docker run -d -p 80:8043 -v path/to/website:/srv/http --name goStatic pierrezemb/gostatic
docker run -d -p 443:8043 -v path/to/website:/srv/http --name goStatic pierrezemb/gostatic
// HTTP server
docker run -d -p 80:8043 -v path/to/website:/srv/http --name goStatic pierrezemb/gostatic --forceHTTP
``` ```
### Wow, such container! What are you using? ### Wow, such container! What are you using?
I'm using [echo](http://echo.labstack.com/) as a micro web framework because he has great performance, and [golang-builder](https://github.com/CenturyLinkLabs/golang-builder) to generate the static binary (command line in the makefile) I'm using the centurylink/ca-certs image instead of the scratch image to avoid this error:
I'm also using the centurylink/ca-certs image instead of the scratch image to avoid this error:
``` ```
x509: failed to load system roots and no roots provided x509: failed to load system roots and no roots provided
``` ```
The centurylink/ca-certs image is simply the scratch image with the most common root CA certificates pre-installed. The resulting image is only 258 kB which is still a good starting point for creating your own minimal images. The centurylink/ca-certs image is simply the scratch image with the most common root CA certificates pre-installed. The resulting image is only 258 kB which is still a good starting point for creating your own minimal images.
### How to build
```bash
GOARCH=amd64 GOOS=linux go build -ldflags "-linkmode external -extldflags -static -w"
```

37
glide.lock generated
View file

@ -1,37 +0,0 @@
hash: ff8a32679991a0939800d56b0b34ade1a46ed2f6d9a6f21a95e07196b5e16fbe
updated: 2017-05-27T20:48:09.06190059+02:00
imports:
- name: github.com/dgrijalva/jwt-go
version: 6c8dedd55f8a2e41f605de6d5d66e51ed1f299fc
- name: github.com/labstack/echo
version: 1049c9613cd371b7ea8f219404c9a821734781ed
subpackages:
- '...'
- middleware
- name: github.com/labstack/gommon
version: 1121fd3e243c202482226a7afe4dcd07ffc4139a
subpackages:
- bytes
- color
- log
- random
- name: github.com/mattn/go-colorable
version: ded68f7a9561c023e790de24279db7ebf473ea80
- name: github.com/mattn/go-isatty
version: fc9e8d8ef48496124e79ae0df75490096eccf6fe
- name: github.com/PierreZ/tlscert
version: 132262881d39c577835ab78f179abd2de116cb32
- name: github.com/valyala/bytebufferpool
version: e746df99fe4a3986f4d4f79e13c1e0117ce9c2f7
- name: github.com/valyala/fasttemplate
version: dcecefd839c4193db0d35b88ec65b4c12d360ab0
- name: golang.org/x/crypto
version: 7e9105388ebff089b3f99f0ef676ea55a6da3a7e
subpackages:
- acme
- acme/autocert
- name: golang.org/x/sys
version: a55a76086885b80f79961eacb876ebd8caf3868d
subpackages:
- unix
testImports: []

View file

@ -1,8 +0,0 @@
package: github.com/PierreZ/goStatic
import:
- package: github.com/labstack/echo
version: ^3.1.0
subpackages:
- '...'
- middleware
- package: github.com/PierreZ/tlscert

BIN
goStatic

Binary file not shown.

40
main.go
View file

@ -1,56 +1,30 @@
// This small program is just a small web server created in static mode // This small program is just a small web server created in static mode
// in order to provide the smallest docker image possible // in order to provide the smallest docker image possible
package main // import "github.com/PierreZ/goStatic" package main
import ( import (
"flag" "flag"
"log" "log"
"os" "net/http"
"strconv" "strconv"
"github.com/PierreZ/tlscert"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
) )
var ( var (
// Def of flags // Def of flags
portPtr = flag.Int("p", 8043, "The listening port") portPtr = flag.Int("p", 8043, "The listening port")
pathPtr = flag.String("static", "/srv/http", "The path for the static files") path = flag.String("static", "/srv/http", "The path for the static files")
crtPtr = flag.String("crt", "/etc/ssl/server", "Folder for server.pem and key.pem")
isUnsecure = flag.Bool("forceHTTP", false, "Forcing HTTP and not HTTPS")
) )
func main() { func main() {
flag.Parse() flag.Parse()
e := echo.New()
// Root level middleware
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.Static("/", *pathPtr) // Serve everything from the current dir
port := ":" + strconv.FormatInt(int64(*portPtr), 10) port := ":" + strconv.FormatInt(int64(*portPtr), 10)
path := *crtPtr
// Start server with unsecure HTTP fs := http.FileServer(http.Dir(*path))
if *isUnsecure { http.Handle("/", fs)
log.Println("Starting serving", *pathPtr, "on", *portPtr)
e.Start(port)
} else { // or with awesome TLS log.Println("Listening...")
if _, err := os.Stat(path + "/cert.pem"); os.IsNotExist(err) { log.Fatalln(http.ListenAndServe(port, nil))
// Generating certificates
err := tlscert.GenerateCert(path)
if err != nil {
log.Fatalf("Failed generating certs: %v", err)
}
}
log.Println("Starting serving", *pathPtr, "on", *portPtr)
e.StartTLS(port, path+"/cert.pem", path+"/key.pem")
}
} }