Skip to content

Commit

Permalink
fix(*): update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
iawia002 committed Oct 21, 2020
1 parent 9f2f552 commit ffcb1e3
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 3 deletions.
5 changes: 3 additions & 2 deletions examples/getting-started/any-method/main.go
Expand Up @@ -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() {
Expand All @@ -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
},
Expand All @@ -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)),
},
},
},
Expand Down
82 changes: 82 additions & 0 deletions 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)
}
}
2 changes: 1 addition & 1 deletion hack/verify_boilerplate.py
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions service/executor.go
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"fmt"
"io"
"net/http"
"path"
"reflect"
"runtime"
Expand Down

0 comments on commit ffcb1e3

Please sign in to comment.