Skip to content

Commit 51989ca

Browse files
committed
feat: dockerfile and docker-composep
1 parent cc307d6 commit 51989ca

23 files changed

Lines changed: 273 additions & 114 deletions

Dockerfile

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
FROM golang:1.21-alpine3.18 AS builder
2+
3+
WORKDIR /app
4+
5+
COPY go.mod go.sum ./
6+
RUN go mod download
7+
8+
COPY . .
9+
10+
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o build/authonomy .
11+
12+
FROM alpine:latest
13+
14+
RUN apk --no-cache add ca-certificates
15+
16+
WORKDIR /root/
17+
18+
COPY --from=builder /app/build/authonomy .
19+
20+
COPY --from=builder /app/config.yaml .
21+
22+
# sample schemas
23+
COPY --from=builder /app/sample_schema ./sample_schema
24+
25+
EXPOSE 8081
26+
27+
CMD ["./authonomy"]

Makefile

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Variables
2+
BINARY_FOLDER = build
3+
BINARY_NAME = authonomy
4+
DOCKER_IMAGE_NAME = authonomy-image
5+
6+
# Run the server
7+
run:
8+
@echo "Running the server..."
9+
@go run main.go start
10+
11+
# Build the Go binary
12+
build:
13+
@echo "Building the Go binary..."
14+
@go build -o $(BINARY_FOLDER)/$(BINARY_NAME)
15+
16+
# Lint the Go code
17+
lint:
18+
@echo "Linting the Go code..."
19+
@golangci-lint run
20+
21+
## TESTS
22+
TEST_PACKAGES=$(shell go list ./...)
23+
TEST_TARGETS := test-unit test-race
24+
BASE_FLAGS=-mod=readonly -timeout=5m
25+
test-unit: ARGS=-tags=norace
26+
test-race: ARGS=-race
27+
$(TEST_TARGETS): run-tests
28+
29+
run-tests:
30+
@echo "--> Running tests $(BASE_FLAGS) $(ARGS)"
31+
ifneq (,$(shell which tparse 2>/dev/null))
32+
@go test $(BASE_FLAGS) -json $(ARGS) $(TEST_PACKAGES) | tparse
33+
else
34+
@go test $(BASE_FLAGS) $(ARGS) $(TEST_PACKAGES)
35+
endif
36+
37+
38+
# Build a Docker image
39+
docker-build:
40+
@echo "Building Docker image..."
41+
@docker build -t $(DOCKER_IMAGE_NAME) .
42+
43+
# Run the application inside a Docker container
44+
docker-run:
45+
@echo "Running Docker container..."
46+
@docker run -p 7322:7322 $(DOCKER_IMAGE_NAME)
47+
48+
# Clean up
49+
clean:
50+
@echo "Cleaning up..."
51+
@rm $(BINARY_NAME)
52+
53+
.PHONY: run build lint docker-build docker-run clean

cmd/cmd.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/spf13/cobra"
8+
"github.com/spf13/viper"
9+
)
10+
11+
var rootCmd = &cobra.Command{
12+
Use: "authonomy",
13+
Short: "authonomy service",
14+
Long: "authonomy service build over ssi service",
15+
}
16+
17+
func init() {
18+
cobra.OnInitialize(getConfig)
19+
rootCmd.AddCommand(startCmd)
20+
startCmd.Flags().BoolVarP(&resetFlag, "reset", "r", false, "Reset the service")
21+
}
22+
23+
func getConfig() {
24+
// Set the base name of the config file, without the file extension.
25+
viper.SetConfigName("config")
26+
// Set the path to look for the config file in.
27+
viper.AddConfigPath(".")
28+
// Read in environment variables that match
29+
viper.AutomaticEnv()
30+
// If a config file is found, read it in.
31+
if err := viper.ReadInConfig(); err == nil {
32+
fmt.Println("Using config file:", viper.ConfigFileUsed())
33+
} else {
34+
fmt.Println("Error reading config file:", err)
35+
}
36+
}
37+
38+
func servicePort() string {
39+
port := viper.GetString("service.port")
40+
if port == "" {
41+
port = "8081"
42+
}
43+
return ":" + port
44+
}
45+
46+
var resetFlag bool
47+
var startCmd = &cobra.Command{
48+
Use: "start",
49+
Short: "Start the authonomy service",
50+
Run: func(cmd *cobra.Command, args []string) {
51+
dbPath := viper.GetString("service.badger_path")
52+
secret := viper.GetString("service.db_encryption_key")
53+
apiKey := viper.GetString("api.x-api-key")
54+
ssiUrl := viper.GetString("service.ssi_service_url")
55+
Start(dbPath, secret, apiKey, servicePort(), ssiUrl, resetFlag)
56+
},
57+
}
58+
59+
func Execute() {
60+
err := rootCmd.Execute()
61+
if err != nil {
62+
fmt.Println("Error:", err)
63+
os.Exit(1)
64+
}
65+
}
Lines changed: 25 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,48 @@
11
package cmd
22

33
import (
4+
"authonomy/pkg/handlers"
5+
"authonomy/services"
6+
"authonomy/store"
47
"fmt"
5-
"identitysphere-api/pkg/handlers"
6-
"identitysphere-api/services"
7-
"identitysphere-api/store"
88
"log"
99
"net/http"
1010

11-
_ "identitysphere-api/docs" // Swaggo generates docs in this package
11+
_ "authonomy/docs" // Swaggo generates docs in this package
1212

13-
"github.com/spf13/viper"
1413
httpSwagger "github.com/swaggo/http-swagger"
1514
)
1615

17-
func getConfig() {
18-
// Set the base name of the config file, without the file extension.
19-
viper.SetConfigName("config")
20-
// Set the path to look for the config file in.
21-
viper.AddConfigPath(".")
22-
// Read in environment variables that match
23-
viper.AutomaticEnv()
24-
// If a config file is found, read it in.
25-
if err := viper.ReadInConfig(); err == nil {
26-
fmt.Println("Using config file:", viper.ConfigFileUsed())
27-
} else {
28-
fmt.Println("Error reading config file:", err)
29-
}
30-
}
31-
32-
func Start() {
33-
getConfig()
34-
dbPath := viper.GetString("service.badger_path")
35-
secret := viper.GetString("service.db_encryption_key")
16+
func Start(dbPath, secret, apiKey, port, ssiUrl string, reset bool) {
3617
// Initialize the data store (e.g., database connection)
3718
store, err := store.NewStore(dbPath, secret)
3819
if err != nil {
3920
log.Fatalf("Failed to initialize the database: %v", err)
4021
}
4122
defer store.Close()
42-
// // clear db before start (for the demo)
43-
// err = store.ClearDB()
44-
// if err != nil {
45-
// log.Fatalf("Failed to clean the database: %v", err)
46-
// }
4723
// Initialize services with dependencies
48-
ssiService := services.NewSsiClient()
49-
// // Dummy policies
50-
// err = services.CreateDemoPolicies(ssiService, store)
51-
// if err != nil {
52-
// log.Fatalf("Failed to create policies: %v", err)
53-
// }
54-
apiKey := viper.GetString("api.x-api-key")
24+
ssiService := services.NewSsiClient(ssiUrl)
25+
// clear db before start (for the demo) or use the reset flag
26+
if reset {
27+
err = store.ClearDB()
28+
if err != nil {
29+
log.Fatalf("Failed to clean the database: %v", err)
30+
}
31+
32+
// Dummy policies
33+
err = services.CreateDemoPolicies(ssiService, store)
34+
if err != nil {
35+
log.Fatalf("Failed to create policies: %v", err)
36+
}
37+
}
38+
5539
fmt.Println("=======================")
5640
fmt.Println("\033[32m", "------x-api-key------", "\033[0m")
5741
fmt.Println("\033[32m", apiKey, "\033[0m")
5842
fmt.Println("=======================")
43+
fmt.Println("=====Swagger=======")
44+
fmt.Println("\033[32m", fmt.Sprintf("http://localhost%s/swagger", port), "\033[0m")
45+
fmt.Println("=======================")
5946
// Initialize handlers with services
6047
m := handlers.NewMiddlewareService(apiKey)
6148
appHandler := handlers.NewAppHandler(ssiService, store)
@@ -100,6 +87,6 @@ func Start() {
10087
fs := http.FileServer(http.Dir("web"))
10188
http.Handle("/web/", http.StripPrefix("/web/", fs))
10289
// Start the server
103-
log.Println("Starting server on port 8081...")
104-
log.Fatal(http.ListenAndServe(":8081", nil))
90+
log.Printf("Starting server on port %s", port)
91+
log.Fatal(http.ListenAndServe(port, nil))
10592
}

config.yaml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# config.yaml
22
service:
3-
badger_path: "./badger_db"
4-
db_encryption_key: "badger"
5-
jwt_encryption_key: "random"
3+
badger_path: ./badger_db
4+
db_encryption_key: badger
5+
jwt_encryption_key: random
6+
port: 8081
7+
ssi_service_url : http://172.23.0.5:8080/v1
68
api:
7-
x-api-key: "9dfccbf4-8c41-412f-841f-8cc10e4a26be"
9+
x-api-key: 9dfccbf4-8c41-412f-841f-8cc10e4a26be
810

docker-compose.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
version: '3.8'
3+
4+
services:
5+
authonomy:
6+
build:
7+
context: .
8+
dockerfile: Dockerfile
9+
ports:
10+
- "8081:8081"
11+
command: ./authonomy start --reset
12+
volumes:
13+
- .:/app
14+
- ./config.yaml:/root/config.yaml
15+
- ./sample_schema:/root/sample_schema
16+

go.mod

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module identitysphere-api
1+
module authonomy
22

33
go 1.21.4
44

@@ -7,9 +7,9 @@ require (
77
github.com/dgraph-io/badger/v3 v3.2103.5
88
github.com/go-playground/validator v9.31.0+incompatible
99
github.com/golang-jwt/jwt v3.2.2+incompatible
10-
github.com/google/uuid v1.3.0
11-
github.com/lestrrat-go/jwx/v2 v2.0.9-0.20230429214153-5090ec1bd2cd
10+
github.com/lestrrat-go/jwx/v2 v2.0.18
1211
github.com/pkg/errors v0.9.1
12+
github.com/spf13/cobra v0.0.5
1313
github.com/spf13/viper v1.3.2
1414
github.com/swaggo/http-swagger v1.3.4
1515
github.com/swaggo/swag v1.16.2
@@ -26,31 +26,34 @@ require (
2626
github.com/dgraph-io/ristretto v0.1.1 // indirect
2727
github.com/dustin/go-humanize v1.0.0 // indirect
2828
github.com/fsnotify/fsnotify v1.4.7 // indirect
29+
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
2930
github.com/go-openapi/jsonpointer v0.20.2 // indirect
3031
github.com/go-openapi/jsonreference v0.20.4 // indirect
3132
github.com/go-openapi/spec v0.20.13 // indirect
3233
github.com/go-openapi/swag v0.22.7 // indirect
3334
github.com/go-playground/locales v0.14.1 // indirect
3435
github.com/go-playground/universal-translator v0.18.1 // indirect
35-
github.com/go-playground/validator/v10 v10.13.0 // indirect
36+
github.com/go-playground/validator/v10 v10.14.0 // indirect
3637
github.com/goccy/go-json v0.10.2 // indirect
3738
github.com/gogo/protobuf v1.3.2 // indirect
3839
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b // indirect
3940
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6 // indirect
4041
github.com/golang/protobuf v1.5.2 // indirect
4142
github.com/golang/snappy v0.0.4 // indirect
4243
github.com/google/flatbuffers v1.12.1 // indirect
44+
github.com/google/uuid v1.3.0 // indirect
4345
github.com/hashicorp/hcl v1.0.0 // indirect
4446
github.com/hyperledger/aries-framework-go v0.3.1 // indirect
4547
github.com/hyperledger/aries-framework-go/component/kmscrypto v0.0.0-20230427134832-0c9969493bd3 // indirect
4648
github.com/hyperledger/aries-framework-go/component/log v0.0.0-20230427134832-0c9969493bd3 // indirect
4749
github.com/hyperledger/aries-framework-go/component/models v0.0.0-20230501135648-a9a7ad029347 // indirect
4850
github.com/hyperledger/aries-framework-go/spi v0.0.0-20230427134832-0c9969493bd3 // indirect
51+
github.com/inconshreveable/mousetrap v1.0.0 // indirect
4952
github.com/josharian/intern v1.0.0 // indirect
5053
github.com/kilic/bls12-381 v0.1.1-0.20210503002446-7b7597926c69 // indirect
5154
github.com/klauspost/compress v1.12.3 // indirect
5255
github.com/leodido/go-urn v1.2.4 // indirect
53-
github.com/lestrrat-go/blackmagic v1.0.1 // indirect
56+
github.com/lestrrat-go/blackmagic v1.0.2 // indirect
5457
github.com/lestrrat-go/httpcc v1.0.1 // indirect
5558
github.com/lestrrat-go/httprc v1.0.4 // indirect
5659
github.com/lestrrat-go/iter v1.0.2 // indirect
@@ -68,6 +71,7 @@ require (
6871
github.com/piprate/json-gold v0.5.0 // indirect
6972
github.com/pmezard/go-difflib v1.0.0 // indirect
7073
github.com/pquerna/cachecontrol v0.1.0 // indirect
74+
github.com/segmentio/asm v1.2.0 // indirect
7175
github.com/sirupsen/logrus v1.9.0 // indirect
7276
github.com/spf13/afero v1.1.2 // indirect
7377
github.com/spf13/cast v1.3.0 // indirect
@@ -81,7 +85,7 @@ require (
8185
golang.org/x/sys v0.15.0 // indirect
8286
golang.org/x/text v0.14.0 // indirect
8387
golang.org/x/tools v0.16.1 // indirect
84-
google.golang.org/protobuf v1.28.1 // indirect
88+
google.golang.org/protobuf v1.30.0 // indirect
8589
gopkg.in/go-playground/assert.v1 v1.2.1 // indirect
8690
gopkg.in/yaml.v2 v2.4.0 // indirect
8791
gopkg.in/yaml.v3 v3.0.1 // indirect

0 commit comments

Comments
 (0)