Hacker News new | ask | show | jobs
by skanga 3091 days ago
Nice. Does anyone have a server like this but with the addition of

* serving of files and directories

* LetsEncrypt certificates and SSL

2 comments

static file server in go:

    package main

    import (
        "fmt"
        "net/http"
        "path/filepath"
    )

    var (
        host = "0.0.0.0"
        path = "/var/www/public"
        port = 8080
    )

    func main() {
        path, err := filepath.Abs(path)
        if err != nil {
            panic(err)
        }
        listenAt := fmt.Sprintf("%s:%v", host, port)
        fmt.Println("static-serve-dir serving", path, "over http at", listenAt)
        err = http.ListenAndServe(listenAt, http.FileServer(http.Dir(path)))
        if err != nil {
            panic(err)
        }
    }
The builtin golang.org/x/crypto/acme/autocert [0] package manages SSL for you easily through Lets Encrypt.

[0]: https://godoc.org/golang.org/x/crypto/acme/autocert

golang.org/x/ packages are not builtin. You still must download them and use them just as you do any other dependencies, they just share some contribution process and developers with golang's stdlib.