Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server impl #7

Merged
merged 14 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions .github/workflows/image-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.

# GitHub recommends pinning actions to a commit SHA.
# To get a newer version, you will need to update the SHA.
# You can also reference a tag or branch, but the action may change without warning.

name: Create and publish a Docker images

on:
push:
branches: ['main']
tags:
- 'v*.*.*'

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}

jobs:

build-and-push-ssi:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Log in to the Container registry
uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v4
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=schedule
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
type=sha

- name: Build and push Docker image
uses: docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc
with:
context: .
file: impl/build/Dockerfile
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ bower_components
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
impl/build/Release

# Dependency directories
node_modules/
Expand Down Expand Up @@ -143,3 +143,4 @@ dist

.DS_STORE
.idea
*.db
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,20 @@
The `did:dht` method. Home to the [DID DHT Method Specification](./spec.md), and a reference implementation of a server
in Go.

## Build & Run

From the root directory run:

```
docker build . -t did-dht -f build/Dockerfile
```

and then

```
docker run -p8305:8305 did-dht
```

## Implementations

| Language | Client | Server | Link |
Expand Down
19 changes: 19 additions & 0 deletions impl/build/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
FROM golang:1.21.3-alpine

# Create directory for our app inside the container
WORKDIR /app

# Prepare dependencies
COPY go.mod ./
COPY go.sum ./
RUN go mod download

# Copy code /to the container image.
COPY . ./

# Build the binary and call it "docker-ssi-service"
RUN go build -tags jwx_es256k -o /did-dht ./cmd

EXPOSE 8305

CMD [ "/did-dht" ]
9 changes: 0 additions & 9 deletions impl/cmd/cli.go

This file was deleted.

5 changes: 5 additions & 0 deletions impl/cmd/cli/cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package main

func main() {
Execute()
}
15 changes: 8 additions & 7 deletions impl/cmd/cli/identity.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cli
package main

import (
"context"
Expand All @@ -10,6 +10,7 @@ import (
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/TBD54566975/did-dht-method/config"
"github.com/TBD54566975/did-dht-method/internal"
"github.com/TBD54566975/did-dht-method/internal/cli"
"github.com/TBD54566975/did-dht-method/internal/util"
Expand Down Expand Up @@ -66,7 +67,7 @@ var identityAddCmd = &cobra.Command{
}

// start dht
d, err := dht.NewDHT()
d, err := dht.NewDHT(config.GetDefaultBootstrapPeers())
if err != nil {
logrus.WithError(err).Error("failed to create dht")
return err
Expand Down Expand Up @@ -101,14 +102,14 @@ var identityAddCmd = &cobra.Command{
Answer: rrds,
}
// generate put request
putReq, err := dht.CreatePKARRPutRequest(pubKey, privKey, msg)
putReq, err := dht.CreatePKARRPublishRequest(pubKey, privKey, msg)
if err != nil {
logrus.WithError(err).Error("failed to create put request")
return err
}

// put the identity into the dht
id, err := d.Put(context.Background(), pubKey, *putReq)
id, err := d.Put(context.Background(), *putReq)
if err != nil {
logrus.WithError(err).Error("failed to put identity into dht")
return err
Expand Down Expand Up @@ -155,20 +156,20 @@ var identityGetCmd = &cobra.Command{
// fall back to dht if not found in diddht file

// start dht
d, err := dht.NewDHT()
d, err := dht.NewDHT(config.GetDefaultBootstrapPeers())
if err != nil {
logrus.WithError(err).Error("failed to create dht")
return err
}

// get the identity from the dht
gotRR, err := d.Get(context.Background(), id)
gotResp, err := d.Get(context.Background(), id)
if err != nil {
logrus.WithError(err).Error("failed to get identity from dht")
return err
}

msg, err := dht.ParsePKARRGetResponse(gotRR)
msg, err := dht.ParsePKARRGetResponse(*gotResp)
if err != nil {
logrus.WithError(err).Error("failed to parse get response")
return err
Expand Down
2 changes: 1 addition & 1 deletion impl/cmd/cli/root.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cli
package main

import (
"os"
Expand Down
83 changes: 83 additions & 0 deletions impl/cmd/main.go
Original file line number Diff line number Diff line change
@@ -1 +1,84 @@
package main

import (
"context"
"os"
"os/signal"
"syscall"
"time"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"

"github.com/TBD54566975/did-dht-method/config"
"github.com/TBD54566975/did-dht-method/pkg/server"
)

// main godoc
//
// @title DID DHT Service API
// @description The DID DHT Service
// @contact.name TBD
// @contact.url https://github.com/TBD54566975/did-dht-method/issues
// @contact.email [email protected]
// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
// @host {{.Server.APIHost}}
// @version {{.SVN}}
func main() {
logrus.Info("Starting up...")

if err := run(); err != nil {
logrus.Fatalf("main: error: %s", err.Error())
}
}

func run() error {
configPath := config.DefaultConfigPath
envConfigPath, present := os.LookupEnv(config.ConfigPath.String())
if present {
logrus.Infof("loading config from env var path: %s", envConfigPath)
configPath = envConfigPath
}
cfg, err := config.LoadConfig(configPath)
if err != nil {
logrus.Fatalf("could not instantiate config: %s", err.Error())
}

// create a channel of buffer size 1 to handle shutdown.
// buffer's size is 1 in order to ignore any additional ctrl+c
// spamming.
shutdown := make(chan os.Signal, 1)
signal.Notify(shutdown, os.Interrupt, syscall.SIGTERM)

s, err := server.NewServer(cfg, shutdown)
if err != nil {
logrus.WithError(err).Error("could not start http services")
return err
}

serverErrors := make(chan error, 1)
go func() {
logrus.Infof("main: server started and listening on -> %s", s.Addr)
serverErrors <- s.ListenAndServe()
}()

select {
case err = <-serverErrors:
return errors.Wrap(err, "server error")
case sig := <-shutdown:
logrus.Infof("main: shutdown signal received -> %v", sig)

ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()

if err = s.Shutdown(ctx); err != nil {
if err = s.Close(); err != nil {
return err
}
return errors.Wrap(err, "main: failed to stop server gracefully")
}
}

return nil
}
Empty file added impl/config/config.env
Empty file.
Loading
Loading