From 0aee90f4afd03cd0eb70286e8840c4b96d42ef8f Mon Sep 17 00:00:00 2001 From: smantic Date: Mon, 5 May 2025 19:04:48 -0500 Subject: [PATCH] Initalize go project - Removes the existing elxir project - Sets up the go module. - Sets up goose to use for database migrations. - Begins setup of some other basic things such as the logger and the http server. --- .gitignore | 12 ++--- backend/.formatter.exs | 4 -- backend/README.md | 19 +++++++ backend/lib/application.ex | 9 ---- backend/main.go | 60 +++++++++++++++++++++ backend/migrations/20250506004650_init.sql | 9 ++++ backend/mix.exs | 26 --------- backend/test/test_helper.exs | 1 - go.mod | 27 ++++++++++ go.sum | 63 ++++++++++++++++++++++ mise.toml | 19 ++++++- 11 files changed, 200 insertions(+), 49 deletions(-) delete mode 100644 backend/.formatter.exs create mode 100644 backend/README.md delete mode 100644 backend/lib/application.ex create mode 100644 backend/main.go create mode 100644 backend/migrations/20250506004650_init.sql delete mode 100644 backend/mix.exs delete mode 100644 backend/test/test_helper.exs create mode 100644 go.mod create mode 100644 go.sum diff --git a/.gitignore b/.gitignore index be158e7..e40ed21 100644 --- a/.gitignore +++ b/.gitignore @@ -1,11 +1,8 @@ -# mix related gitignore stuff -backend/_build/ -backend/cover/ -backend/deps/ -backend/doc/ +#db +backend/ligmotes.db +# binary +backend/backend -erl_crash.dump -*.ez ligmo-*.tar tmp/ @@ -26,3 +23,4 @@ Thumbs.db .env.* !.env.example !.env.test +.envrc diff --git a/backend/.formatter.exs b/backend/.formatter.exs deleted file mode 100644 index d2cda26..0000000 --- a/backend/.formatter.exs +++ /dev/null @@ -1,4 +0,0 @@ -# Used by "mix format" -[ - inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] -] diff --git a/backend/README.md b/backend/README.md new file mode 100644 index 0000000..bbae233 --- /dev/null +++ b/backend/README.md @@ -0,0 +1,19 @@ +# Ligmotes Backend + +# Migrations +We are using goose to manage migrations. + +### Migrate Up +The server will run migrations and get you caught up. + +Otherwise you can do: + ``` + goose sqlite3 ./ligmotes.db up -dir migrations + ``` + from the backend folder. + +### Create New Migrations +From the backend folder + ``` + goose sqlite3 ./ligmotes.db create sql -dir migrations + ``` diff --git a/backend/lib/application.ex b/backend/lib/application.ex deleted file mode 100644 index c8b751d..0000000 --- a/backend/lib/application.ex +++ /dev/null @@ -1,9 +0,0 @@ -defmodule Ligmo.Application do - @moduledoc false - - use Application - - def start(_type, _args) do - :todo - end -end diff --git a/backend/main.go b/backend/main.go new file mode 100644 index 0000000..c460c58 --- /dev/null +++ b/backend/main.go @@ -0,0 +1,60 @@ +package main + +import ( + "cmp" + "context" + "database/sql" + "embed" + "log/slog" + "net/http" + "os" + "runtime/debug" + + "github.com/pressly/goose/v3" + _ "modernc.org/sqlite" +) + +var ( + //go:embed migrations/*.sql + embedMigrations embed.FS + SERVE_ADDR = cmp.Or("0.0.0.0:9876", os.Getenv("SERVE_ADDR")) +) + +func main() { + + var ( + ctx = context.Background() + buildInfo, _ = debug.ReadBuildInfo() + logger = slog.Default().With( + "go-version", buildInfo.GoVersion, + "commit", buildInfo.Main.Sum, + ) + ) + + db, err := sql.Open("sqlite", "ligmotes.db") + if err != nil { + logger.ErrorContext(ctx, "failed to open DB", "error", err) + return + } + + goose.SetBaseFS(embedMigrations) + + if err := goose.SetDialect(string(goose.DialectSQLite3)); err != nil { + logger.ErrorContext(ctx, "failed to set migration dialect", "error", err) + return + } + + // goose grabs a lock on the database when doing migrations + // so running multiple instances of the server is safe. + if err := goose.UpContext(ctx, db, "migrations"); err != nil { + logger.ErrorContext(ctx, "failed to migrate up", "error", err) + return + } + + logger.InfoContext(ctx, "serving on: "+SERVE_ADDR) + err = http.ListenAndServe(SERVE_ADDR, nil) + if err != nil { + logger.ErrorContext(ctx, "failed to serve", "error", err) + return + } +} diff --git a/backend/migrations/20250506004650_init.sql b/backend/migrations/20250506004650_init.sql new file mode 100644 index 0000000..b9c449e --- /dev/null +++ b/backend/migrations/20250506004650_init.sql @@ -0,0 +1,9 @@ +-- +goose Up +-- +goose StatementBegin +SELECT 'up SQL query'; +-- +goose StatementEnd + +-- +goose Down +-- +goose StatementBegin +SELECT 'down SQL query'; +-- +goose StatementEnd diff --git a/backend/mix.exs b/backend/mix.exs deleted file mode 100644 index 283833f..0000000 --- a/backend/mix.exs +++ /dev/null @@ -1,26 +0,0 @@ -defmodule Ligmo.MixProject do - use Mix.Project - - def project do - [ - app: :ligmo, - version: "0.1.0", - elixir: "~> 1.18", - start_permanent: Mix.env() == :prod, - deps: deps() - ] - end - - # Run "mix help compile.app" to learn about applications. - def application do - [ - extra_applications: [:logger], - mod: {Ligmo.Application, []} - ] - end - - # Run "mix help deps" to learn about dependencies. - defp deps do - [] - end -end diff --git a/backend/test/test_helper.exs b/backend/test/test_helper.exs deleted file mode 100644 index 869559e..0000000 --- a/backend/test/test_helper.exs +++ /dev/null @@ -1 +0,0 @@ -ExUnit.start() diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..4cc80d6 --- /dev/null +++ b/go.mod @@ -0,0 +1,27 @@ +module ligmotes.com + +go 1.23.0 + +toolchain go1.23.8 + +require ( + github.com/pressly/goose/v3 v3.24.2 + modernc.org/sqlite v1.36.2 +) + +require ( + github.com/dustin/go-humanize v1.0.1 // indirect + github.com/google/uuid v1.6.0 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/mfridman/interpolate v0.0.2 // indirect + github.com/ncruces/go-strftime v0.1.9 // indirect + github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect + github.com/sethvargo/go-retry v0.3.0 // indirect + go.uber.org/multierr v1.11.0 // indirect + golang.org/x/exp v0.0.0-20250305212735-054e65f0b394 // indirect + golang.org/x/sync v0.12.0 // indirect + golang.org/x/sys v0.31.0 // indirect + modernc.org/libc v1.61.13 // indirect + modernc.org/mathutil v1.7.1 // indirect + modernc.org/memory v1.9.1 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..e85e882 --- /dev/null +++ b/go.sum @@ -0,0 +1,63 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +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/pprof v0.0.0-20250317173921-a4b03ec1a45e h1:ijClszYn+mADRFY17kjQEVQ1XRhq2/JR1M3sGqeJoxs= +github.com/google/pprof v0.0.0-20250317173921-a4b03ec1a45e/go.mod h1:boTsfXsheKC2y+lKOCMpSfarhxDeIzfZG1jqGcPl3cA= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mfridman/interpolate v0.0.2 h1:pnuTK7MQIxxFz1Gr+rjSIx9u7qVjf5VOoM/u6BbAxPY= +github.com/mfridman/interpolate v0.0.2/go.mod h1:p+7uk6oE07mpE/Ik1b8EckO0O4ZXiGAfshKBWLUM9Xg= +github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdhx/f4= +github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls= +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/pressly/goose/v3 v3.24.2 h1:c/ie0Gm8rnIVKvnDQ/scHErv46jrDv9b4I0WRcFJzYU= +github.com/pressly/goose/v3 v3.24.2/go.mod h1:kjefwFB0eR4w30Td2Gj2Mznyw94vSP+2jJYkOVNbD1k= +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/sethvargo/go-retry v0.3.0 h1:EEt31A35QhrcRZtrYFDTBg91cqZVnFL2navjDrah2SE= +github.com/sethvargo/go-retry v0.3.0/go.mod h1:mNX17F0C/HguQMyMyJxcnU471gOZGxCLyYaFyAZraas= +github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= +github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= +go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= +golang.org/x/exp v0.0.0-20250305212735-054e65f0b394 h1:nDVHiLt8aIbd/VzvPWN6kSOPE7+F/fNFDSXLVYkE/Iw= +golang.org/x/exp v0.0.0-20250305212735-054e65f0b394/go.mod h1:sIifuuw/Yco/y6yb6+bDNfyeQ/MdPUy/hKEMYQV17cM= +golang.org/x/mod v0.24.0 h1:ZfthKaKaT4NrhGVZHO1/WDTwGES4De8KtWO0SIbNJMU= +golang.org/x/mod v0.24.0/go.mod h1:IXM97Txy2VM4PJ3gI61r1YEk/gAj6zAHN3AdZt6S9Ww= +golang.org/x/sync v0.12.0 h1:MHc5BpPuC30uJk597Ri8TV3CNZcTLu6B6z4lJy+g6Jw= +golang.org/x/sync v0.12.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik= +golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= +golang.org/x/tools v0.31.0 h1:0EedkvKDbh+qistFTd0Bcwe/YLh4vHwWEkiI0toFIBU= +golang.org/x/tools v0.31.0/go.mod h1:naFTU+Cev749tSJRXJlna0T3WxKvb1kWEx15xA4SdmQ= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +modernc.org/cc/v4 v4.24.4 h1:TFkx1s6dCkQpd6dKurBNmpo+G8Zl4Sq/ztJ+2+DEsh0= +modernc.org/cc/v4 v4.24.4/go.mod h1:uVtb5OGqUKpoLWhqwNQo/8LwvoiEBLvZXIQ/SmO6mL0= +modernc.org/ccgo/v4 v4.23.16 h1:Z2N+kk38b7SfySC1ZkpGLN2vthNJP1+ZzGZIlH7uBxo= +modernc.org/ccgo/v4 v4.23.16/go.mod h1:nNma8goMTY7aQZQNTyN9AIoJfxav4nvTnvKThAeMDdo= +modernc.org/fileutil v1.3.0 h1:gQ5SIzK3H9kdfai/5x41oQiKValumqNTDXMvKo62HvE= +modernc.org/fileutil v1.3.0/go.mod h1:XatxS8fZi3pS8/hKG2GH/ArUogfxjpEKs3Ku3aK4JyQ= +modernc.org/gc/v2 v2.6.3 h1:aJVhcqAte49LF+mGveZ5KPlsp4tdGdAOT4sipJXADjw= +modernc.org/gc/v2 v2.6.3/go.mod h1:YgIahr1ypgfe7chRuJi2gD7DBQiKSLMPgBQe9oIiito= +modernc.org/libc v1.61.13 h1:3LRd6ZO1ezsFiX1y+bHd1ipyEHIJKvuprv0sLTBwLW8= +modernc.org/libc v1.61.13/go.mod h1:8F/uJWL/3nNil0Lgt1Dpz+GgkApWh04N3el3hxJcA6E= +modernc.org/mathutil v1.7.1 h1:GCZVGXdaN8gTqB1Mf/usp1Y/hSqgI2vAGGP4jZMCxOU= +modernc.org/mathutil v1.7.1/go.mod h1:4p5IwJITfppl0G4sUEDtCr4DthTaT47/N3aT6MhfgJg= +modernc.org/memory v1.9.1 h1:V/Z1solwAVmMW1yttq3nDdZPJqV1rM05Ccq6KMSZ34g= +modernc.org/memory v1.9.1/go.mod h1:/JP4VbVC+K5sU2wZi9bHoq2MAkCnrt2r98UGeSK7Mjw= +modernc.org/opt v0.1.4 h1:2kNGMRiUjrp4LcaPuLY2PzUfqM/w9N23quVwhKt5Qm8= +modernc.org/opt v0.1.4/go.mod h1:03fq9lsNfvkYSfxrfUhZCWPk1lm4cq4N+Bh//bEtgns= +modernc.org/sortutil v1.2.1 h1:+xyoGf15mM3NMlPDnFqrteY07klSFxLElE2PVuWIJ7w= +modernc.org/sortutil v1.2.1/go.mod h1:7ZI3a3REbai7gzCLcotuw9AC4VZVpYMjDzETGsSMqJE= +modernc.org/sqlite v1.36.2 h1:vjcSazuoFve9Wm0IVNHgmJECoOXLZM1KfMXbcX2axHA= +modernc.org/sqlite v1.36.2/go.mod h1:ADySlx7K4FdY5MaJcEv86hTJ0PjedAloTUuif0YS3ws= +modernc.org/strutil v1.2.1 h1:UneZBkQA+DX2Rp35KcM69cSsNES9ly8mQWD71HKlOA0= +modernc.org/strutil v1.2.1/go.mod h1:EHkiggD70koQxjVdSBM3JKM7k6L0FbGE5eymy9i3B9A= +modernc.org/token v1.1.0 h1:Xl7Ap9dKaEs5kLoOQeQmPWevfnk/DM5qcLcYlA8ys6Y= +modernc.org/token v1.1.0/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM= diff --git a/mise.toml b/mise.toml index 12ae3b2..837ff07 100644 --- a/mise.toml +++ b/mise.toml @@ -1,5 +1,20 @@ [tools] node = "23.11.0" "aqua:pnpm/pnpm" = "10.10.0" -erlang = "27" -elixir = "1.18.3" + +[tasks."db:up"] +dir = "{{cwd}}/backend" +description = "Run DB migration" +run = [ + 'goose sqlite3 ./ligmotes.db up -dir migrations' +] + +[tasks."db:new"] +dir = "{{cwd}}/backend" +description = "Create New DB migration" +run = [ + 'goose sqlite3 ./ligmotes.db create {{arg(name="migration_name")}} -dir migrations' +] + +[env] +SERV_ADDR = ":9876"