Skip to content

Commit

Permalink
feat: add ssl support
Browse files Browse the repository at this point in the history
  • Loading branch information
Dorian committed Sep 24, 2024
1 parent cb3cde8 commit 8a8a17e
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .env.dev
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
APP_HOST=http://localhost
APP_HOST=https://localhost
APP_DOMAIN=localhost

DATABASE_HOST=redis
DATABASE_PORT=6379
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ go.work.sum

# env file
.env.prod

# SSL certs
certs/
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
cert:
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout certs/localhost.key -out certs/localhost.crt -subj "/C=CA/ST=Newfoundland/L=St. John\'s/O=Dorian Neto/OU=Development/CN=localhost/emailAddress=doriansampaioneto@gmail.com"
1 change: 1 addition & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ services:
- APP_ENV=dev
ports:
- 80:80
- 443:443
volumes:
- ./:/app
redis:
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ require (
require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/text v0.18.0 // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ github.com/redis/go-redis/v9 v9.6.1 h1:HHDteefn6ZkTtY5fGUE8tj8uy85AHk6zP7CpzIAM0
github.com/redis/go-redis/v9 v9.6.1/go.mod h1:0C0c6ycQsdpVNQpxb1njEQIqkx5UcsM8FJCQLgE9+RA=
golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A=
golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70=
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
42 changes: 40 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,36 @@
package main

import (
"crypto/tls"
"fmt"
"log/slog"
"net/http"
"os"
"path/filepath"

"github.com/dorianneto/burn-secret/cmd/api"
"github.com/dorianneto/burn-secret/internal"
"github.com/joho/godotenv"
"golang.org/x/crypto/acme/autocert"
)

func getSelfSignedOrLetsEncryptCert(certManager *autocert.Manager) func(hello *tls.ClientHelloInfo) (*tls.Certificate, error) {
return func(hello *tls.ClientHelloInfo) (*tls.Certificate, error) {
dirCache, ok := certManager.Cache.(autocert.DirCache)
if !ok {
dirCache = "certs"
}

keyFile := filepath.Join(string(dirCache), hello.ServerName+".key")
crtFile := filepath.Join(string(dirCache), hello.ServerName+".crt")
certificate, err := tls.LoadX509KeyPair(crtFile, keyFile)
if err != nil {
return certManager.GetCertificate(hello)
}
return &certificate, err
}
}

func main() {
godotenv.Load(".env." + os.Getenv("APP_ENV"))

Expand All @@ -21,9 +42,26 @@ func main() {

app := api.NewApp(logger, database)

logger.Info("server running on port :80")
certManager := autocert.Manager{
Prompt: autocert.AcceptTOS,
HostPolicy: autocert.HostWhitelist(os.Getenv("APP_DOMAIN")),
Cache: autocert.DirCache("certs"),
}

tlsConfig := certManager.TLSConfig()
tlsConfig.GetCertificate = getSelfSignedOrLetsEncryptCert(&certManager)

server := http.Server{
Addr: ":443",
Handler: app.Routes(),
TLSConfig: tlsConfig,
}

logger.Info(fmt.Sprintf("server running on port :%s", server.Addr))

go http.ListenAndServe(":80", certManager.HTTPHandler(nil))

if err := http.ListenAndServe(":80", app.Routes()); err != nil {
if err := server.ListenAndServeTLS("certs/localhost.crt", "certs/localhost.key"); err != nil {
logger.Error(err.Error())
os.Exit(1)
}
Expand Down
2 changes: 1 addition & 1 deletion public/App.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/App.js.map

Large diffs are not rendered by default.

0 comments on commit 8a8a17e

Please sign in to comment.