| 105 | } |
| 106 | |
| 107 | func run(args []string) error { |
| 108 | if len(args) == 0 { |
| 109 | return errors.New("") |
| 110 | } |
| 111 | |
| 112 | fs := flag.NewFlagSet(args[0], flag.ExitOnError) |
| 113 | var ( |
| 114 | addr = fs.String("addr", ":8000", "The address to host the sync server at") |
| 115 | shouldProxy = fs.Bool("proxy", true, "Whether or not certain endpoints (like /user_info) should proxy data to the real API, or return fake data.") |
| 116 | |
| 117 | // Backend blob storage stuff |
| 118 | s3Bucket = fs.String("s3_bucket", "", "Name of the S3 bucket to hand out temp credentials for.") |
| 119 | s3Region = fs.String("s3_region", "us-west-2", "Name of the S3 region where AWS resources live") |
| 120 | s3RoleARN = fs.String("s3_role_arn", "", "ARN of the role to grant temporary credentials for S3 bucket access from.") |
| 121 | |
| 122 | useSQLite = fs.Bool("use_sqlite", true, "If true, use the SQLite backend instead of the in-memory database") |
| 123 | sqlitePath = fs.String("sqlite_path", "logseq-sync.db", "Path to the SQLite database, will be created if it doesn't exist") |
| 124 | ) |
| 125 | if err := fs.Parse(args[1:]); err != nil { |
| 126 | return fmt.Errorf("failed to parse flags: %w", err) |
| 127 | } |
| 128 | |
| 129 | mux := http.NewServeMux() |
| 130 | |
| 131 | // (def API-DOMAIN "api.logseq.com") |
| 132 | apiTarget := &url.URL{ |
| 133 | Scheme: "https", |
| 134 | Host: "api.logseq.com", |
| 135 | } |
| 136 | |
| 137 | awsBlob, err := awsblob.New(*s3Bucket, *s3Region, *s3RoleARN) |
| 138 | if err != nil { |
| 139 | return fmt.Errorf("failed to init AWS blob backend: %w", err) |
| 140 | } |
| 141 | |
| 142 | var db DB |
| 143 | if *useSQLite { |
| 144 | log.Printf("Using SQLite datbase at %q", *sqlitePath) |
| 145 | sdb, err := sqlite.New(*sqlitePath) |
| 146 | if err != nil { |
| 147 | return fmt.Errorf("failed to open SQLite database: %w", err) |
| 148 | } |
| 149 | db = sdb |
| 150 | } else { |
| 151 | db = mem.New() |
| 152 | } |
| 153 | |
| 154 | s := server{ |
| 155 | blob: awsBlob, |
| 156 | db: db, |
| 157 | shouldProxy: *shouldProxy, |
| 158 | proxy: &httputil.ReverseProxy{ |
| 159 | Rewrite: func(r *httputil.ProxyRequest) { |
| 160 | r.SetURL(apiTarget) |
| 161 | }, |
| 162 | }, |
| 163 | now: func() time.Time { return time.Now() }, |
| 164 | r: cryptorandrand.New(), |