https://github.com/j7mbo/gohttpsws
A simple go tls webserver example that works for both dev and prod.
https://github.com/j7mbo/gohttpsws
Last synced: 2 months ago
JSON representation
A simple go tls webserver example that works for both dev and prod.
- Host: GitHub
- URL: https://github.com/j7mbo/gohttpsws
- Owner: J7mbo
- License: mit
- Created: 2021-01-10T22:04:33.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2021-01-17T11:16:45.000Z (over 4 years ago)
- Last Synced: 2025-01-30T21:29:35.457Z (4 months ago)
- Language: Go
- Size: 7.81 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# gohttpsws
#### A simple go tls webserver example that works for both dev and prod.
[](https://travis-ci.com/J7mbo/gohttpsws)
[](https://godoc.org/github.com/J7mbo/gohttpsws)
[](LICENSE.md)> I regularly find myself re-using the same simple code to set up a webserver in Go with TLS for both dev and prod.
> So I placed it in a library for simpler re-use.### Pre-requisites
- [mkcert](https://github.com/FiloSottile/mkcert) on your host machine (macos: `brew install mkcert`)
### Usage - Dev
1. Generate ca, cert and key using mkcert:
```bash
CAROOT=./certs mkcert -install
CAROOT=./certs mkcert -cert-file ./certs/cert.pem -key-file ./certs/key.pem localhost 127.0.0.1
```*On MacOS these are added to your Keychain, so you can manage them using Keychain Access > System > Certificates.*
This will give you `ca.pem`, `cert.pem` and `key.pem` in the directory you run the command from.
2. Use these when invoking the library. Make sure to _absolute paths_ for the files:
```go
package mainimport (
"github.com/gorilla/mux"
"github.com/j7mbo/gohttpsws"
)func main() {
c := gohttpsws.NewConfig(
"8080",
"/absolute/path/to/cert.pem",
"/absolute/path/to/key.pem",
"/absolute/path/to/rootCA.pem",
)
s, _ := gohttpsws.NewServer(c)r := mux.NewRouter()
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte("Hello World"))
})if err := s.ServeDev(r); err != nil {
panic(err)
}
}
```### Usage - Prod
Use `ServeProd()` instead of dev, and provide the certificate and key.
Ideally use letsencrypt and point to the files generated by the letsencrypt binary. The certificate authority is not
needed.