github.com/iron-io/functions@v0.0.0-20180820112432-d59d7d1c40b2/examples/extensions/main.go (about)

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"html"
     7  	"net/http"
     8  
     9  	"github.com/iron-io/functions/api/models"
    10  	"github.com/iron-io/functions/api/server"
    11  )
    12  
    13  func main() {
    14  	ctx := context.Background()
    15  
    16  	funcServer := server.NewFromEnv(ctx)
    17  	// Setup your custom extensions, listeners, etc here
    18  	funcServer.AddEndpoint("GET", "/custom1", &Custom1Handler{})
    19  	funcServer.AddEndpointFunc("GET", "/custom2", func(w http.ResponseWriter, r *http.Request) {
    20  		// fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
    21  		fmt.Println("Custom2Handler called")
    22  		fmt.Fprintf(w, "Hello func, %q", html.EscapeString(r.URL.Path))
    23  	})
    24  
    25  	// the following will be at /v1/apps/:app_name/custom2
    26  	funcServer.AddAppEndpoint("GET", "/custom3", &Custom3Handler{})
    27  	funcServer.AddAppEndpointFunc("GET", "/custom4", func(w http.ResponseWriter, r *http.Request, app *models.App) {
    28  		// fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
    29  		fmt.Println("Custom4Handler called")
    30  		fmt.Fprintf(w, "Hello app %v func, %q", app.Name, html.EscapeString(r.URL.Path))
    31  	})
    32  	funcServer.Start(ctx)
    33  }
    34  
    35  type Custom1Handler struct {
    36  }
    37  
    38  func (h *Custom1Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    39  	fmt.Println("Custom1Handler called")
    40  	fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
    41  }
    42  
    43  type Custom3Handler struct {
    44  }
    45  
    46  func (h *Custom3Handler) ServeHTTP(w http.ResponseWriter, r *http.Request, app *models.App) {
    47  	fmt.Println("Custom3Handler called")
    48  	fmt.Fprintf(w, "Hello app %v, %q", app.Name, html.EscapeString(r.URL.Path))
    49  }