-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd/flatend, config: support serving static files
- Loading branch information
Showing
6 changed files
with
253 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
"strings" | ||
"time" | ||
) | ||
|
||
type Config struct { | ||
HTTP []ConfigHTTP | ||
} | ||
|
||
func (c Config) Validate() error { | ||
for _, srv := range c.HTTP { | ||
err := srv.Validate() | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
type Duration struct { | ||
time.Duration | ||
} | ||
|
||
func (d *Duration) UnmarshalText(text []byte) error { | ||
var err error | ||
d.Duration, err = time.ParseDuration(string(text)) | ||
return err | ||
} | ||
|
||
type ConfigHTTP struct { | ||
Addr string | ||
Addrs []string | ||
|
||
RedirectTrailingSlash *bool `toml:"redirect_trailing_slash"` | ||
RedirectFixedPath *bool `toml:"redirect_fixed_path"` | ||
|
||
Timeout struct { | ||
Read Duration | ||
ReadHeader Duration | ||
Idle Duration | ||
Write Duration | ||
Shutdown Duration | ||
} | ||
|
||
Min struct { | ||
BodySize *int `toml:"body_size"` | ||
} | ||
|
||
Max struct { | ||
HeaderSize int `toml:"header_size"` | ||
BodySize *int `toml:"body_size"` | ||
} | ||
|
||
Routes []ConfigRoute | ||
} | ||
|
||
func (h ConfigHTTP) GetAddrs() []string { | ||
if h.Addr != "" { | ||
return []string{h.Addr} | ||
} | ||
return h.Addrs | ||
} | ||
|
||
func (h ConfigHTTP) Validate() error { | ||
if h.Addr != "" && h.Addrs != nil { | ||
return errors.New("'addr' and 'addrs' cannot both be non-nil at the same time") | ||
} | ||
|
||
for _, route := range h.Routes { | ||
err := route.Validate() | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
type ConfigRoute struct { | ||
Path string | ||
Dispatch string | ||
Static string // static files | ||
Service string | ||
Services []string | ||
|
||
NoCache bool | ||
|
||
Min struct { | ||
BodySize *int `toml:"body_size"` | ||
} | ||
|
||
Max struct { | ||
BodySize *int `toml:"body_size"` | ||
} | ||
} | ||
|
||
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") | ||
} | ||
|
||
fields := strings.Fields(r.Path) | ||
if len(fields) != 2 { | ||
return fmt.Errorf("invalid number of fields in route path '%s' (format: 'HTTP_METHOD /path/here')", | ||
r.Path) | ||
} | ||
|
||
method := strings.ToUpper(fields[0]) | ||
_, exists := Methods[method] | ||
if !exists { | ||
return fmt.Errorf("unknown http method '%s'", method) | ||
} | ||
|
||
if len(fields[1]) < 1 || fields[1][0] != '/' { | ||
return fmt.Errorf("path must begin with '/' in path '%s'", fields[1]) | ||
} | ||
|
||
_, err := url.ParseRequestURI(fields[1]) | ||
if err != nil { | ||
return fmt.Errorf("invalid http path '%s': %w", fields[1], err) | ||
} | ||
|
||
if r.Static != "" && method != http.MethodGet { | ||
return fmt.Errorf("path '%s' method must be 'GET' to serve files", r.Path) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/julienschmidt/httprouter" | ||
"github.com/lithdew/flatend" | ||
"io" | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
func HandleService(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 { | ||
headers[strings.ToLower(key)] = r.Header.Get(key) | ||
} | ||
|
||
for key := range r.URL.Query() { | ||
headers["query."+strings.ToLower(key)] = r.URL.Query().Get(key) | ||
} | ||
|
||
params := httprouter.ParamsFromContext(r.Context()) | ||
for _, param := range params { | ||
headers["params."+strings.ToLower(param.Key)] = param.Value | ||
} | ||
|
||
stream, err := node.Push(services, headers, r.Body) | ||
if err != nil { | ||
w.Write([]byte(err.Error())) | ||
return | ||
} | ||
|
||
for name, val := range stream.Header.Headers { | ||
w.Header().Set(name, val) | ||
} | ||
|
||
_, _ = io.Copy(w, stream.Reader) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.