diff --git a/examples/getting-started/any-method/main.go b/examples/getting-started/any-method/main.go index 80fa9162..7e67d517 100644 --- a/examples/getting-started/any-method/main.go +++ b/examples/getting-started/any-method/main.go @@ -27,6 +27,7 @@ import ( "github.com/caicloud/nirvana/config" "github.com/caicloud/nirvana/definition" "github.com/caicloud/nirvana/log" + "github.com/caicloud/nirvana/service" ) func main() { @@ -48,7 +49,7 @@ func main() { { Method: definition.Any, Consumes: []string{definition.MIMEAll}, - Produces: []string{definition.MIMEAll}, + Produces: []string{definition.MIMEText}, Function: func(ctx context.Context) (string, error) { return "hello", nil }, @@ -64,7 +65,7 @@ func main() { Method: definition.Any, Consumes: []string{definition.MIMEAll}, Produces: []string{definition.MIMEAll}, - Handler: httputil.NewSingleHostReverseProxy(rpURL), + Function: service.WrapHTTPHandler(httputil.NewSingleHostReverseProxy(rpURL)), }, }, }, diff --git a/examples/getting-started/handler/main.go b/examples/getting-started/handler/main.go new file mode 100644 index 00000000..0c9cc6cd --- /dev/null +++ b/examples/getting-started/handler/main.go @@ -0,0 +1,82 @@ +/* +Copyright 2020 Caicloud Authors + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package main + +import ( + "context" + "fmt" + "net/http" + "net/http/httptest" + "net/http/httputil" + "net/url" + + "github.com/caicloud/nirvana/config" + "github.com/caicloud/nirvana/definition" + "github.com/caicloud/nirvana/log" + "github.com/caicloud/nirvana/middlewares/reqlog" + "github.com/caicloud/nirvana/service" +) + +func main() { + backendServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + fmt.Fprintln(w, "this call was relayed by the reverse proxy") + })) + defer backendServer.Close() + + rpURL, err := url.Parse(backendServer.URL) + if err != nil { + log.Fatal(err) + } + + descriptors := []definition.Descriptor{ + { + Path: "/", + Description: "hello API", + Definitions: []definition.Definition{ + { + Method: definition.Get, + Consumes: []string{definition.MIMEAll}, + Produces: []string{definition.MIMEJSON}, + Function: func(ctx context.Context) (string, error) { + return "hello", nil + }, + Results: definition.DataErrorResults(""), + }, + }, + }, + { + Path: "/proxy", + Description: "proxy API", + Middlewares: []definition.Middleware{ + reqlog.Default(), + }, + Definitions: []definition.Definition{ + { + Method: definition.Get, + Consumes: []string{definition.MIMEAll}, + Produces: []string{definition.MIMEAll}, + Function: service.WrapHTTPHandler(httputil.NewSingleHostReverseProxy(rpURL)), + }, + }, + }, + } + + cmd := config.NewDefaultNirvanaCommand() + if err = cmd.Execute(descriptors...); err != nil { + log.Fatal(err) + } +} diff --git a/hack/verify_boilerplate.py b/hack/verify_boilerplate.py index 518e0528..4f9fda3b 100755 --- a/hack/verify_boilerplate.py +++ b/hack/verify_boilerplate.py @@ -154,7 +154,7 @@ def get_regexs(): # Search for "YEAR" which exists in the boilerplate, but shouldn't in the real thing regexs["year"] = re.compile('YEAR') # dates can be 2017 or 2018, company holder names can be anything - regexs["date"] = re.compile('(2017|2018)') + regexs["date"] = re.compile('(2017|2018|2019|2020)') # strip // +build \n\n build constraints regexs["go_build_constraints"] = re.compile(r"^(// \+build.*\n)+\n", re.MULTILINE) # strip #!.* from shell/python scripts diff --git a/service/executor.go b/service/executor.go index af98e98d..ea4d2aaf 100644 --- a/service/executor.go +++ b/service/executor.go @@ -20,6 +20,7 @@ import ( "context" "fmt" "io" + "net/http" "path" "reflect" "runtime"