Skip to content

Commit

Permalink
cmd/flatend, flathttp: move http.Handler for forward http requests to…
Browse files Browse the repository at this point in the history
… services/nodes and middleware into separate flathttp pkg
  • Loading branch information
lithdew committed Jun 16, 2020
1 parent 573deeb commit a795271
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 22 deletions.
19 changes: 10 additions & 9 deletions cmd/flatend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/caddyserver/certmagic"
"github.com/julienschmidt/httprouter"
"github.com/lithdew/flatend"
"github.com/lithdew/flatend/flathttp"
"github.com/spf13/pflag"
"io/ioutil"
"log"
Expand All @@ -25,12 +26,12 @@ func check(err error) {
}
}

var Methods = map[string]struct{}{
http.MethodGet: {},
http.MethodPost: {},
http.MethodPut: {},
http.MethodDelete: {},
http.MethodPatch: {},
func hostOnly(hostPort string) string {
host, _, err := net.SplitHostPort(hostPort)
if err != nil {
return hostPort
}
return host
}

func main() {
Expand All @@ -43,7 +44,7 @@ func main() {
pflag.Uint16VarP(&bindPort, "port", "p", 9000, "bind port")
pflag.Parse()

var cfg Config
var cfg flathttp.Config

buf, err := ioutil.ReadFile(configPath)
if err == nil {
Expand Down Expand Up @@ -115,12 +116,12 @@ func main() {
})
}
case len(services) > 0:
handler = HandleService(node, services)
handler = flathttp.Handle(node, services)
}

if handler != nil {
if route.NoCache {
handler = NoCache(handler)
handler = flathttp.NoCache(handler)
}
router.Handler(fields[0], fields[1], handler)
}
Expand Down
10 changes: 9 additions & 1 deletion cmd/flatend/config.go → flathttp/config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package flathttp

import (
"errors"
Expand All @@ -10,6 +10,14 @@ import (
"time"
)

var Methods = map[string]struct{}{
http.MethodGet: {},
http.MethodPost: {},
http.MethodPut: {},
http.MethodDelete: {},
http.MethodPatch: {},
}

type Config struct {
Addr string
HTTP []ConfigHTTP
Expand Down
11 changes: 1 addition & 10 deletions cmd/flatend/http.go → flathttp/middleware.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main
package flathttp

import (
"net"
"net/http"
"time"
)
Expand Down Expand Up @@ -41,11 +40,3 @@ func NoCache(h http.Handler) http.Handler {

return http.HandlerFunc(fn)
}

func hostOnly(hostPort string) string {
host, _, err := net.SplitHostPort(hostPort)
if err != nil {
return hostPort // OK; probably had no port to begin with
}
return host
}
4 changes: 2 additions & 2 deletions cmd/flatend/node.go → flathttp/service.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package flathttp

import (
"github.com/julienschmidt/httprouter"
Expand All @@ -8,7 +8,7 @@ import (
"strings"
)

func HandleService(node *flatend.Node, services []string) http.Handler {
func Handle(node *flatend.Node, services []string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
headers := make(map[string]string)
for key := range r.Header {
Expand Down

0 comments on commit a795271

Please sign in to comment.