mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
API extension points (#473)
* API endpoint extensions working. extensions example. * Added server.NewEnv and some docs for the API extensions example. extensions example. example main.go. * Uncommented special handler stuff. * Added section in docs for extending API linking to example main.go. * Commented out special_handler test * Changed to NewFromEnv
This commit is contained in:
2
examples/extensions/.gitignore
vendored
Normal file
2
examples/extensions/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
/extensions
|
||||
/extensions.exe
|
||||
22
examples/extensions/README.md
Normal file
22
examples/extensions/README.md
Normal file
@@ -0,0 +1,22 @@
|
||||
# Extensions Example
|
||||
|
||||
This example adds extra endpoints to the API.
|
||||
|
||||
## Building and Running
|
||||
|
||||
```sh
|
||||
go build -o functions
|
||||
./functions
|
||||
```
|
||||
|
||||
Then test with:
|
||||
|
||||
```sh
|
||||
# First, create an app
|
||||
fn apps create myapp
|
||||
# And test
|
||||
curl http://localhost:8080/v1/custom1
|
||||
curl http://localhost:8080/v1/custom2
|
||||
curl http://localhost:8080/v1/apps/myapp/custom3
|
||||
curl http://localhost:8080/v1/apps/myapp/custom4
|
||||
```
|
||||
49
examples/extensions/main.go
Normal file
49
examples/extensions/main.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"html"
|
||||
"net/http"
|
||||
|
||||
"github.com/iron-io/functions/api/models"
|
||||
"github.com/iron-io/functions/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))
|
||||
}
|
||||
Reference in New Issue
Block a user