+32
Dockerfile
+32
Dockerfile
···
1
+
# syntax=docker/dockerfile:1
2
+
3
+
FROM golang:1.25-alpine AS builder
4
+
5
+
ENV CGO_ENABLED=1 \
6
+
GOOS=linux
7
+
8
+
RUN apk add --no-cache \
9
+
gcc \
10
+
musl-dev
11
+
12
+
WORKDIR /build
13
+
14
+
COPY go.mod go.sum ./
15
+
16
+
RUN go mod download
17
+
18
+
COPY . .
19
+
20
+
RUN go build -o /app ./cmd/main.go
21
+
22
+
FROM alpine:3.23 AS final
23
+
24
+
COPY --from=builder /app /bin/build
25
+
26
+
# https://github.com/bluesky-social/indigo/blob/main/cmd/relay/Dockerfile#L39
27
+
ENV GODEBUG=netdns=go
28
+
ENV TZ=Etc/UTC
29
+
30
+
EXPOSE 8080
31
+
32
+
CMD ["bin/build", "start"]
+14
-17
cmd/main.go
+14
-17
cmd/main.go
···
2
2
3
3
import (
4
4
"context"
5
-
"log"
6
5
"os"
7
6
7
+
"github.com/charmbracelet/log"
8
8
_ "github.com/joho/godotenv/autoload"
9
9
"github.com/urfave/cli/v3"
10
-
"tangled.org/dane.is.extraordinarily.cool/pallet/internal/app"
11
10
"tangled.org/dane.is.extraordinarily.cool/pallet/internal/db"
12
-
"tangled.org/dane.is.extraordinarily.cool/pallet/internal/logger"
11
+
"tangled.org/dane.is.extraordinarily.cool/pallet/internal/server"
13
12
)
14
-
15
13
16
14
func main() {
17
15
if err := run(os.Args); err != nil {
···
22
20
23
21
func run(args []string) error {
24
22
app := &cli.Command{
25
-
Name: "pallet",
23
+
Name: "pallet",
26
24
Usage: "pallet app server",
27
25
}
28
26
29
27
app.Flags = []cli.Flag{
30
28
&cli.StringFlag{
31
-
Name: "log-level",
32
-
Usage: "log verbosity level (eg: warn, info, debug)",
29
+
Name: "log-level",
30
+
Usage: "log verbosity level (eg: warn, info, debug)",
33
31
Sources: cli.EnvVars("GO_LOG_LEVEL", "LOG_LEVEL"),
34
32
},
35
33
}
36
34
37
35
app.Commands = []*cli.Command{
38
36
&cli.Command{
39
-
Name: "start",
40
-
Usage: "start the pallet app server",
37
+
Name: "start",
38
+
Usage: "start the pallet app server",
41
39
Action: runServer,
42
40
Flags: []cli.Flag{
43
41
&cli.StringFlag{
44
-
Name: "port",
45
-
Usage: "Port to bind the HTTP server to",
46
-
Value: ":8080",
47
-
Sources: cli.EnvVars("SERVER_PORT"),
42
+
Name: "port",
43
+
Usage: "Port to bind the HTTP server to",
44
+
Value: ":8080",
45
+
Sources: cli.EnvVars("PORT"),
48
46
},
49
47
},
50
48
},
51
49
}
52
50
53
-
54
51
return app.Run(context.Background(), args)
55
52
}
56
53
57
54
func runServer(ctx context.Context, cmd *cli.Command) error {
58
-
logger := logger.NewLogger(cmd.String("log-level"), os.Stdout)
55
+
logger := log.New(os.Stdout)
59
56
db, err := db.NewSQLiteDB()
60
57
if err != nil {
61
58
log.Fatal(err)
62
59
}
63
60
logger.Info("database created")
64
61
65
-
server := app.NewServer(cmd.String("port"), db, logger)
62
+
server := server.NewServer(cmd.String("port"), db, logger)
66
63
67
64
if err := server.Start(); err != nil {
68
65
log.Fatal(err)
69
66
}
70
67
71
68
return nil
72
-
}
69
+
}
+19
-1
go.mod
+19
-1
go.mod
···
8
8
github.com/urfave/cli/v3 v3.6.1
9
9
)
10
10
11
-
require github.com/joho/godotenv v1.5.1 // indirect
11
+
require (
12
+
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
13
+
github.com/charmbracelet/colorprofile v0.2.3-0.20250311203215-f60798e515dc // indirect
14
+
github.com/charmbracelet/lipgloss v1.1.0 // indirect
15
+
github.com/charmbracelet/log v0.4.2 // indirect
16
+
github.com/charmbracelet/x/ansi v0.8.0 // indirect
17
+
github.com/charmbracelet/x/cellbuf v0.0.13-0.20250311204145-2c3ea96c31dd // indirect
18
+
github.com/charmbracelet/x/term v0.2.1 // indirect
19
+
github.com/go-logfmt/logfmt v0.6.0 // indirect
20
+
github.com/joho/godotenv v1.5.1 // indirect
21
+
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
22
+
github.com/mattn/go-isatty v0.0.20 // indirect
23
+
github.com/mattn/go-runewidth v0.0.16 // indirect
24
+
github.com/muesli/termenv v0.16.0 // indirect
25
+
github.com/rivo/uniseg v0.4.7 // indirect
26
+
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
27
+
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
28
+
golang.org/x/sys v0.30.0 // indirect
29
+
)
+34
go.sum
+34
go.sum
···
1
+
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
2
+
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
3
+
github.com/charmbracelet/colorprofile v0.2.3-0.20250311203215-f60798e515dc h1:4pZI35227imm7yK2bGPcfpFEmuY1gc2YSTShr4iJBfs=
4
+
github.com/charmbracelet/colorprofile v0.2.3-0.20250311203215-f60798e515dc/go.mod h1:X4/0JoqgTIPSFcRA/P6INZzIuyqdFY5rm8tb41s9okk=
5
+
github.com/charmbracelet/lipgloss v1.1.0 h1:vYXsiLHVkK7fp74RkV7b2kq9+zDLoEU4MZoFqR/noCY=
6
+
github.com/charmbracelet/lipgloss v1.1.0/go.mod h1:/6Q8FR2o+kj8rz4Dq0zQc3vYf7X+B0binUUBwA0aL30=
7
+
github.com/charmbracelet/log v0.4.2 h1:hYt8Qj6a8yLnvR+h7MwsJv/XvmBJXiueUcI3cIxsyig=
8
+
github.com/charmbracelet/log v0.4.2/go.mod h1:qifHGX/tc7eluv2R6pWIpyHDDrrb/AG71Pf2ysQu5nw=
9
+
github.com/charmbracelet/x/ansi v0.8.0 h1:9GTq3xq9caJW8ZrBTe0LIe2fvfLR/bYXKTx2llXn7xE=
10
+
github.com/charmbracelet/x/ansi v0.8.0/go.mod h1:wdYl/ONOLHLIVmQaxbIYEC/cRKOQyjTkowiI4blgS9Q=
11
+
github.com/charmbracelet/x/cellbuf v0.0.13-0.20250311204145-2c3ea96c31dd h1:vy0GVL4jeHEwG5YOXDmi86oYw2yuYUGqz6a8sLwg0X8=
12
+
github.com/charmbracelet/x/cellbuf v0.0.13-0.20250311204145-2c3ea96c31dd/go.mod h1:xe0nKWGd3eJgtqZRaN9RjMtK7xUYchjzPr7q6kcvCCs=
13
+
github.com/charmbracelet/x/term v0.2.1 h1:AQeHeLZ1OqSXhrAWpYUtZyX1T3zVxfpZuEQMIQaGIAQ=
14
+
github.com/charmbracelet/x/term v0.2.1/go.mod h1:oQ4enTYFV7QN4m0i9mzHrViD7TQKvNEEkHUMCmsxdUg=
1
15
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2
16
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
17
+
github.com/go-logfmt/logfmt v0.6.0 h1:wGYYu3uicYdqXVgoYbvnkrPVXkuLM1p1ifugDMEdRi4=
18
+
github.com/go-logfmt/logfmt v0.6.0/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
3
19
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
4
20
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
5
21
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
6
22
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
23
+
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
24
+
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
25
+
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
26
+
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
27
+
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
28
+
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
7
29
github.com/mattn/go-sqlite3 v1.14.32 h1:JD12Ag3oLy1zQA+BNn74xRgaBbdhbNIDYvQUEuuErjs=
8
30
github.com/mattn/go-sqlite3 v1.14.32/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
31
+
github.com/muesli/termenv v0.16.0 h1:S5AlUN9dENB57rsbnkPyfdGuWIlkmzJjbFf0Tf5FWUc=
32
+
github.com/muesli/termenv v0.16.0/go.mod h1:ZRfOIKPFDYQoDFF4Olj7/QJbW60Ol/kL1pU3VfY/Cnk=
9
33
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
10
34
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
35
+
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
36
+
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
37
+
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
11
38
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
12
39
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
13
40
github.com/urfave/cli/v3 v3.6.1 h1:j8Qq8NyUawj/7rTYdBGrxcH7A/j7/G8Q5LhWEW4G3Mo=
14
41
github.com/urfave/cli/v3 v3.6.1/go.mod h1:ysVLtOEmg2tOy6PknnYVhDoouyC/6N42TMeoMzskhso=
42
+
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
43
+
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
44
+
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
45
+
golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo=
46
+
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
47
+
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
48
+
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
15
49
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
16
50
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
-44
internal/app/app.go
-44
internal/app/app.go
···
1
-
package app
2
-
3
-
import (
4
-
"database/sql"
5
-
"fmt"
6
-
"log/slog"
7
-
"net/http"
8
-
"time"
9
-
10
-
"github.com/gorilla/mux"
11
-
)
12
-
13
-
type Server struct {
14
-
addr string
15
-
db *sql.DB
16
-
logger *slog.Logger
17
-
}
18
-
19
-
func NewServer(addr string, db *sql.DB, logger *slog.Logger) *Server {
20
-
return &Server{
21
-
addr,
22
-
db,
23
-
logger,
24
-
}
25
-
}
26
-
27
-
func (s *Server) Start() error {
28
-
router := mux.NewRouter()
29
-
server := &http.Server{
30
-
Addr: s.addr,
31
-
WriteTimeout: 5 * time.Second,
32
-
ReadTimeout: 10 * time.Second,
33
-
IdleTimeout: 30 * time.Second,
34
-
Handler: router,
35
-
}
36
-
37
-
router.HandleFunc("/healthcheck", func(w http.ResponseWriter, r *http.Request) {
38
-
w.WriteHeader(http.StatusOK);
39
-
fmt.Fprintf(w, "reporting for duty")
40
-
})
41
-
42
-
s.logger.Info("app server started")
43
-
return server.ListenAndServe()
44
-
}
+43
-2
internal/db/db.go
+43
-2
internal/db/db.go
···
7
7
_ "github.com/mattn/go-sqlite3"
8
8
)
9
9
10
-
11
10
func NewSQLiteDB() (*sql.DB, error) {
12
11
db, err := sql.Open("sqlite3", "./pallet.db")
12
+
_, err = db.Exec(`
13
+
pragma foreign_keys = on;
14
+
pragma journal_mode = wal;
15
+
16
+
create table if not exists boards (
17
+
uri text primary key,
18
+
cid text not null,
19
+
rkey text not null,
20
+
owner text not null,
21
+
name text not null,
22
+
description text,
23
+
created_at datetime default current_timestamp,
24
+
indexed_at text not null
25
+
);
26
+
27
+
create table if not exists board_item (
28
+
uri text primary key,
29
+
cid text not null,
30
+
rkey text not null,
31
+
owner text not null,
32
+
board_id text not null,
33
+
type text not null check(type in ('text', 'image')),
34
+
text text,
35
+
36
+
blob_cid text,
37
+
mime_type text,
38
+
width integer,
39
+
height integer,
40
+
alt_text text,
41
+
42
+
position integer,
43
+
created_at datetime default current_timestamp,
44
+
indexed_at text not null,
45
+
46
+
foreign key(board_id) references boards(rkey) on delete cascade,
47
+
48
+
check (
49
+
(type = 'text' and text is not null and blob_cid is null) or
50
+
(type = 'image' and blob_cid is not null and text is null)
51
+
)
52
+
);
53
+
`)
13
54
14
55
if err != nil {
15
56
log.Fatal("error initializing database", err)
···
17
58
18
59
defer db.Close()
19
60
return db, nil
20
-
}
61
+
}
-30
internal/logger/logger.go
-30
internal/logger/logger.go
···
1
-
package logger
2
-
3
-
import (
4
-
"io"
5
-
"log/slog"
6
-
)
7
-
8
-
func NewLogger(level string, writer io.Writer) *slog.Logger {
9
-
var logLevel slog.Level
10
-
11
-
switch level {
12
-
case "error":
13
-
logLevel = slog.LevelError
14
-
case "warn":
15
-
logLevel = slog.LevelWarn
16
-
case "info":
17
-
logLevel = slog.LevelInfo
18
-
case "debug":
19
-
logLevel = slog.LevelDebug
20
-
default:
21
-
logLevel = slog.LevelInfo
22
-
}
23
-
24
-
logger := slog.New(slog.NewJSONHandler(writer, &slog.HandlerOptions{
25
-
Level: logLevel,
26
-
}))
27
-
28
-
slog.SetDefault(logger)
29
-
return logger
30
-
}
+13
internal/server/handlers.go
+13
internal/server/handlers.go
···
1
+
package server
2
+
3
+
import "net/http"
4
+
5
+
func (s *Server) handleHomeRoute(w http.ResponseWriter, r *http.Request) {
6
+
w.WriteHeader(http.StatusOK)
7
+
w.Write([]byte("hello world"))
8
+
}
9
+
10
+
func (s *Server) handleHealthCheckRoute(w http.ResponseWriter, r *http.Request) {
11
+
w.WriteHeader(http.StatusOK)
12
+
w.Write([]byte("reporting for duty"))
13
+
}
+79
internal/server/server.go
+79
internal/server/server.go
···
1
+
package server
2
+
3
+
import (
4
+
"context"
5
+
"database/sql"
6
+
"fmt"
7
+
"net/http"
8
+
"time"
9
+
10
+
"github.com/charmbracelet/log"
11
+
"github.com/gorilla/mux"
12
+
)
13
+
14
+
type Server struct {
15
+
addr string
16
+
db *sql.DB
17
+
logger *log.Logger
18
+
}
19
+
20
+
type statusRecorder struct {
21
+
http.ResponseWriter
22
+
status int
23
+
}
24
+
25
+
func NewServer(addr string, db *sql.DB, logger *log.Logger) *Server {
26
+
return &Server{
27
+
addr,
28
+
db,
29
+
logger,
30
+
}
31
+
}
32
+
33
+
func (s *Server) Start() error {
34
+
router := mux.NewRouter()
35
+
router.Use(s.requestLoggerMiddleware)
36
+
router.Use(s.contextWithTimeoutMiddleware)
37
+
server := &http.Server{
38
+
Addr: s.addr,
39
+
WriteTimeout: 5 * time.Second,
40
+
ReadTimeout: 10 * time.Second,
41
+
IdleTimeout: 30 * time.Second,
42
+
Handler: router,
43
+
}
44
+
45
+
router.HandleFunc("/healthcheck", s.handleHomeRoute)
46
+
router.HandleFunc("/", s.handleHealthCheckRoute)
47
+
48
+
s.logger.Info(fmt.Sprintf("app server started at 0.0.0.0%s", s.addr))
49
+
return server.ListenAndServe()
50
+
}
51
+
52
+
func (s *Server) contextWithTimeoutMiddleware(next http.Handler) http.Handler {
53
+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
54
+
ctx, cancel := context.WithTimeout(r.Context(), time.Second*30)
55
+
defer cancel()
56
+
r = r.WithContext(ctx)
57
+
next.ServeHTTP(w, r)
58
+
})
59
+
}
60
+
61
+
func (s *Server) requestLoggerMiddleware(next http.Handler) http.Handler {
62
+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
63
+
recorder := statusRecorder{
64
+
ResponseWriter: w,
65
+
status: http.StatusOK,
66
+
}
67
+
68
+
next.ServeHTTP(recorder, r)
69
+
var (
70
+
ip = r.RemoteAddr
71
+
method = r.Method
72
+
url = r.URL.String()
73
+
proto = r.Proto
74
+
timestamp = time.Now().Format(time.RFC850)
75
+
)
76
+
77
+
s.logger.Info(fmt.Sprintf("%s [%s] %s %s %s %d", ip, timestamp, method, url, proto, recorder.status))
78
+
})
79
+
}
+5
-2
justfile
+5
-2
justfile
lexicons/board.json
lexicons/board.json
This is a binary file and will not be displayed.
lexicons/item.json
lexicons/item.json
This is a binary file and will not be displayed.