From 64bd10915b231cd18f32c408feeec4dbba04dafb Mon Sep 17 00:00:00 2001 From: noe Date: Wed, 26 Mar 2025 01:03:26 -0700 Subject: [PATCH] readme, nix fixes --- .env.example | 7 ++++ .gitignore | 3 +- README.md | 55 +++++++++++++++++++++++++++++++ discord/api.go | 4 +-- discord/guildservice.go | 7 ++++ discord/guildservice_mock.go | 5 +++ interactions/interactions.go | 12 ++----- interactions/interactions_test.go | 7 +--- main.go | 10 +++++- result | 1 - roleypoly/fiber.go | 12 +++++-- utils/strings_test.go | 2 +- 12 files changed, 102 insertions(+), 23 deletions(-) create mode 100644 README.md delete mode 120000 result diff --git a/.env.example b/.env.example index e69de29..9681e43 100644 --- a/.env.example +++ b/.env.example @@ -0,0 +1,7 @@ +DISCORD_CLIENT_ID=aa +DISCORD_CLIENT_SECRET=bb +DISCORD_PUBLIC_KEY=cc +DISCORD_BOT_TOKEN=dd + +LISTEN_ADDR=:8169 +STORAGE_DIR=./.storage \ No newline at end of file diff --git a/.gitignore b/.gitignore index 98de019..e44b9f1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .direnv .env -.storage \ No newline at end of file +.storage +result \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..4d9bcd7 --- /dev/null +++ b/README.md @@ -0,0 +1,55 @@ +# Roleypoly v4 + +yeah 4 of em.. + +## Developing + +use `nix shell` or `nix-shell` or maybe just run `go run .` or somethin its ok + +please run `just precommit` before committing thx, it fixes weird build issues + +## Deploying + +roleypoly can be deployed as a docker container (built with nix) or nix package right now. more options to come (like prebuilt binaries) + +```sh +nix build .#container +docker load -i result +docker run -it --rm -p 8169:8169 localhost/roleypoly/roleypoly +``` + +or like if in irl nixos + +```nix +#== flake.nix +{ + inputs = { + roleypoly.url = "git+https://git.sapphic.engineer/roleypoly/v4"; + }; +} +``` + +and want to use docker (its ok) + +```nix +#== roleypoly.nix +{ inputs, pkgs, ... }: { + virtualisation.oci-containers.containers.roleypoly = { + image = "roleypoly/roleypoly:latest"; + imageFile = inputs.roleypoly.packages.${pkgs.system}.container; + ports = [ "8169:8169" ]; + # probably include environment and stuff too + }; +} +``` + +or use `inputs.roleypoly.packages.${pkgs.system}.roleypoly` for the actual package for like a systemd thing + +<33 + +## thanks + +roleypoly has been a journey and a pleasure to make. thanks to everyone who has used it over the years. there will be no major updates after v4. + +as always, with love +-- noe, aki, and aurelia! diff --git a/discord/api.go b/discord/api.go index 1352350..0716fe7 100644 --- a/discord/api.go +++ b/discord/api.go @@ -21,8 +21,8 @@ type DiscordClient struct { ClientSecret string } -func NewDiscordClient(clientID, clientSecret, botToken string) DiscordClient { - return DiscordClient{ +func NewDiscordClient(clientID, clientSecret, botToken string) *DiscordClient { + return &DiscordClient{ ClientID: clientID, ClientSecret: clientSecret, BotToken: botToken, diff --git a/discord/guildservice.go b/discord/guildservice.go index c22c13f..79278ac 100644 --- a/discord/guildservice.go +++ b/discord/guildservice.go @@ -9,10 +9,17 @@ type GuildService struct { client IDiscordClient } +func NewGuildService(client IDiscordClient) *GuildService { + return &GuildService{ + client: client, + } +} + func (gs *GuildService) Client() IDiscordClient { return gs.client } func (gs *GuildService) GetGuild(guildID string) (IGuild, error) { // gs.client + return nil, nil } diff --git a/discord/guildservice_mock.go b/discord/guildservice_mock.go index eb2857a..b126c3a 100644 --- a/discord/guildservice_mock.go +++ b/discord/guildservice_mock.go @@ -17,3 +17,8 @@ func (gs *GuildServiceMock) GetGuild(guildID string) (IGuild, error) { return args.Get(0).(IGuild), args.Error(1) } + +func (gs *GuildServiceMock) Client() IDiscordClient { + args := gs.Called() + return args.Get(0).(IDiscordClient) +} diff --git a/interactions/interactions.go b/interactions/interactions.go index 20fd7d2..f4b2ccc 100644 --- a/interactions/interactions.go +++ b/interactions/interactions.go @@ -4,7 +4,6 @@ import ( "crypto/ed25519" "log" "net/http" - "os" "github.com/gofiber/fiber/v3" @@ -22,10 +21,6 @@ type Interactions struct { } func NewInteractions(publicKey string, guildsService discord.IGuildService) *Interactions { - if publicKey == "" { - publicKey = os.Getenv("DISCORD_PUBLIC_KEY") - } - return &Interactions{ PublicKey: ed25519.PublicKey(publicKey), Guilds: guildsService, @@ -73,7 +68,6 @@ func (i *Interactions) PostHandler(c fiber.Ctx) error { return types.NewAPIError(http.StatusNotImplemented, "not implemented").Send(c) } -func (i *Interactions) Respond(ix Interaction, ir InteractionResponse) error { - // TODO: make request to /webhooks/appid/{ix.Token} - return nil -} +// func (i *Interactions) Respond(ix Interaction, ir InteractionResponse) error { +// i.Guilds +// } diff --git a/interactions/interactions_test.go b/interactions/interactions_test.go index 1d01916..e698ce8 100644 --- a/interactions/interactions_test.go +++ b/interactions/interactions_test.go @@ -18,15 +18,10 @@ import ( func TestNewInteractions(t *testing.T) { pub, _, _ := ed25519.GenerateKey(nil) key := hex.EncodeToString(pub) - t.Setenv("DISCORD_PUBLIC_KEY", key) gsm := &discord.GuildServiceMock{} - i := interactions.NewInteractions("", gsm) - assert.True(t, i.OK, "interactions OK") - assert.Equal(t, string(i.PublicKey), key, "public key fetched from environment") - - i = interactions.NewInteractions(key, gsm) + i := interactions.NewInteractions(key, gsm) assert.True(t, i.OK, "interactions OK") assert.Equal(t, string(i.PublicKey), key, "public key accepted from args") } diff --git a/main.go b/main.go index 234709c..096ac8f 100644 --- a/main.go +++ b/main.go @@ -4,12 +4,20 @@ import ( "log" "os" + "git.sapphic.engineer/roleypoly/v4/discord" "git.sapphic.engineer/roleypoly/v4/roleypoly" ) func main() { app := roleypoly.CreateFiberApp() - roleypoly.SetupRoutes(app) + + clientID := os.Getenv("DISCORD_CLIENT_ID") + clientSecret := os.Getenv("DISCORD_CLIENT_SECRET") + botToken := os.Getenv("DISCORD_BOT_TOKEN") + dc := discord.NewDiscordClient(clientID, clientSecret, botToken) + + publicKey := os.Getenv("DISCORD_PUBLIC_KEY") + roleypoly.SetupControllers(app, dc, publicKey) listenAddr := os.Getenv("LISTEN_ADDR") if listenAddr == "" { diff --git a/result b/result deleted file mode 120000 index f22da09..0000000 --- a/result +++ /dev/null @@ -1 +0,0 @@ -/nix/store/pfv1lbrn6cawjj523cba1c56d835q6sd-docker-image-roleypoly.tar.gz \ No newline at end of file diff --git a/roleypoly/fiber.go b/roleypoly/fiber.go index 9671e43..7617b48 100644 --- a/roleypoly/fiber.go +++ b/roleypoly/fiber.go @@ -8,6 +8,8 @@ import ( "github.com/gofiber/fiber/v3/middleware/session" "github.com/gofiber/template/html/v2" + "git.sapphic.engineer/roleypoly/v4/discord" + "git.sapphic.engineer/roleypoly/v4/interactions" "git.sapphic.engineer/roleypoly/v4/templates" "git.sapphic.engineer/roleypoly/v4/testing" ) @@ -27,8 +29,14 @@ func CreateFiberApp() *fiber.App { return app } -func SetupRoutes(app *fiber.App) { - (&testing.TestingController{}).Routes(app.Group("/testing")) +func SetupControllers(app *fiber.App, dc discord.IDiscordClient, publicKey string) { + gs := discord.NewGuildService(dc) + + (&testing.TestingController{ + Guilds: gs, + }).Routes(app.Group("/testing")) + + interactions.NewInteractions(publicKey, gs).Routes(app.Group("/interactions")) } // func getStorageDirectory() string { diff --git a/utils/strings_test.go b/utils/strings_test.go index 552a520..6b73c58 100644 --- a/utils/strings_test.go +++ b/utils/strings_test.go @@ -13,5 +13,5 @@ func TestHeadTitle(t *testing.T) { } func TestJ(t *testing.T) { - assert.Equal(t, "a/b/c", utils.J("a", "b", "c")) + assert.Equal(t, "/a/b/c", utils.J("a", "b", "c")) }