Skip to content

Commit a0cb02c

Browse files
authored
Merge pull request #4 from michaelcoll/feat/web-daemon-registering
feat: Web - Daemon registering
2 parents 0286219 + 017ffde commit a0cb02c

File tree

15 files changed

+407
-92
lines changed

15 files changed

+407
-92
lines changed

.github/dependabot.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ updates:
77
- package-ecosystem: "gomod"
88
directory: "/"
99
schedule:
10-
interval: "weekly"
10+
interval: "daily"

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,4 @@ jobs:
4242
goos: ${{ matrix.goos }}
4343
goarch: ${{ matrix.goarch }}
4444
binary_name: "gallery-web"
45-
ldflags: -X "github.com/michaelcoll/gallery-web/cmd.version=${{ env.APP_VERSION }}"
45+
ldflags: -X "github.com/michaelcoll/gallery-web/cmd.version=${{ env.APP_VERSION }}" -s -w

Makefile

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,21 @@ build: build-web build-go
33
build-go:
44
go build -v .
55

6+
build-prod:
7+
go build -v -ldflags="-s -w -X 'github.com/michaelcoll/gallery-web/cmd.version=v0.0.0'" .
8+
69
build-web:
710
cd internal/web \
811
&& pnpm run build
912

10-
gen: protoc
11-
12-
protoc:
13-
protoc --go_out=. \
14-
--go_opt=paths=source_relative \
15-
--go-grpc_out=. \
16-
--go-grpc_opt=paths=source_relative \
17-
proto/gallery.proto
18-
19-
clean:
20-
rm proto/*.pb.go
21-
2213
run:
23-
go run . index -f ~/Images/Photos
14+
go run . serve
2415

2516
prepare:
2617
cd internal/web \
2718
&& corepack enable && corepack prepare \
28-
&& pnpm i
19+
&& pnpm i
20+
21+
dep-upgrade:
22+
go get -u
23+
go mod tidy

cmd/serve.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ var serveCmd = &cobra.Command{
2828
Short: "Starts the server",
2929
Long: ``,
3030
Run: func(cmd *cobra.Command, args []string) {
31-
photo.New().GetController().Serve()
31+
module := photo.New()
32+
go module.GetDaemonController().Serve()
33+
module.GetPhotoController().Serve()
3234
},
3335
}
3436

go.mod

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ go 1.18
55
require (
66
github.com/fatih/color v1.13.0
77
github.com/gin-gonic/gin v1.8.1
8-
github.com/michaelcoll/gallery-proto v0.2.0
8+
github.com/google/uuid v1.3.0
9+
github.com/michaelcoll/gallery-proto v0.3.0
910
github.com/spf13/cobra v1.5.0
10-
google.golang.org/grpc v1.49.0
11+
google.golang.org/grpc v1.50.0
1112
)
1213

1314
require (
@@ -20,18 +21,18 @@ require (
2021
github.com/inconshreveable/mousetrap v1.0.1 // indirect
2122
github.com/json-iterator/go v1.1.12 // indirect
2223
github.com/leodido/go-urn v1.2.1 // indirect
23-
github.com/mattn/go-colorable v0.1.9 // indirect
24+
github.com/mattn/go-colorable v0.1.13 // indirect
2425
github.com/mattn/go-isatty v0.0.16 // indirect
2526
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
2627
github.com/modern-go/reflect2 v1.0.2 // indirect
2728
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
2829
github.com/spf13/pflag v1.0.5 // indirect
2930
github.com/ugorji/go/codec v1.2.7 // indirect
30-
golang.org/x/crypto v0.0.0-20220926161630-eccd6366d1be // indirect
31-
golang.org/x/net v0.0.0-20221002022538-bcab6841153b // indirect
32-
golang.org/x/sys v0.0.0-20220928140112-f11e5e49a4ec // indirect
31+
golang.org/x/crypto v0.0.0-20221005025214-4161e89ecf1b // indirect
32+
golang.org/x/net v0.0.0-20221004154528-8021a29435af // indirect
33+
golang.org/x/sys v0.0.0-20221006211917-84dc82d7e875 // indirect
3334
golang.org/x/text v0.3.7 // indirect
34-
google.golang.org/genproto v0.0.0-20220829175752-36a9c930ecbf // indirect
35+
google.golang.org/genproto v0.0.0-20220930163606-c98284e70a91 // indirect
3536
google.golang.org/protobuf v1.28.1 // indirect
3637
gopkg.in/yaml.v2 v2.4.0 // indirect
3738
)

go.sum

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiu
2525
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
2626
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
2727
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
28+
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
29+
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
2830
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
2931
github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc=
3032
github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
@@ -40,14 +42,15 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
4042
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
4143
github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w=
4244
github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY=
43-
github.com/mattn/go-colorable v0.1.9 h1:sqDoxXbdeALODt0DAeJCVp38ps9ZogZEAXjus69YV3U=
4445
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
46+
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
47+
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
4548
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
4649
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
4750
github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ=
4851
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
49-
github.com/michaelcoll/gallery-proto v0.2.0 h1:ptpF4RK8P26nrv4ZyFHD01wNHW6L261uT9OC2V0TR5c=
50-
github.com/michaelcoll/gallery-proto v0.2.0/go.mod h1:u+8HDPcdiLSkZgArkWvFTD3fsGI7sddH6iZRH0An0aE=
52+
github.com/michaelcoll/gallery-proto v0.3.0 h1:NgsERwiqX1DhzFdqfN0Z/6BbmaqYHTThD4fUWcIDabg=
53+
github.com/michaelcoll/gallery-proto v0.3.0/go.mod h1:u+8HDPcdiLSkZgArkWvFTD3fsGI7sddH6iZRH0An0aE=
5154
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
5255
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
5356
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
@@ -78,11 +81,11 @@ github.com/ugorji/go v1.2.7/go.mod h1:nF9osbDWLy6bDVv/Rtoh6QgnvNDpmCalQV5urGCCS6
7881
github.com/ugorji/go/codec v1.2.7 h1:YPXUKf7fYbp/y8xloBqZOw2qaVggbfwMlI8WM3wZUJ0=
7982
github.com/ugorji/go/codec v1.2.7/go.mod h1:WGN1fab3R1fzQlVQTkfxVtIBhWDRqOviHU95kRgeqEY=
8083
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
81-
golang.org/x/crypto v0.0.0-20220926161630-eccd6366d1be h1:fmw3UbQh+nxngCAHrDCCztao/kbYFnWjoqop8dHx05A=
82-
golang.org/x/crypto v0.0.0-20220926161630-eccd6366d1be/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
84+
golang.org/x/crypto v0.0.0-20221005025214-4161e89ecf1b h1:huxqepDufQpLLIRXiVkTvnxrzJlpwmIWAObmcCcUFr0=
85+
golang.org/x/crypto v0.0.0-20221005025214-4161e89ecf1b/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
8386
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
84-
golang.org/x/net v0.0.0-20221002022538-bcab6841153b h1:6e93nYa3hNqAvLr0pD4PN1fFS+gKzp2zAXqrnTCstqU=
85-
golang.org/x/net v0.0.0-20221002022538-bcab6841153b/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
87+
golang.org/x/net v0.0.0-20221004154528-8021a29435af h1:wv66FM3rLZGPdxpYL+ApnDe2HzHcTFta3z5nsc13wI4=
88+
golang.org/x/net v0.0.0-20221004154528-8021a29435af/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
8689
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
8790
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
8891
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
@@ -91,19 +94,19 @@ golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBc
9194
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
9295
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
9396
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
94-
golang.org/x/sys v0.0.0-20220928140112-f11e5e49a4ec h1:BkDtF2Ih9xZ7le9ndzTA7KJow28VbQW3odyk/8drmuI=
95-
golang.org/x/sys v0.0.0-20220928140112-f11e5e49a4ec/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
97+
golang.org/x/sys v0.0.0-20221006211917-84dc82d7e875 h1:AzgQNqF+FKwyQ5LbVrVqOcuuFB67N47F9+htZYH0wFM=
98+
golang.org/x/sys v0.0.0-20221006211917-84dc82d7e875/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
9699
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
97100
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
98101
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
99102
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
100103
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
101104
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
102105
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
103-
google.golang.org/genproto v0.0.0-20220829175752-36a9c930ecbf h1:Q5xNKbTSFwkuaaGaR7CMcXEM5sy19KYdUU8iF8/iRC0=
104-
google.golang.org/genproto v0.0.0-20220829175752-36a9c930ecbf/go.mod h1:dbqgFATTzChvnt+ujMdZwITVAJHFtfyN1qUhDqEiIlk=
105-
google.golang.org/grpc v1.49.0 h1:WTLtQzmQori5FUH25Pq4WT22oCsv8USpQ+F6rqtsmxw=
106-
google.golang.org/grpc v1.49.0/go.mod h1:ZgQEeidpAuNRZ8iRrlBKXZQP1ghovWIVhdJRyCDK+GI=
106+
google.golang.org/genproto v0.0.0-20220930163606-c98284e70a91 h1:Ezh2cpcnP5Rq60sLensUsFnxh7P6513NLvNtCm9iyJ4=
107+
google.golang.org/genproto v0.0.0-20220930163606-c98284e70a91/go.mod h1:3526vdqwhZAwq4wsRUaVG555sVgsNmIjRtO7t/JH29U=
108+
google.golang.org/grpc v1.50.0 h1:fPVVDxY9w++VjTZsYvXWqEf9Rqar/e+9zYfxKK+W+YU=
109+
google.golang.org/grpc v1.50.0/go.mod h1:ZgQEeidpAuNRZ8iRrlBKXZQP1ghovWIVhdJRyCDK+GI=
107110
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
108111
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
109112
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=

internal/photo/domain/service/daemonCaller.go renamed to internal/photo/domain/model/daemon.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,21 @@
1414
* limitations under the License.
1515
*/
1616

17-
package service
17+
package model
1818

19-
import "github.com/michaelcoll/gallery-web/internal/photo/domain/model"
19+
import (
20+
"time"
2021

21-
type DaemonCaller interface {
22-
List() ([]*model.Photo, error)
23-
GetByHash(string) (*model.Photo, error)
24-
ContentByHash(hash string) ([]byte, error)
22+
"github.com/google/uuid"
23+
)
24+
25+
// Daemon a representation of a daemon
26+
type Daemon struct {
27+
Id uuid.UUID
28+
Name string
29+
Hostname string
30+
Port int
31+
Version string
32+
Alive bool
33+
NextSee time.Time
2534
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*
2+
* Copyright (c) 2022 Michaël COLL.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package service
18+
19+
import (
20+
"context"
21+
"errors"
22+
"fmt"
23+
"strconv"
24+
"sync"
25+
"time"
26+
27+
"github.com/fatih/color"
28+
"github.com/google/uuid"
29+
30+
"github.com/michaelcoll/gallery-web/internal/photo/domain/model"
31+
)
32+
33+
const expiresIn = 2
34+
35+
type DaemonService struct {
36+
c PhotoServiceCaller
37+
daemons map[uuid.UUID]*model.Daemon
38+
mu sync.Mutex
39+
}
40+
41+
func NewDaemonService(c PhotoServiceCaller) DaemonService {
42+
return DaemonService{
43+
c: c,
44+
daemons: make(map[uuid.UUID]*model.Daemon),
45+
}
46+
}
47+
48+
func (s *DaemonService) Register(d *model.Daemon) (uuid.UUID, int32, error) {
49+
fmt.Printf("%s Registering a new daemon %s (%s) located at %s:%s...",
50+
color.GreenString("❗"),
51+
color.GreenString(d.Name),
52+
color.GreenString(d.Version),
53+
color.CyanString(d.Hostname), color.CyanString(strconv.Itoa(d.Port)))
54+
55+
if s.validateDaemonConnection(d) {
56+
s.activate(d)
57+
58+
s.daemons[d.Id] = d
59+
60+
fmt.Println(color.GreenString(" ✅ OK"))
61+
62+
return d.Id, expiresIn, nil
63+
} else {
64+
65+
fmt.Println(color.RedString(" ❌ KO"))
66+
67+
return [16]byte{}, 0, errors.New("could not establish connection to your daemon")
68+
}
69+
}
70+
71+
func (s *DaemonService) HeartBeat(id uuid.UUID) error {
72+
daemon, exists := s.daemons[id]
73+
74+
if exists {
75+
s.activate(daemon)
76+
} else {
77+
fmt.Printf("%s Daemon %s not found...\n", color.RedString("❌"), color.RedString(id.String()))
78+
return errors.New("daemon not found")
79+
}
80+
81+
return nil
82+
}
83+
84+
func (s *DaemonService) activate(d *model.Daemon) {
85+
if !d.Alive {
86+
fmt.Printf("%s Daemon %s reconnected.\n",
87+
color.GreenString("❗"),
88+
color.GreenString(d.Name))
89+
}
90+
s.mu.Lock()
91+
d.NextSee = time.Now().Add(time.Duration(expiresIn) * time.Second)
92+
d.Alive = true
93+
s.mu.Unlock()
94+
}
95+
96+
func (s *DaemonService) validateDaemonConnection(d *model.Daemon) bool {
97+
_, err := s.c.Exists(context.Background(), *d, "0")
98+
if err != nil {
99+
return false
100+
}
101+
102+
return true
103+
}
104+
105+
func (s *DaemonService) Watch() {
106+
for {
107+
for _, d := range s.daemons {
108+
109+
if d.NextSee.Before(time.Now()) && d.Alive {
110+
s.mu.Lock()
111+
d.Alive = false
112+
s.mu.Unlock()
113+
114+
fmt.Printf("%s Daemon %s unregistered.\n",
115+
color.YellowString("❗"),
116+
color.YellowString(d.Name))
117+
}
118+
}
119+
120+
time.Sleep(1 * time.Second)
121+
}
122+
}

internal/photo/domain/service/photoService.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,28 @@
1616

1717
package service
1818

19-
import "github.com/michaelcoll/gallery-web/internal/photo/domain/model"
19+
import (
20+
"context"
21+
22+
"github.com/michaelcoll/gallery-web/internal/photo/domain/model"
23+
)
2024

2125
type PhotoService struct {
22-
c DaemonCaller
26+
c PhotoServiceCaller
2327
}
2428

25-
func New(c DaemonCaller) PhotoService {
29+
func NewPhotoService(c PhotoServiceCaller) PhotoService {
2630
return PhotoService{c: c}
2731
}
2832

29-
func (s *PhotoService) List() ([]*model.Photo, error) {
30-
return s.c.List()
33+
func (s *PhotoService) List(ctx context.Context) ([]*model.Photo, error) {
34+
return s.c.List(ctx, model.Daemon{Hostname: "localhost", Port: 9000})
3135
}
3236

33-
func (s *PhotoService) GetByHash(hash string) (*model.Photo, error) {
34-
return s.c.GetByHash(hash)
37+
func (s *PhotoService) GetByHash(ctx context.Context, hash string) (*model.Photo, error) {
38+
return s.c.GetByHash(ctx, model.Daemon{Hostname: "localhost", Port: 9000}, hash)
3539
}
3640

37-
func (s *PhotoService) ContentByHash(hash string) ([]byte, error) {
38-
return s.c.ContentByHash(hash)
41+
func (s *PhotoService) ContentByHash(ctx context.Context, hash string) ([]byte, error) {
42+
return s.c.ContentByHash(ctx, model.Daemon{Hostname: "localhost", Port: 9000}, hash)
3943
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) 2022 Michaël COLL.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package service
18+
19+
import (
20+
"context"
21+
22+
"github.com/michaelcoll/gallery-web/internal/photo/domain/model"
23+
)
24+
25+
type PhotoServiceCaller interface {
26+
List(ctx context.Context, d model.Daemon) ([]*model.Photo, error)
27+
GetByHash(ctx context.Context, d model.Daemon, hash string) (*model.Photo, error)
28+
Exists(ctx context.Context, d model.Daemon, hash string) (bool, error)
29+
ContentByHash(ctx context.Context, d model.Daemon, hash string) ([]byte, error)
30+
}

0 commit comments

Comments
 (0)