Skip to content

Commit

Permalink
run ok
Browse files Browse the repository at this point in the history
  • Loading branch information
NhanPT committed Aug 22, 2022
1 parent ead9ee3 commit 3b6aa02
Show file tree
Hide file tree
Showing 14 changed files with 206 additions and 41 deletions.
15 changes: 15 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/main.go"
}
]
}
12 changes: 7 additions & 5 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import (
)

type Config struct {
Services ConfigServices `yaml:"services"`
Jobs []BackupJob `yaml:"jobs"`
Services []ServiceConfig `yaml:"services"`
Jobs []BackupJob `yaml:"jobs"`
}

type ConfigServices struct {
Deta DetaConfig `yaml:"deta"`
Cloudflare S3Config `yaml:"cloudflare"`
type ServiceConfig struct {
Deta DetaConfig `yaml:"deta"`
S3 S3Config `yaml:"s3"`
Name string `yaml:"name"`
Service string `yaml:"service"`
}

func LoadConfig() (*Config, error) {
Expand Down
9 changes: 5 additions & 4 deletions config/job.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package config

type BackupJob struct {
Name string `yaml:"name"`
UseZip bool `yaml:"useZip"`
Path string `yaml:"path"`
UploadTo ServiceName `yaml:"uploadTo"`
Name string `yaml:"name"`
UseZip bool `yaml:"useZip"`
Path string `yaml:"path"`
UploadTo string `yaml:"uploadTo"`
RunAt string `yaml:"runAt"`
}
8 changes: 3 additions & 5 deletions config/services.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package config

type ServiceName string

const (
Deta string = "deta"
Cloudflare string = "cloudflare"
S3 string = "s3"
ServiceName_Deta string = "deta"
ServiceName_Cloudflare string = "cloudflare"
ServiceName_S3 string = "s3"
)
1 change: 1 addition & 0 deletions dashboard/web.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package dashboard
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ require (
github.com/aws/aws-sdk-go-v2 v1.16.11
github.com/aws/aws-sdk-go-v2/config v1.17.1
github.com/aws/aws-sdk-go-v2/credentials v1.12.14
github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.11.27
github.com/aws/aws-sdk-go-v2/service/s3 v1.27.5
github.com/deta/deta-go v1.0.0
github.com/robfig/cron/v3 v3.0.1
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.4.4 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.12.12 // indirect
github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.11.27 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.18 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.12 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.3.19 // indirect
Expand All @@ -25,6 +27,5 @@ require (
github.com/aws/aws-sdk-go-v2/service/sso v1.11.17 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.16.13 // indirect
github.com/aws/smithy-go v1.12.1 // indirect
github.com/deta/deta-go v1.0.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,24 @@ github.com/aws/aws-sdk-go-v2/service/sts v1.16.13 h1:dl8T0PJlN92rvEGOEUiD0+YPYdP
github.com/aws/aws-sdk-go-v2/service/sts v1.16.13/go.mod h1:Ru3QVMLygVs/07UQ3YDur1AQZZp2tUNje8wfloFttC0=
github.com/aws/smithy-go v1.12.1 h1:yQRC55aXN/y1W10HgwHle01DRuV9Dpf31iGkotjt3Ag=
github.com/aws/smithy-go v1.12.1/go.mod h1:Tg+OJXh4MB2R/uN61Ko2f6hTZwB/ZYGOtib8J3gBHzA=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/deta/deta-go v1.0.0 h1:vg94dg2t7ChYhs8DEn4oXLzLAGGafgCSClHfMYrVFvY=
github.com/deta/deta-go v1.0.0/go.mod h1:vbQaUT8iD6xREm816eNp7Nw1aewd95dpcb/uj0T2vuY=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
67 changes: 66 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,80 @@ import (
"io"
"log"
"net/http"
"strings"

"github.com/help-14/ocean-backup/config"
"github.com/help-14/ocean-backup/services"
"github.com/help-14/ocean-backup/utils"
"github.com/robfig/cron/v3"
)

var LoadedConfig config.Config
var SupportServices []services.Service
var JobRunners []utils.JobRunner

func main() {
loadedConfig, err := config.LoadConfig()
if err != nil {
log.Fatalln("Reading config.yaml failed!", err)
}
LoadedConfig = *loadedConfig

for _, serviceConfig := range LoadedConfig.Services {
var newService services.Service

switch serviceConfig.Service {
case config.ServiceName_Deta:
newService = new(services.DetaService)
case config.ServiceName_Cloudflare:
newService = new(services.CloudFlareService)
default:
log.Fatalln("Unsupported service name: "+serviceConfig.Service, err)
}

err = newService.Setup(serviceConfig)
if err != nil {
log.Fatalln("Setup service '"+serviceConfig.Name+"' failed!", err)
}
err = newService.Connect()
if err != nil {
log.Fatalln("Connect to service '"+serviceConfig.Name+"' failed!", err)
}
log.Println("Service '" + serviceConfig.Name + "' is connected!")
SupportServices = append(SupportServices, newService)
}

for _, job := range LoadedConfig.Jobs {
var runner utils.JobRunner
runner.Job = job

cronTime := job.RunAt
if len(strings.TrimSpace(cronTime)) == 0 {
cronTime = "0 0 * * *"
}
cron := cron.New()
cron.AddFunc(cronTime, func() {
runner.Run()
})
cron.Start()
runner.Cron = cron

for _, service := range SupportServices {
if service.Name() == job.UploadTo {
runner.Service = service
}
}

JobRunners = append(JobRunners, runner)
}

// Hello world, the web server
helloHandler := func(w http.ResponseWriter, req *http.Request) {
io.WriteString(w, "Hello, world!\n")
}

http.HandleFunc("/hello", helloHandler)
log.Println("Listing for requests at http://localhost:8000/hello")
log.Println("Ocean backup is started!")
log.Println("Web dashboard is running at http://localhost:8000")
log.Fatal(http.ListenAndServe(":8000", nil))
}
24 changes: 14 additions & 10 deletions sample-config.yaml
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
services:
deta:
projectKey: default-project-key
driveName: backup
cloudflare:
url: https://accountid.r2.cloudflarestorage.com
bucket: backup
accessKeyId: 1234567890abcde
accessKeySecret: 1234567890abcdefghijklmnopqrstuvwxyz
- name: location1
service: deta
deta:
projectKey: default-project-key
driveName: backup
- name: location2
service: cloudflare
s3:
url: https://accountid.r2.cloudflarestorage.com
bucket: backup
accessKeyId: 1234567890abcde
accessKeySecret: 1234567890abcdefghijklmnopqrstuvwxyz
jobs:
- name: test
useZip: true
path: /home/pi/test/
uploadTo: deta
uploadTo: location1
- name: vault
useZip: true
path: /home/pi/vault/
uploadTo: cloudflare
uploadTo: location2
3 changes: 2 additions & 1 deletion services/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package services
import "github.com/help-14/ocean-backup/config"

type Service interface {
Setup(config config.Config) error
Name() string
Setup(config config.ServiceConfig) error

Connect() error
Disconnect() error
Expand Down
16 changes: 11 additions & 5 deletions services/cloudflare.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,22 @@ import (
)

type CloudFlareService struct {
name string
config obconfig.S3Config
client *s3.Client
}

func (service CloudFlareService) Setup(config obconfig.Config) error {
service.config = config.Services.Cloudflare
func (service *CloudFlareService) Name() string {
return service.name
}

func (service *CloudFlareService) Setup(config obconfig.ServiceConfig) error {
service.config = config.S3
service.name = config.Name
return nil
}

func (service CloudFlareService) Connect() error {
func (service *CloudFlareService) Connect() error {
r2Resolver := aws.EndpointResolverWithOptionsFunc(func(sv, region string, options ...interface{}) (aws.Endpoint, error) {
return aws.Endpoint{
URL: service.config.Url,
Expand All @@ -41,12 +47,12 @@ func (service CloudFlareService) Connect() error {
return nil
}

func (service CloudFlareService) Disconnect() error {
func (service *CloudFlareService) Disconnect() error {
service.client = nil
return nil
}

func (service CloudFlareService) Upload(localPath string, remotePath string) error {
func (service *CloudFlareService) Upload(localPath string, remotePath string) error {
uploader := manager.NewUploader(service.client)
uploadFile, err := os.Open(localPath)
if err != nil {
Expand Down
16 changes: 11 additions & 5 deletions services/deta.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@ import (
)

type DetaService struct {
name string
config obconfig.DetaConfig
drive *drive.Drive
}

func (service DetaService) Setup(config obconfig.Config) error {
service.config = config.Services.Deta
func (service *DetaService) Setup(config obconfig.ServiceConfig) error {
service.config = config.Deta
service.name = config.Name
return nil
}

func (service DetaService) Connect() error {
func (service *DetaService) Connect() error {
d, err := deta.New(deta.WithProjectKey(service.config.ProjectKey))
if err != nil {
return err
Expand All @@ -34,12 +36,16 @@ func (service DetaService) Connect() error {
return nil
}

func (service DetaService) Disconnect() error {
func (service *DetaService) Name() string {
return service.name
}

func (service *DetaService) Disconnect() error {
service.drive = nil
return nil
}

func (service DetaService) Upload(localPath string, remotePath string) error {
func (service *DetaService) Upload(localPath string, remotePath string) error {
file, err := os.Open(localPath)
if err != nil {
return err
Expand Down
6 changes: 3 additions & 3 deletions services/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package services

type S3Service struct{}

func (service S3Service) Connect() error {
func (service *S3Service) Connect() error {
return nil
}

func (service S3Service) Disconnect() error {
func (service *S3Service) Disconnect() error {
return nil
}

func (service S3Service) Upload() error {
func (service *S3Service) Upload() error {
return nil
}
Loading

0 comments on commit 3b6aa02

Please sign in to comment.