Skip to content

Commit

Permalink
Merge pull request #4 from vasyahuyasa/external_storage
Browse files Browse the repository at this point in the history
Path to google auth files
  • Loading branch information
vasyahuyasa authored May 14, 2020
2 parents 8acbfb7 + 0971d83 commit e4b2024
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmd/july/july
vendor
gdrive_test
credentials.*
token.json
cmd/july/july
8 changes: 7 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,10 @@ COPY --from=builder /build/july /app/july
EXPOSE 80
ENV STORAGE_DRIVER=gdrive
ENV CATALOG_ROOT=root
CMD /app/july -drv $STORAGE_DRIVER -d $CATALOG_ROOT
ENV GOOGLE_CREDENTIALS_PATH=credentials.json
env GOOGLE_TOKEN_PATH=token.json
CMD /app/july \
-s $STORAGE_DRIVER \
-d $CATALOG_ROOT \
-googlecred $GOOGLE_CREDENTIALS_PATH \
-googletoken $GOOGLE_TOKEN_PATH
10 changes: 5 additions & 5 deletions cmd/july/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ func main() {
root := flag.String("d", "./", "Root storage directory")
port := flag.Int("p", 80, "Service port")
host := flag.String("i", "0.0.0.0", "Service network interface")
driver := flag.String("drv", "local", "Storage driver (can be local, gdrive, yadisk)")
driver := flag.String("s", "local", "Storage driver (can be local, gdrive, yadisk)")

gdriveCredFile := flag.String("googlecred", "credentials.json", "Path to file with secret for google drive driver")
//gdriveTokenFile := flag.String("gtoken", "token.json", "Path to file with token for google drive driver")
gdriveCredFile := flag.String("googlecred", "credentials.json", "Path to file with secret for google drive")
gdriveTokenFile := flag.String("googletoken", "token.json", "Path to file with token for google drive")

flag.Parse()

Expand All @@ -30,10 +30,10 @@ func main() {
case "gdrive":
o, err := gdrive.OAuth2FromFile(*gdriveCredFile)
if err != nil {
log.Fatalf("Can not load google credentials: %v", err)
log.Fatalf("Can not load google credentials from file: %v", err)
}

svc, err := gdrive.NewServiceFromOauth2(o)
svc, err := gdrive.NewServiceFromOauth2(o, *gdriveTokenFile)
if err != nil {
log.Fatalf("Can not init google client: %v", err)
}
Expand Down
3 changes: 3 additions & 0 deletions deploy/kubernetes/july/credentials-secret.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

../kubectl create secret generic july-credentials --from-file=credentials.json=./credentials.json --from-file=token.json=./token.json
76 changes: 76 additions & 0 deletions deploy/kubernetes/july/july.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: july
labels:
app: july
spec:
replicas: 1
selector:
matchLabels:
app: july
template:
metadata:
labels:
app: july
spec:
containers:
- name: july
image: vasyahuyasa/july:0.0.3
ports:
- containerPort: 80
volumeMounts:
- name: credentials
mountPath: "/app/credentials.json"
- name: token
mountPath: "/app/token.json"
env:
- name: STORAGE_DRIVER
value: "gdrive"
- name: CATALOG_ROOT
value: "root"
volumes:
- name: credentials
projected:
sources:
- secret:
name: july-credentials
items:
- key: credentials.json
path: credentials.json
- name: token
projected:
sources:
- secret:
name: july-credentials
items:
- key: token.json
path: token.json

---
apiVersion: v1
kind: Service
metadata:
name: july
labels:
run: july
spec:
ports:
- port: 80
protocol: TCP
selector:
app: july

---
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: july-ingress
spec:
rules:
- http:
paths:
- path: /
backend:
serviceName: july
servicePort: 80
12 changes: 5 additions & 7 deletions opds/storage/gdrive/gdrive.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import (

const (
MIMETypeFolder = "application/vnd.google-apps.folder"

tokFile = "token.json"
)

var _ storage.Storage = &GdriveStorage{}
Expand Down Expand Up @@ -137,8 +135,8 @@ func OAuth2FromFile(filename string) (*oauth2.Config, error) {
return config, nil
}

func NewServiceFromOauth2(config *oauth2.Config) (*drive.Service, error) {
client := getClient(config)
func NewServiceFromOauth2(config *oauth2.Config, tokenPath string) (*drive.Service, error) {
client := getClient(config, tokenPath)

srv, err := drive.New(client)
if err != nil {
Expand All @@ -148,14 +146,14 @@ func NewServiceFromOauth2(config *oauth2.Config) (*drive.Service, error) {
return srv, err
}

func getClient(config *oauth2.Config) *http.Client {
func getClient(config *oauth2.Config, tokenPath string) *http.Client {
// The file token.json stores the user's access and refresh tokens, and is
// created automatically when the authorization flow completes for the first
// time.
tok, err := tokenFromFile(tokFile)
tok, err := tokenFromFile(tokenPath)
if err != nil {
tok = getTokenFromWeb(config)
saveToken(tokFile, tok)
saveToken(tokenPath, tok)
}
return config.Client(context.Background(), tok)
}
Expand Down

0 comments on commit e4b2024

Please sign in to comment.