Skip to content

Commit

Permalink
cmd/flatend: initial support for https
Browse files Browse the repository at this point in the history
  • Loading branch information
lithdew committed Jun 15, 2020
1 parent 9a7f0b7 commit d6f5f67
Show file tree
Hide file tree
Showing 5 changed files with 481 additions and 15 deletions.
9 changes: 9 additions & 0 deletions cmd/flatend/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ type ConfigHTTP struct {
Addr string
Addrs []string

HTTPS bool

RedirectTrailingSlash *bool `toml:"redirect_trailing_slash"`
RedirectFixedPath *bool `toml:"redirect_fixed_path"`

Expand Down Expand Up @@ -101,6 +103,13 @@ type ConfigRoute struct {
}
}

func (r ConfigRoute) GetServices() []string {
if r.Service == "" {
return r.Services
}
return []string{r.Service}
}

func (r ConfigRoute) Validate() error {
if r.Service != "" && r.Services != nil {
return errors.New("'service' and 'services' cannot both be non-nil at the same time")
Expand Down
51 changes: 36 additions & 15 deletions cmd/flatend/main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package main

import (
"crypto/tls"
"errors"
"fmt"
"github.com/BurntSushi/toml"
"github.com/caddyserver/certmagic"
"github.com/julienschmidt/httprouter"
"github.com/lithdew/flatend"
"github.com/spf13/pflag"
Expand Down Expand Up @@ -69,6 +71,7 @@ func main() {

for _, route := range cfg.Routes {
fields := strings.Fields(route.Path)
services := route.GetServices()

var handler http.Handler

Expand All @@ -93,12 +96,7 @@ func main() {
http.ServeFile(w, r, static)
})
}
case route.Service != "" || len(route.Services) > 0:
services := route.Services
if route.Service != "" {
services = append(services, route.Service)
}

case len(services) > 0:
handler = HandleService(node, services)
}

Expand All @@ -123,17 +121,40 @@ func main() {
check(srv.Close())
}()

for _, addr := range cfg.GetAddrs() {
addr := addr
go func() {
ln, err := net.Listen("tcp", addr)
check(err)
addrs := cfg.GetAddrs()

if cfg.HTTPS {
magic := certmagic.NewDefault()
check(magic.ManageSync(addrs))

err = srv.Serve(ln)
if !errors.Is(err, http.ErrServerClosed) {
acme := certmagic.NewACMEManager(magic, certmagic.DefaultACME)
srv.Handler = acme.HTTPChallengeHandler(srv.Handler)

for _, addr := range addrs {
addr := addr
go func() {
ln, err := tls.Listen("tcp", addr, magic.TLSConfig())
check(err)
}
}()

err = srv.Serve(ln)
if !errors.Is(err, http.ErrServerClosed) {
check(err)
}
}()
}
} else {
for _, addr := range addrs {
addr := addr
go func() {
ln, err := net.Listen("tcp", addr)
check(err)

err = srv.Serve(ln)
if !errors.Is(err, http.ErrServerClosed) {
check(err)
}
}()
}
}
}

Expand Down
1 change: 1 addition & 0 deletions config.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[[http]]
addr = ":3000"
https = false
redirect_trailing_slash = true
redirect_fixed_path = true

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.14

require (
github.com/BurntSushi/toml v0.3.1
github.com/caddyserver/certmagic v0.11.2
github.com/jpillora/backoff v1.0.0
github.com/julienschmidt/httprouter v1.3.0
github.com/lithdew/bytesutil v0.0.0-20200409052507-d98389230a59
Expand Down
Loading

0 comments on commit d6f5f67

Please sign in to comment.