-
Notifications
You must be signed in to change notification settings - Fork 405
/
main.go
47 lines (38 loc) · 1.47 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package main
import (
"context"
"fmt"
"html"
"net/http"
"github.com/fnproject/fn/api/models"
"github.com/fnproject/fn/api/server"
)
func main() {
ctx := context.Background()
funcServer := server.NewFromEnv(ctx)
// Setup your custom extensions, listeners, etc here
funcServer.AddEndpoint("GET", "/custom1", &custom1Handler{})
funcServer.AddEndpointFunc("GET", "/custom2", func(w http.ResponseWriter, r *http.Request) {
// fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
fmt.Println("custom2Handler called")
fmt.Fprintf(w, "Hello func, %q", html.EscapeString(r.URL.Path))
})
// the following will be at /v1/apps/:app_name/custom2
funcServer.AddAppEndpoint("GET", "/custom3", &custom3Handler{})
funcServer.AddAppEndpointFunc("GET", "/custom4", func(w http.ResponseWriter, r *http.Request, app *models.App) {
// fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
fmt.Println("custom4Handler called")
fmt.Fprintf(w, "Hello app %v func, %q", app.Name, html.EscapeString(r.URL.Path))
})
funcServer.Start(ctx)
}
type custom1Handler struct{}
func (h *custom1Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Println("custom1Handler called")
fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
}
type custom3Handler struct{}
func (h *custom3Handler) ServeHTTP(w http.ResponseWriter, r *http.Request, app *models.App) {
fmt.Println("custom3Handler called")
fmt.Fprintf(w, "Hello app %v, %q", app.Name, html.EscapeString(r.URL.Path))
}