generated from cheqd/.github
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
437 additions
and
174 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/cheqd/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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/cheqd/cheqd-did-resolver/services" | ||
"github.com/cheqd/cheqd-did-resolver/types" | ||
"github.com/cheqd/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() | ||
|
||
networks := strings.Split(config.Resolver.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, accept) | ||
return c.JSONBlob(http.StatusOK, []byte(responseBody)) | ||
}) | ||
|
||
log.Info().Msg("Starting listener") | ||
log.Fatal().Err(e.Start(config.Api.Listener)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
method: cheqd | ||
listener: 0.0.0.0:1313 | ||
resolverPath: "/1.0/identifiers/:did" | ||
ledgerTimeout: 5s | ||
loglevel: debug | ||
resolver: | ||
method: "cheqd" | ||
networks: "mainnet=rpc.cheqd.net:443;testnet=159.89.208.88:443" | ||
ledgerTimeout: "5s" | ||
|
||
networks: | ||
mainnet: rpc.cheqd.net:443 | ||
testnet: 159.89.208.88:443 | ||
api: | ||
listener: "0.0.0.0:1313" | ||
resolverPath: "/1.0/identifiers/:did" | ||
|
||
logLevel: "debug" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,53 @@ | ||
############################################################### | ||
##################################################################### | ||
### 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 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/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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.