Commit 80ac55d472ece40e963956273f34f8be7fe9290d

Parents: 8465ea713989354cc7024ee8a3b74c0d09895173

From: Moritz Poldrack <git@moritz.sh>
Date: Fri Jan 26 23:06:30 2024 +0700

db/sqlite: add a basic database
Originally I wanted to try out sqlc, had some issues, but got it going
with some help, only to learn that the rarely used UPDATE SQL statement
is not supported. So handcrafted SQL it is.

Stats

.gitignore +2/-0
Makefile +31/-0
README.md +25/-0
go.mod +29/-0
go.sum +75/-0
internal/config/config.go +15/-0
internal/datastore/interface.go +24/-0
internal/datastore/sqlite/queries.go +97/-0
internal/datastore/sqlite/schema.sql +7/-0
internal/datastore/sqlite/setup.go +43/-0
internal/datastore/sqlite/type.go +41/-0
internal/datastore/types.go +8/-0
main.go +34/-0

Changeset

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000000000000000000000000000000000000..97c5a72371a4ca53a3c112b676d2b01d0121d0f8
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+*.db
+4wd*
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000000000000000000000000000000000000..e033c8c41524ea3c2a92e78cba0ba9b0e339e60f
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,31 @@
+GO?=$(shell which go)
+GOEXE:=$(shell go env GOEXE)
+BUILD_OPTS?=-trimpath -v
+SQLC?=$(GO) run github.com/sqlc-dev/sqlc/cmd/sqlc@v1.25.0
+
+VERSION?=$(shell git describe --always --dirty || echo 0.0.1)
+
+GO_LDFLAGS:=
+GO_LDFLAGS+=-X mpldr.codes/4wd/main.Version=$(VERSION)
+GO_LDFLAGS+=$(EXTRA_GO_LDFLAGS)
+
+GOSRC!=find * -type f \( -name '*.go' -and -not -name '*_test.go' \)
+GOSRC+=go.mod go.sum
+
+.PHONY: all
+all: 4wd$(GOEXE)
+
+4wd$(GOEXE): $(GOSRC) 
+	$(GO) build $(BUILD_OPTS) -ldflags "$(GO_LDFLAGS)" -o $@
+
+internal/client/api/: client-api-spec.json go.mod
+	@-mkdir -p $@
+	$(GO) run github.com/ogen-go/ogen/cmd/ogen --target internal/client/api/ --clean $<
+	touch $@
+
+AUTHORS: .git/index
+	git log '--pretty=format:%an%n%(trailers:key=co-authored-by,valueonly)' | sed -e 's/ <.*//' | sort -f | uniq | tail -n+2 > $@
+
+.PHONY: ui/dist
+ui/dist:
+	$(MAKE) -C ui dist
diff --git a/README.md b/README.md
new file mode 100644
index 0000000000000000000000000000000000000000..fd098ce7b4d6ea92fa917e169a8dabcc4d350191
--- /dev/null
+++ b/README.md
@@ -0,0 +1,25 @@
+# 4WD
+
+Get forwarded.
+
+## Why?
+
+This is more or less an attempt at trying out new things. The UI is made with
+elm, the code is licensed under the EUPL, and the only supported way of running
+it is in a container. (feel free to make it work outside though)
+
+Since this is a tiny project, don't expect a ton of resources. Documentation
+will be made available. Whenever I feel like it at least. 
+
+## Container
+
+Container images are provided as described in my [Container
+Policy](https://moritz.sh/container-policy/).
+
+[<img alt="Container-Registry Logo" src="https://moritz.sh/images/logos/container-registry.svg" height="128">](https://container-registry.com/)
+
+## Licence
+<!--     this is for you, puggy -->
+
+Most of this thing is licensed under the EUPL Version 1.2. The rest is CC-0.
+Take it, break it, use it, have fun doing it :)
diff --git a/go.mod b/go.mod
new file mode 100644
index 0000000000000000000000000000000000000000..be0da5a25e121e84d1a1041d69f9ab5ddf62da36
--- /dev/null
+++ b/go.mod
@@ -0,0 +1,29 @@
+module mpldr.codes/4wd
+
+go 1.21.6
+
+require (
+	github.com/caarlos0/env/v10 v10.0.0
+	modernc.org/sqlite v1.28.0
+)
+
+require (
+	github.com/dustin/go-humanize v1.0.1 // indirect
+	github.com/google/uuid v1.3.0 // indirect
+	github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
+	github.com/mattn/go-isatty v0.0.16 // indirect
+	github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
+	golang.org/x/mod v0.3.0 // indirect
+	golang.org/x/sys v0.9.0 // indirect
+	golang.org/x/tools v0.0.0-20201124115921-2c860bdd6e78 // indirect
+	golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
+	lukechampine.com/uint128 v1.2.0 // indirect
+	modernc.org/cc/v3 v3.40.0 // indirect
+	modernc.org/ccgo/v3 v3.16.13 // indirect
+	modernc.org/libc v1.29.0 // indirect
+	modernc.org/mathutil v1.6.0 // indirect
+	modernc.org/memory v1.7.2 // indirect
+	modernc.org/opt v0.1.3 // indirect
+	modernc.org/strutil v1.1.3 // indirect
+	modernc.org/token v1.0.1 // indirect
+)
diff --git a/go.sum b/go.sum
new file mode 100644
index 0000000000000000000000000000000000000000..70b910dc8d4429e515561df160a6cc2b19491f7f
--- /dev/null
+++ b/go.sum
@@ -0,0 +1,75 @@
+github.com/caarlos0/env/v10 v10.0.0 h1:yIHUBZGsyqCnpTkbjk8asUlx6RFhhEs+h7TOBdgdzXA=
+github.com/caarlos0/env/v10 v10.0.0/go.mod h1:ZfulV76NvVPw3tm591U4SwL3Xx9ldzBP9aGxzeN7G18=
+github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
+github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
+github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
+github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
+github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26 h1:Xim43kblpZXfIBQsbuBVKCudVG457BR2GZFIz3uw3hQ=
+github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26/go.mod h1:dDKJzRmX4S37WGHujM7tX//fmj1uioxKzKxz3lo4HJo=
+github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
+github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
+github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
+github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8=
+github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ=
+github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
+github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y=
+github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
+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/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
+github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
+github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
+golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
+golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
+golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
+golang.org/x/mod v0.3.0 h1:RM4zey1++hCTbCVQfnWeKs9/IEsaBLA8vTkd0WVtmH4=
+golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
+golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
+golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
+golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
+golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
+golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
+golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
+golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/sys v0.9.0 h1:KS/R3tvhPqvJvwcKfnBHJwwthS11LRhmM5D59eEXa0s=
+golang.org/x/sys v0.9.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
+golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
+golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
+golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
+golang.org/x/tools v0.0.0-20201124115921-2c860bdd6e78 h1:M8tBwCtWD/cZV9DZpFYRUgaymAYAr+aIUTWzDaM3uPs=
+golang.org/x/tools v0.0.0-20201124115921-2c860bdd6e78/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
+golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
+golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
+golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
+golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
+lukechampine.com/uint128 v1.2.0 h1:mBi/5l91vocEN8otkC5bDLhi2KdCticRiwbdB0O+rjI=
+lukechampine.com/uint128 v1.2.0/go.mod h1:c4eWIwlEGaxC/+H1VguhU4PHXNWDCDMUlWdIWl2j1gk=
+modernc.org/cc/v3 v3.40.0 h1:P3g79IUS/93SYhtoeaHW+kRCIrYaxJ27MFPv+7kaTOw=
+modernc.org/cc/v3 v3.40.0/go.mod h1:/bTg4dnWkSXowUO6ssQKnOV0yMVxDYNIsIrzqTFDGH0=
+modernc.org/ccgo/v3 v3.16.13 h1:Mkgdzl46i5F/CNR/Kj80Ri59hC8TKAhZrYSaqvkwzUw=
+modernc.org/ccgo/v3 v3.16.13/go.mod h1:2Quk+5YgpImhPjv2Qsob1DnZ/4som1lJTodubIcoUkY=
+modernc.org/ccorpus v1.11.6 h1:J16RXiiqiCgua6+ZvQot4yUuUy8zxgqbqEEUuGPlISk=
+modernc.org/ccorpus v1.11.6/go.mod h1:2gEUTrWqdpH2pXsmTM1ZkjeSrUWDpjMu2T6m29L/ErQ=
+modernc.org/httpfs v1.0.6 h1:AAgIpFZRXuYnkjftxTAZwMIiwEqAfk8aVB2/oA6nAeM=
+modernc.org/httpfs v1.0.6/go.mod h1:7dosgurJGp0sPaRanU53W4xZYKh14wfzX420oZADeHM=
+modernc.org/libc v1.29.0 h1:tTFRFq69YKCF2QyGNuRUQxKBm1uZZLubf6Cjh/pVHXs=
+modernc.org/libc v1.29.0/go.mod h1:DaG/4Q3LRRdqpiLyP0C2m1B8ZMGkQ+cCgOIjEtQlYhQ=
+modernc.org/mathutil v1.6.0 h1:fRe9+AmYlaej+64JsEEhoWuAYBkOtQiMEU7n/XgfYi4=
+modernc.org/mathutil v1.6.0/go.mod h1:Ui5Q9q1TR2gFm0AQRqQUaBWFLAhQpCwNcuhBOSedWPo=
+modernc.org/memory v1.7.2 h1:Klh90S215mmH8c9gO98QxQFsY+W451E8AnzjoE2ee1E=
+modernc.org/memory v1.7.2/go.mod h1:NO4NVCQy0N7ln+T9ngWqOQfi7ley4vpwvARR+Hjw95E=
+modernc.org/opt v0.1.3 h1:3XOZf2yznlhC+ibLltsDGzABUGVx8J6pnFMS3E4dcq4=
+modernc.org/opt v0.1.3/go.mod h1:WdSiB5evDcignE70guQKxYUl14mgWtbClRi5wmkkTX0=
+modernc.org/sqlite v1.28.0 h1:Zx+LyDDmXczNnEQdvPuEfcFVA2ZPyaD7UCZDjef3BHQ=
+modernc.org/sqlite v1.28.0/go.mod h1:Qxpazz0zH8Z1xCFyi5GSL3FzbtZ3fvbjmywNogldEW0=
+modernc.org/strutil v1.1.3 h1:fNMm+oJklMGYfU9Ylcywl0CO5O6nTfaowNsh2wpPjzY=
+modernc.org/strutil v1.1.3/go.mod h1:MEHNA7PdEnEwLvspRMtWTNnp2nnyvMfkimT1NKNAGbw=
+modernc.org/tcl v1.15.2 h1:C4ybAYCGJw968e+Me18oW55kD/FexcHbqH2xak1ROSY=
+modernc.org/tcl v1.15.2/go.mod h1:3+k/ZaEbKrC8ePv8zJWPtBSW0V7Gg9g8rkmhI1Kfs3c=
+modernc.org/token v1.0.1 h1:A3qvTqOwexpfZZeyI0FeGPDlSWX5pjZu9hF4lU+EKWg=
+modernc.org/token v1.0.1/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM=
+modernc.org/z v1.7.3 h1:zDJf6iHjrnB+WRD88stbXokugjyc0/pB91ri1gO6LZY=
+modernc.org/z v1.7.3/go.mod h1:Ipv4tsdxZRbQyLq9Q1M6gdbkxYzdlrciF2Hi/lS7nWE=
diff --git a/internal/config/config.go b/internal/config/config.go
new file mode 100644
index 0000000000000000000000000000000000000000..805770e870202e0a5e2ab881ba7b92c78d3d1d97
--- /dev/null
+++ b/internal/config/config.go
@@ -0,0 +1,15 @@
+package config
+
+import "github.com/caarlos0/env/v10"
+
+type Cfg struct {
+	Database string `env:"DATABASE"`
+}
+
+func init() {
+	if err := env.Parse(&Config); err != nil {
+		panic(err)
+	}
+}
+
+var Config Cfg
diff --git a/internal/datastore/interface.go b/internal/datastore/interface.go
new file mode 100644
index 0000000000000000000000000000000000000000..41714d6ee2c3b9053489ea724f7baa0f89dff8c8
--- /dev/null
+++ b/internal/datastore/interface.go
@@ -0,0 +1,24 @@
+package datastore
+
+import "errors"
+
+type Interface interface {
+	// Open opens the data storage backend.
+	Open(connection string) error
+	// Close closes the data storage backend.
+	Close() error
+	// Exists returns whether a given short code already exists in the
+	// store.
+	Exists(short string) (bool, error)
+	// AddRedirect adds a new redirect to the store.
+	AddRedirect(short, URL string) error
+	// GetRedirect returns the URL associated with a given short code.
+	GetRedirect(short string, countAsHit bool) (*Redirect, error)
+}
+
+var (
+	ErrNotFound = errors.New("not found")
+	ErrExists   = errors.New("already exists")
+	ErrInvalid  = errors.New("invalid")
+	ErrExpired  = errors.New("expired")
+)
diff --git a/internal/datastore/sqlite/queries.go b/internal/datastore/sqlite/queries.go
new file mode 100644
index 0000000000000000000000000000000000000000..e9e5cdb4548aaf0fc471e161209e932bf8b4554c
--- /dev/null
+++ b/internal/datastore/sqlite/queries.go
@@ -0,0 +1,97 @@
+package sqlite
+
+import (
+	"database/sql"
+	"fmt"
+	"sync"
+
+	"mpldr.codes/4wd/internal/datastore"
+)
+
+var (
+	checkStmt    *sql.Stmt
+	prepareCheck sync.Once
+)
+
+func check(db *sql.DB) error {
+	var err error
+	prepareCheck.Do(func() {
+		const query = `
+		SELECT
+			COUNT(ID),
+			COUNT(Destination),
+			COUNT(Hits),
+			COUNT(Owner)
+		FROM redirections;`
+		checkStmt, err = db.Prepare(query)
+	})
+	if err != nil {
+		return fmt.Errorf("failed to prepare verification query: %w", err)
+	}
+
+	_, err = checkStmt.Exec()
+	return err
+}
+
+var (
+	redirQueryStmt    *sql.Stmt
+	prepareRedirQuery sync.Once
+)
+
+func redirQuery(db *sql.DB, short string, increment bool) (*datastore.Redirect, error) {
+	var err error
+	prepareRedirQuery.Do(func() {
+		const query = `UPDATE redirections
+SET Hits = Hits + ?
+WHERE ID = ?;
+SELECT
+	ID,
+	Destination,
+	Hits,
+	Owner
+FROM redirections
+WHERE ID = ?;`
+		redirQueryStmt, err = db.Prepare(query)
+	})
+	if err != nil {
+		return nil, fmt.Errorf("failed to prepare redirection query: %w", err)
+	}
+
+	var res datastore.Redirect
+	var inc int
+	if increment {
+		inc = 1
+	}
+	err = redirQueryStmt.QueryRow(inc, short, short).Scan(
+		&res.Short, &res.URL, &res.Hits, &res.Owner,
+	)
+	if err != nil {
+		return nil, fmt.Errorf("failed to query redirect %q: %w", short, err)
+	}
+	return &res, nil
+}
+
+var (
+	redirInsertStmt    *sql.Stmt
+	prepareRedirInsert sync.Once
+)
+
+func redirInsert(db *sql.DB, short string, target string) error {
+	var err error
+	prepareRedirInsert.Do(func() {
+		const query = `INSERT INTO redirections
+(ID, Destination)
+VALUES
+(?, ?);`
+		redirInsertStmt, err = db.Prepare(query)
+	})
+	if err != nil {
+		return fmt.Errorf("failed to prepare insertion query: %w", err)
+	}
+
+	_, err = redirQueryStmt.Exec(short, target)
+	if err != nil {
+		return fmt.Errorf("failed to insert %q: %w", short, err)
+	}
+	return nil
+}
diff --git a/internal/datastore/sqlite/schema.sql b/internal/datastore/sqlite/schema.sql
new file mode 100644
index 0000000000000000000000000000000000000000..e8cf947d9b429b91485ecd7de0b6414ada8844e4
--- /dev/null
+++ b/internal/datastore/sqlite/schema.sql
@@ -0,0 +1,7 @@
+CREATE TABLE redirections (
+	"ID"	TEXT NOT NULL,
+	"Destination"	TEXT NOT NULL,
+	"Hits"	INTEGER NOT NULL DEFAULT 0,
+	"Owner"	TEXT NOT NULL,
+	PRIMARY KEY("ID")
+);
diff --git a/internal/datastore/sqlite/setup.go b/internal/datastore/sqlite/setup.go
new file mode 100644
index 0000000000000000000000000000000000000000..e44827db400a5c4c78b85cecf6525eb7aa750abb
--- /dev/null
+++ b/internal/datastore/sqlite/setup.go
@@ -0,0 +1,43 @@
+package sqlite
+
+import (
+	"database/sql"
+	_ "embed"
+	"fmt"
+	"net/url"
+
+	// import sqlite driver
+	_ "modernc.org/sqlite"
+)
+
+//go:embed schema.sql
+var schema string
+
+// Open opens the database and initializes the schema if necessary.
+func (s *Sqlite) Open(connection string) error {
+	if connection == "" {
+		connection = "sqlite:///data/test.db"
+	}
+	u, _ := url.Parse(connection) // error checked in main
+
+	var err error
+	s.db, err = sql.Open("sqlite", u.Path)
+	if err != nil {
+		return fmt.Errorf("failed to open database: %w", err)
+	}
+
+	err = check(s.db)
+	if err != nil {
+		_, err = s.db.Exec(schema)
+		if err != nil {
+			return fmt.Errorf("failed to initialize database: %w", err)
+		}
+	}
+
+	return nil
+}
+
+// Close closes the database.
+func (s *Sqlite) Close() error {
+	return s.db.Close()
+}
diff --git a/internal/datastore/sqlite/type.go b/internal/datastore/sqlite/type.go
new file mode 100644
index 0000000000000000000000000000000000000000..c9b8046b6f0b1c725e2bfa142b6d628e33aa58cc
--- /dev/null
+++ b/internal/datastore/sqlite/type.go
@@ -0,0 +1,41 @@
+package sqlite
+
+import (
+	"database/sql"
+	"errors"
+	"fmt"
+
+	"mpldr.codes/4wd/internal/datastore"
+)
+
+type Sqlite struct {
+	db *sql.DB
+}
+
+func (s *Sqlite) AddRedirect(short string, URL string) error {
+	return redirInsert(s.db, short, URL)
+}
+
+func (s *Sqlite) GetRedirect(short string, countAsHit bool) (*datastore.Redirect, error) {
+	redir, err := redirQuery(s.db, short, countAsHit)
+	switch {
+	case errors.Is(err, sql.ErrNoRows):
+		return nil, datastore.ErrNotFound
+	case err != nil:
+		return nil, fmt.Errorf("failed to get redirect: %w", err)
+	}
+	return redir, nil
+}
+
+func (s *Sqlite) Exists(short string) (bool, error) {
+	_, err := redirQuery(s.db, short, false)
+	switch {
+	case errors.Is(err, sql.ErrNoRows):
+		return false, nil
+	case err != nil:
+		return false, fmt.Errorf("failed to check existence: %w", err)
+	}
+	return true, nil
+}
+
+var _ datastore.Interface = new(Sqlite)
diff --git a/internal/datastore/types.go b/internal/datastore/types.go
new file mode 100644
index 0000000000000000000000000000000000000000..849451e8429ae410512b57ffde29809843c3b780
--- /dev/null
+++ b/internal/datastore/types.go
@@ -0,0 +1,8 @@
+package datastore
+
+type Redirect struct {
+	Short string
+	URL   string
+	Hits  int
+	Owner string
+}
diff --git a/main.go b/main.go
new file mode 100644
index 0000000000000000000000000000000000000000..67ba7bb297c1b401684c3bdc83cbe00a95260892
--- /dev/null
+++ b/main.go
@@ -0,0 +1,34 @@
+package main
+
+import (
+	"fmt"
+	"net/url"
+	"os"
+
+	"mpldr.codes/4wd/internal/config"
+	"mpldr.codes/4wd/internal/datastore"
+	"mpldr.codes/4wd/internal/datastore/sqlite"
+)
+
+func main() {
+	var db datastore.Interface
+	u, err := url.Parse(config.Config.Database)
+	if err != nil && config.Config.Database != "" {
+		fmt.Printf("failed to parse database URL: %v\n", err)
+		os.Exit(1)
+	}
+	switch u.Scheme {
+	case "sqlite":
+		fallthrough
+	default:
+		db = &sqlite.Sqlite{}
+	}
+	err = db.Open(config.Config.Database)
+	if err != nil {
+		fmt.Printf("failed to open database: %v\n", err)
+		os.Exit(1)
+	}
+
+	for {
+	}
+}