Skip to content

Commit

Permalink
Merge pull request #7 from cheqd/general-improvements
Browse files Browse the repository at this point in the history
Improve logging and configuration
  • Loading branch information
Toktar committed May 13, 2022
2 parents 5964b6c + 53d1c33 commit c5fb30c
Show file tree
Hide file tree
Showing 15 changed files with 468 additions and 186 deletions.
25 changes: 25 additions & 0 deletions cmd/print_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cmd

import (
"github.com/cheqd/did-resolver/utils"
"github.com/spf13/cobra"
)

func getPrintConfigCmd() *cobra.Command {
return &cobra.Command{
Use: "print-config",
Short: "Prints the active configuration",
RunE: func(cmd *cobra.Command, args []string) error {
return printConfig()
},
}
}

func printConfig() error {
config := utils.MustLoadConfig()
configYaml := config.MustMarshalYaml()

println(configYaml)

return nil
}
16 changes: 16 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package cmd

import (
"github.com/spf13/cobra"
)

func GetRootCmd() *cobra.Command {
rootCmd := &cobra.Command{
Use: "cheqd-did-resolver",
Short: "Did resolver for the cheqd method",
}

rootCmd.AddCommand(getServeCmd(), getPrintConfigCmd())

return rootCmd
}
95 changes: 95 additions & 0 deletions cmd/serve.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package cmd

import (
"github.com/cheqd/did-resolver/services"
"github.com/cheqd/did-resolver/types"
"github.com/cheqd/did-resolver/utils"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"net/http"
"strings"
)

func getServeCmd() *cobra.Command {
return &cobra.Command{
Use: "serve",
Short: "Runs resolver as a web server",
Run: func(cmd *cobra.Command, args []string) {
serve()
},
}
}

func serve() {
log.Info().Msg("Loading configuration")
config, err := utils.LoadConfig()
if err != nil {
panic(err)
}

log.Info().Msgf("Configuration: %s", config.MustMarshalJson())

log.Info().Msgf("Setting log level: %s", config.LogLevel)
level, err := zerolog.ParseLevel(config.LogLevel)
if err != nil {
panic(err)
}
zerolog.SetGlobalLevel(level)

// Echo instance
e := echo.New()

// Middleware
e.Use(middleware.Logger())
e.Use(middleware.Recover())

// Services
ledgerService := services.NewLedgerService(config.Ledger.Timeout)

networks := strings.Split(config.Ledger.Networks, ";")
for _, network := range networks {
args := strings.Split(network, "=")
name, url := args[0], args[1]

log.Info().Msgf("Registering network. Name: %s, url: %s.", name, url)
err := ledgerService.RegisterLedger(name, url)
if err != nil {
panic(err)
}
}

requestService := services.NewRequestService(config.Resolver.Method, ledgerService)

// Routes
e.GET(config.Api.ResolverPath, func(c echo.Context) error {
didUrl := c.Param("did")
log.Debug().Msgf("DID: %s", didUrl)

accept := strings.Split(c.Request().Header.Get(echo.HeaderAccept), ";")[0]
log.Trace().Msgf("Accept: %s", accept)

var requestedContentType types.ContentType
if strings.Contains(accept, string(types.JSONLD)) {
requestedContentType = types.DIDJSONLD
} else {
requestedContentType = types.DIDJSON
}
log.Debug().Msgf("Requested content type: %s", requestedContentType)

responseBody, err := requestService.ProcessDIDRequest(didUrl, types.ResolutionOption{Accept: requestedContentType})
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}

log.Debug().Msgf("Response body: %s", responseBody)

c.Response().Header().Set(echo.HeaderContentType, string(requestedContentType))
return c.JSONBlob(http.StatusOK, []byte(responseBody))
})

log.Info().Msg("Starting listener")
log.Fatal().Err(e.Start(config.Api.Listener))
}
19 changes: 11 additions & 8 deletions config.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
method: cheqd
listener: 0.0.0.0:1313
resolverPath: "/1.0/identifiers/:did"
ledgerTimeout: 5s
loglevel: debug
ledger:
networks: "mainnet=rpc.cheqd.net:443;testnet=159.89.208.88:443"
timeout: "5s"

networks:
mainnet: rpc.cheqd.net:443
testnet: 159.89.208.88:443
resolver:
method: "cheqd"

api:
listener: "0.0.0.0:1313"
resolverPath: "/1.0/identifiers/:did"

logLevel: "debug"
41 changes: 26 additions & 15 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,43 +1,54 @@
###############################################################
#####################################################################
### STAGE 1: Build cheqd-did-resolver binary pre-requisites ###
###############################################################
#####################################################################

FROM golang:1.17.8-buster as builder

WORKDIR /root

COPY types ./types
COPY services ./services
COPY cmd ./cmd
COPY utils ./utils
COPY go.mod .
COPY go.sum .
COPY main.go .

# Make cheqd-did-resolver binary
RUN go build -o cheqd-did-resolver main.go

###############################################################
#####################################################################
### STAGE 2: Build cheqd-did-resolver runner ###
###############################################################
#####################################################################

FROM ubuntu:focal AS runner
LABEL org.opencontainers.image.description "Cheqd DID-Resolver runner"
LABEL org.opencontainers.image.source "https://github.com/cheqd/cheqd-did-resolver"
ENV CHEQD_RESOLVER_HOME_DIR="/home/cheqd-resolver"

# Set user directory and details
LABEL org.opencontainers.image.description="Cheqd DID-Resolver runner"
LABEL org.opencontainers.image.source="https://github.com/cheqd/did-resolver"


# Set user details
ARG UID=1000
ARG GID=1000
# Add cheqd user to use in the container
RUN groupadd --system --gid $GID cheqd-resolver \
&& useradd --system --create-home --home-dir ${CHEQD_RESOLVER_HOME_DIR} --shell /bin/bash --gid cheqd-resolver --uid $UID cheqd-resolver

ARG USER="resolver"
ARG GROUP="resolver"

ARG HOME="/home/resolver"

# Create user and group
RUN groupadd --system --gid $GID $USER &&\
useradd --system --create-home --home-dir $HOME --shell /bin/bash --gid $GROUP --uid $UID $USER

# Copy compiled cheqd-did-resolver binary from Stage 1
COPY --from=builder /root /bin

# Copy base config.yaml
WORKDIR ${CHEQD_RESOLVER_HOME_DIR}
COPY config.yaml $HOME

WORKDIR $HOME
USER $USER

USER cheqd-resolver
COPY config.yaml ${CHEQD_RESOLVER_HOME_DIR}
EXPOSE 1313
ENTRYPOINT ["cheqd-did-resolver"]

ENTRYPOINT ["cheqd-did-resolver", "serve"]
11 changes: 6 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
module github.com/cheqd/cheqd-did-resolver
module github.com/cheqd/did-resolver

go 1.17

require (
github.com/cheqd/cheqd-node v0.5.0
github.com/labstack/echo/v4 v4.7.2
github.com/rs/zerolog v1.23.0
github.com/spf13/cobra v1.2.1
github.com/spf13/viper v1.11.0
github.com/stretchr/testify v1.7.1
google.golang.org/grpc v1.45.0
google.golang.org/protobuf v1.28.0
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
)

require (
Expand Down Expand Up @@ -69,10 +74,8 @@ require (
github.com/sasha-s/go-deadlock v0.2.1-0.20190427202633-1595213edefa // indirect
github.com/spf13/afero v1.8.2 // indirect
github.com/spf13/cast v1.4.1 // indirect
github.com/spf13/cobra v1.2.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.11.0 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20200815110645-5c35d600f0ca // indirect
github.com/tecbot/gorocksdb v0.0.0-20191217155057-f0fad39f321c // indirect
Expand All @@ -81,10 +84,8 @@ require (
github.com/tendermint/tm-db v0.6.6 // indirect
go.etcd.io/bbolt v1.3.6 // indirect
google.golang.org/genproto v0.0.0-20220407144326-9054f6ed7bac // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/ini.v1 v1.66.4 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)

require (
Expand Down
Loading

0 comments on commit c5fb30c

Please sign in to comment.