summaryrefslogtreecommitdiff
path: root/modules/caddyhttp/caddyhttp.go
diff options
context:
space:
mode:
authorMatthew Holt <mholt@users.noreply.github.com>2019-03-26 15:45:51 -0600
committerMatthew Holt <mholt@users.noreply.github.com>2019-03-26 15:45:51 -0600
commit86e2d1b0a48fbd84590291969611f1870471c3e0 (patch)
treed3a8d82ca88f7d7ff60476cd772d8c4314cd6a3f /modules/caddyhttp/caddyhttp.go
parent859b5d7ea3b8f660ac68d9aea5a53d25a9a7422c (diff)
Rudimentary start of HTTP servers
Diffstat (limited to 'modules/caddyhttp/caddyhttp.go')
-rw-r--r--modules/caddyhttp/caddyhttp.go75
1 files changed, 71 insertions, 4 deletions
diff --git a/modules/caddyhttp/caddyhttp.go b/modules/caddyhttp/caddyhttp.go
index 296e28f..99efef0 100644
--- a/modules/caddyhttp/caddyhttp.go
+++ b/modules/caddyhttp/caddyhttp.go
@@ -1,7 +1,13 @@
package caddyhttp
import (
+ "fmt"
"log"
+ "net"
+ "net/http"
+ "strconv"
+ "strings"
+ "time"
"bitbucket.org/lightcodelabs/caddy2"
)
@@ -9,7 +15,7 @@ import (
func init() {
err := caddy2.RegisterModule(caddy2.Module{
Name: "http",
- New: func() (interface{}, error) { return httpModuleConfig{}, nil },
+ New: func() (interface{}, error) { return new(httpModuleConfig), nil },
})
if err != nil {
log.Fatal(err)
@@ -20,8 +26,69 @@ type httpModuleConfig struct {
Servers map[string]httpServerConfig `json:"servers"`
}
+func (hc *httpModuleConfig) Run() error {
+ fmt.Printf("RUNNING: %#v\n", hc)
+
+ for _, srv := range hc.Servers {
+ s := http.Server{
+ ReadTimeout: time.Duration(srv.ReadTimeout),
+ ReadHeaderTimeout: time.Duration(srv.ReadHeaderTimeout),
+ }
+
+ for _, lnAddr := range srv.Listen {
+ proto, addrs, err := parseListenAddr(lnAddr)
+ if err != nil {
+ return fmt.Errorf("parsing listen address '%s': %v", lnAddr, err)
+ }
+ for _, addr := range addrs {
+ ln, err := caddy2.Listen(proto, addr)
+ if err != nil {
+ return fmt.Errorf("%s: listening on %s: %v", proto, addr, err)
+ }
+ go s.Serve(ln)
+ }
+ }
+ }
+
+ return nil
+}
+
+func parseListenAddr(a string) (proto string, addrs []string, err error) {
+ proto = "tcp"
+ if idx := strings.Index(a, ":::"); idx >= 0 {
+ proto = strings.ToLower(strings.TrimSpace(a[:idx]))
+ a = a[idx+3:]
+ }
+ var host, port string
+ host, port, err = net.SplitHostPort(a)
+ if err != nil {
+ return
+ }
+ ports := strings.SplitN(port, "-", 2)
+ if len(ports) == 1 {
+ ports = append(ports, ports[0])
+ }
+ var start, end int
+ start, err = strconv.Atoi(ports[0])
+ if err != nil {
+ return
+ }
+ end, err = strconv.Atoi(ports[1])
+ if err != nil {
+ return
+ }
+ if end < start {
+ err = fmt.Errorf("end port must be greater than start port")
+ return
+ }
+ for p := start; p <= end; p++ {
+ addrs = append(addrs, net.JoinHostPort(host, fmt.Sprintf("%d", p)))
+ }
+ return
+}
+
type httpServerConfig struct {
- Listen []string `json:"listen"`
- ReadTimeout string `json:"read_timeout"`
- ReadHeaderTimeout string `json:"read_header_timeout"`
+ Listen []string `json:"listen"`
+ ReadTimeout caddy2.Duration `json:"read_timeout"`
+ ReadHeaderTimeout caddy2.Duration `json:"read_header_timeout"`
}