From 26c47a0e8c273e40d9ea19f2653089a77e9c4b5c Mon Sep 17 00:00:00 2001 From: Mariano Uvalle Date: Fri, 10 Nov 2023 10:35:45 +0000 Subject: [PATCH] Working with deployment to fly.io --- cmd/cli/getClipboard.go | 4 +++- cmd/cli/main.go | 2 +- cmd/server/Dockerfile | 19 +++++++++++++++++ cmd/server/litefs.yml | 46 +++++++++++++++++++++++++++++++++++++++++ fly.toml | 27 ++++++++++++++++++++++++ go.mod | 3 +-- go.sum | 3 --- pkg/crypto/keys.go | 20 ++++++++++++++++-- 8 files changed, 115 insertions(+), 9 deletions(-) create mode 100644 cmd/server/Dockerfile create mode 100644 cmd/server/litefs.yml create mode 100644 fly.toml diff --git a/cmd/cli/getClipboard.go b/cmd/cli/getClipboard.go index 480601c..fd990cf 100644 --- a/cmd/cli/getClipboard.go +++ b/cmd/cli/getClipboard.go @@ -3,6 +3,7 @@ package main import ( "errors" "fmt" + "os" "github.com/AYM1607/ccclip/internal/configfile" "github.com/spf13/cobra" @@ -34,7 +35,8 @@ var getClipboardCmd = &cobra.Command{ return fmt.Errorf("could not set clipboard: %w", err) } - fmt.Printf("Your current clipbard is %q\n", plain) + fmt.Printf("Your current clipbard is:") + fmt.Fprintf(os.Stdout, plain) return nil }, } diff --git a/cmd/cli/main.go b/cmd/cli/main.go index 207cfa8..33c5c82 100644 --- a/cmd/cli/main.go +++ b/cmd/cli/main.go @@ -13,7 +13,7 @@ func b64(i []byte) string { var apiclient *client.Client func init() { - apiclient = client.New("http://localhost:8080") + apiclient = client.New("https://api.ccclip.io") } func main() { diff --git a/cmd/server/Dockerfile b/cmd/server/Dockerfile new file mode 100644 index 0000000..41104b2 --- /dev/null +++ b/cmd/server/Dockerfile @@ -0,0 +1,19 @@ +FROM golang:1.21-alpine AS builder +# Ensure we have a c compiler. +RUN apk add --no-cache build-base ca-certificates fuse3 sqlite + +# Install LiteFS for distribute SQLite +COPY --from=flyio/litefs:0.5 /usr/local/bin/litefs /usr/local/bin/litefs + +COPY cmd/server/litefs.yml /etc/litefs.yml + +WORKDIR /src +COPY go.mod . +COPY go.sum . +RUN go mod download +COPY . . +RUN go build -ldflags='-s -w' -tags 'linux' -trimpath -o /dist/app ./cmd/server + +EXPOSE 3000 + +ENTRYPOINT ["litefs", "mount"] diff --git a/cmd/server/litefs.yml b/cmd/server/litefs.yml new file mode 100644 index 0000000..4ff55ca --- /dev/null +++ b/cmd/server/litefs.yml @@ -0,0 +1,46 @@ +# The fuse section describes settings for the FUSE file system. This file system +# is used as a thin layer between the SQLite client in your application and the +# storage on disk. It intercepts disk writes to determine transaction boundaries +# so that those transactions can be saved and shipped to replicas. +fuse: + dir: "/litefs" + +# The data section describes settings for the internal LiteFS storage. We'll +# mount a volume to the data directory so it can be persisted across restarts. +# However, this data should not be accessed directly by the user application. +data: + dir: "/var/lib/litefs" + +# This flag ensure that LiteFS continues to run if there is an issue on starup. +# It makes it easy to ssh in and debug any issues you might be having rather +# than continually restarting on initialization failure. +exit-on-error: false + +# This section defines settings for the option HTTP proxy. +# This proxy can handle primary forwarding & replica consistency +# for applications that use a single SQLite database. +proxy: + addr: ":8080" + target: "localhost:3000" + db: "ccclip.db" + +# This section defines a list of commands to run after LiteFS has connected +# and sync'd with the cluster. You can run multiple commands but LiteFS expects +# the last command to be long-running (e.g. an application server). When the +# last command exits, LiteFS is shut down. +exec: + - cmd: "/dist/app" + +# The lease section specifies how the cluster will be managed. We're using the +# "consul" lease type so that our application can dynamically change the primary. +# +# These environment variables will be available in your Fly.io application. +lease: + type: "consul" + advertise-url: "http://${HOSTNAME}.vm.${FLY_APP_NAME}.internal:20202" + candidate: ${FLY_REGION == PRIMARY_REGION} + promote: true + + consul: + url: "${FLY_CONSUL_URL}" + key: "litefs/${FLY_APP_NAME}" diff --git a/fly.toml b/fly.toml new file mode 100644 index 0000000..06956e5 --- /dev/null +++ b/fly.toml @@ -0,0 +1,27 @@ +# fly.toml app configuration file generated for dark-paper-8180 on 2023-11-10T08:23:22Z +# +# See https://fly.io/docs/reference/configuration/ for information about how to use this file. +# + +app = "dark-paper-8180" +primary_region = "sea" + +[build] + dockerfile = "./cmd/server/Dockerfile" + +[env] + CCCLIP_LOAD_RAW_KEYS = "true" + CCCLIP_PORT = "3000" + CCCLIP_DATABASE_LOCATION = "/litefs/ccclip.db" + +[mounts] + source = "litefs" + destination = "/var/lib/litefs" + +[http_service] + internal_port = 8080 +# force_https = true + auto_stop_machines = true + auto_start_machines = true + min_machines_running = 1 + processes = ["app"] diff --git a/go.mod b/go.mod index beea1bd..8cf5f16 100644 --- a/go.mod +++ b/go.mod @@ -4,16 +4,15 @@ go 1.21 require ( github.com/gorilla/mux v1.8.0 + github.com/mattn/go-sqlite3 v1.14.18 github.com/oklog/ulid/v2 v2.1.0 github.com/spf13/cobra v1.8.0 golang.org/x/crypto v0.14.0 golang.org/x/term v0.13.0 - gopkg.in/yaml.v2 v2.4.0 ) require ( github.com/inconshreveable/mousetrap v1.1.0 // indirect - github.com/mattn/go-sqlite3 v1.14.18 // indirect github.com/spf13/pflag v1.0.5 // indirect golang.org/x/sys v0.13.0 // indirect ) diff --git a/go.sum b/go.sum index 68fecef..64876d1 100644 --- a/go.sum +++ b/go.sum @@ -19,8 +19,5 @@ golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.13.0 h1:bb+I9cTfFazGW51MZqBVmZy7+JEJMouUHTUSKVQLBek= golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U= -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.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= -gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/pkg/crypto/keys.go b/pkg/crypto/keys.go index 1b31990..ae84d08 100644 --- a/pkg/crypto/keys.go +++ b/pkg/crypto/keys.go @@ -81,18 +81,34 @@ func PublicKeyFromBytes(keyBytes []byte) *ecdh.PublicKey { } func LoadPrivateKey(fp string) (*ecdh.PrivateKey, error) { - kb, err := loadKey(fp) + var kb []byte + var err error + if os.Getenv("CCCLIP_LOAD_RAW_KEYS") == "" { + kb, err = loadKey(fp) + } else { + kb = make([]byte, KeySize) + base64.StdEncoding.Decode(kb, []byte(fp)) + } if err != nil { return nil, err } + return PrivateKeyFromBytes(kb), nil } func LoadPublicKey(fp string) (*ecdh.PublicKey, error) { - kb, err := loadKey(fp) + var kb []byte + var err error + if os.Getenv("CCCLIP_LOAD_RAW_KEYS") == "" { + kb, err = loadKey(fp) + } else { + kb = make([]byte, KeySize) + base64.StdEncoding.Decode(kb, []byte(fp)) + } if err != nil { return nil, err } + return PublicKeyFromBytes(kb), nil }