Skip to content

Commit

Permalink
Merge pull request #32 from iov-one/remove-host-port
Browse files Browse the repository at this point in the history
remove hostport - this is not needed
  • Loading branch information
Antoine Herzog authored Feb 18, 2020
2 parents 402ee34 + adad870 commit d4fda3b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 28 deletions.
29 changes: 9 additions & 20 deletions cmd/bnsapi/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"github.com/iov-one/bns/cmd/bnsapi/client"
"github.com/iov-one/bns/cmd/bnsapi/util"
"github.com/iov-one/weave/cmd/bnsd/x/username"
"github.com/iov-one/weave/x/cash"
"html/template"
"log"
"math"
Expand All @@ -19,6 +15,11 @@ import (
"strconv"
"strings"

"github.com/iov-one/bns/cmd/bnsapi/client"
"github.com/iov-one/bns/cmd/bnsapi/util"
"github.com/iov-one/weave/cmd/bnsd/x/username"
"github.com/iov-one/weave/x/cash"

"github.com/iov-one/weave"
"github.com/iov-one/weave/cmd/bnsd/x/account"
"github.com/iov-one/weave/cmd/bnsd/x/termdeposit"
Expand Down Expand Up @@ -566,9 +567,7 @@ func lastChunk(path string) string {
}

// DefaultHandler is used to handle the request that no other handler wants.
type DefaultHandler struct {
HostPort string
}
type DefaultHandler struct{}

var wEndpoint = []string{
"/account/accounts/?domainKey=_&ownerKey=_",
Expand All @@ -580,14 +579,6 @@ var wEndpoint = []string{
"/termdeposit/deposits/?depositor=_&contract=_&contract_id=?_offset=_",
}

func endpointsWithDomain(domain string, endpoints []string) []string {
var eps []string
for _, e := range endpoints {
eps = append(eps, "http://"+domain+e)
}
return eps
}

var withoutParamEndpoint = []string{
"/info/",
"/account/accounts/{accountKey}",
Expand All @@ -596,7 +587,6 @@ var withoutParamEndpoint = []string{
}

type endpoints struct {
HostPort string
WithParam []string
WithoutParam []string
}
Expand All @@ -615,7 +605,7 @@ var availableEndpointsTempl = template.Must(template.New("").Parse(`
{{end}}
<h1>Swagger documentation: </h1>
<a href="http://{{ .HostPort}}/docs">http://{{ .HostPort}}/docs</a></br>
<a href="/docs">/docs</a></br>
`))

func (h *DefaultHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
Expand All @@ -627,9 +617,8 @@ func (h *DefaultHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}

eps := endpoints{
HostPort: h.HostPort,
WithParam: endpointsWithDomain(h.HostPort, wEndpoint),
WithoutParam: endpointsWithDomain(h.HostPort, withoutParamEndpoint),
WithParam: wEndpoint,
WithoutParam: withoutParamEndpoint,
}

if err := availableEndpointsTempl.Execute(w, eps); err != nil {
Expand Down
12 changes: 4 additions & 8 deletions cmd/bnsapi/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"

"github.com/iov-one/bns/cmd/bnsapi/client"
"github.com/iov-one/bns/cmd/bnsapi/docs"
"github.com/iov-one/bns/cmd/bnsapi/handlers"
Expand All @@ -27,8 +28,6 @@ import (
type Configuration struct {
HTTP string
Tendermint string
// HostPort is used for swagger docs configuration
HostPort string
}

// @title BNSAPI documentation
Expand All @@ -39,9 +38,7 @@ func main() {

conf := Configuration{
HTTP: env("HTTP", ":8000"),
Tendermint: env("TENDERMINT", "http://localhost:26657"),
HostPort: env("HOST_PORT", "localhost:80"),
}
Tendermint: env("TENDERMINT", "http://localhost:26657")}

if err := run(conf); err != nil {
log.Fatal(err)
Expand Down Expand Up @@ -92,12 +89,11 @@ func run(conf Configuration) error {
rt.Handle("/gov/proposals", &handlers.GovProposalsHandler{Bns: bnscli})
rt.Handle("/gov/votes", &handlers.GovVotesHandler{Bns: bnscli})
rt.Handle("/gconf/", &handlers.GconfHandler{Bns: bnscli, Confs: gconfConfigurations})
rt.Handle("/", &handlers.DefaultHandler{HostPort: conf.HostPort})
rt.Handle("/", &handlers.DefaultHandler{})

docs.SwaggerInfo.Title = "IOV Name Service Rest API"
docs.SwaggerInfo.Version = util.BuildVersion
docs.SwaggerInfo.Host = conf.HostPort
docsUrl := fmt.Sprintf("http://%s/docs/doc.json", conf.HostPort)
docsUrl := fmt.Sprintf("doc.json")
rt.Handle("/docs/", httpSwagger.Handler(httpSwagger.URL(docsUrl)))

if err := http.ListenAndServe(conf.HTTP, rt); err != nil {
Expand Down

0 comments on commit d4fda3b

Please sign in to comment.