Skip to content

Commit

Permalink
Introduced Reige struct for flexible HTTP handler execution
Browse files Browse the repository at this point in the history
  • Loading branch information
mashiike committed Mar 14, 2024
1 parent cf48ef0 commit 26357b3
Showing 1 changed file with 38 additions and 11 deletions.
49 changes: 38 additions & 11 deletions ridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,33 +127,60 @@ func Run(address, prefix string, mux http.Handler) {

// RunWithContext runs http handler on AWS Lambda runtime or net/http's server with context.
func RunWithContext(ctx context.Context, address, prefix string, mux http.Handler) {
r := New(address, prefix, mux)
r.RunWithContext(ctx)
}

// Ridge is a struct to run http handler on AWS Lambda runtime or net/http's server.
type Reige struct {
Address string
Prefix string
Mux http.Handler
}

// New creates a new Reige.
func New(address, prefix string, mux http.Handler) *Reige {
return &Reige{
Address: address,
Prefix: prefix,
Mux: mux,
}
}

// Run runs http handler on AWS Lambda runtime or net/http's server.
func (r *Reige) Run() {
r.RunWithContext(context.Background())
}

// RunWithContext runs http handler on AWS Lambda runtime or net/http's server with context.
func (r *Reige) RunWithContext(ctx context.Context) {
if strings.HasPrefix(os.Getenv("AWS_EXECUTION_ENV"), "AWS_Lambda") || os.Getenv("AWS_LAMBDA_RUNTIME_API") != "" {
// go1.x or custom runtime(provided, provided.al2)
handler := func(event json.RawMessage) (interface{}, error) {
r, err := NewRequest(event)
req, err := NewRequest(event)
if err != nil {
log.Println(err)
return nil, err
}
w := NewResponseWriter()
mux.ServeHTTP(w, r)
r.Mux.ServeHTTP(w, req)
return w.Response(), nil
}
lambda.StartWithContext(ctx, handler)
} else {
m := http.NewServeMux()
switch {
case prefix == "/", prefix == "":
m.Handle("/", mux)
case !strings.HasSuffix(prefix, "/"):
m.Handle(prefix+"/", http.StripPrefix(prefix, mux))
case r.Prefix == "/", r.Prefix == "":
m.Handle("/", r.Mux)
case !strings.HasSuffix(r.Prefix, "/"):
m.Handle(r.Prefix+"/", http.StripPrefix(r.Prefix, r.Mux))
default:
m.Handle(prefix, http.StripPrefix(strings.TrimSuffix(prefix, "/"), mux))
m.Handle(r.Prefix, http.StripPrefix(strings.TrimSuffix(r.Prefix, "/"), r.Mux))
}
log.Println("starting up with local httpd", address)
listener, err := net.Listen("tcp", address)
log.Println("starting up with local httpd", r.Address)
listener, err := net.Listen("tcp", r.Address)
if err != nil {
log.Fatalf("couldn't listen to %s: %s", address, err.Error())
log.Fatalf("couldn't listen to %s: %s", r.Address, err.Error())
}
if ProxyProtocol {
log.Println("enables to PROXY protocol")
Expand All @@ -165,7 +192,7 @@ func RunWithContext(ctx context.Context, address, prefix string, mux http.Handle
go func() {
defer wg.Done()
<-ctx.Done()
log.Println("shutting down local httpd", address)
log.Println("shutting down local httpd", r.Address)
srv.Shutdown(ctx)
}()
if err := srv.Serve(listener); err != nil {
Expand Down

0 comments on commit 26357b3

Please sign in to comment.