Skip to content

Commit

Permalink
feat(web): added new endpoint GET /api/v1/audio - download audio by id
Browse files Browse the repository at this point in the history
  • Loading branch information
Wittano committed Apr 11, 2024
1 parent 658aa8d commit 98e38d4
Show file tree
Hide file tree
Showing 20 changed files with 258 additions and 187 deletions.
4 changes: 2 additions & 2 deletions bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"github.com/google/uuid"
"github.com/wittano/komputer/bot/internal/command"
"github.com/wittano/komputer/bot/internal/config"
"github.com/wittano/komputer/bot/internal/db"
"github.com/wittano/komputer/bot/internal/joke"
"github.com/wittano/komputer/bot/internal/voice"
"github.com/wittano/komputer/db"
"log/slog"
"time"
)
Expand Down Expand Up @@ -161,7 +161,7 @@ func NewDiscordBot(ctx context.Context) (*DiscordBot, error) {
bot.AddHandler(vcHander.HandleVoiceChannelUpdate)

// Register slash commands
database := db.NewMongodbDatabase(ctx)
database := db.Mongodb(ctx)
getServices := createJokeGetServices(ctx, database)
commands := createCommands(ctx, getServices, spockVoiceChns, guildVoiceChats)
for _, c := range commands {
Expand Down
3 changes: 1 addition & 2 deletions bot/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ require (
github.com/google/uuid v1.6.0
github.com/jarcoal/httpmock v1.3.1
github.com/joho/godotenv v1.5.1
github.com/testcontainers/testcontainers-go v0.30.0
github.com/testcontainers/testcontainers-go/modules/mongodb v0.29.1
go.mongodb.org/mongo-driver v1.14.0
layeh.com/gopus v0.0.0-20210501142526-1ee02d434e32
)
Expand Down Expand Up @@ -51,6 +49,7 @@ require (
github.com/shirou/gopsutil/v3 v3.23.12 // indirect
github.com/shoenig/go-m1cpu v0.1.6 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/testcontainers/testcontainers-go v0.30.0 // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
Expand Down
12 changes: 0 additions & 12 deletions bot/internal/db/types.go

This file was deleted.

7 changes: 3 additions & 4 deletions bot/internal/joke/joke.go → bot/internal/joke/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"errors"
"fmt"
"github.com/wittano/komputer/bot/internal/db"
"github.com/wittano/komputer/db"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
Expand All @@ -14,7 +14,6 @@ import (
)

const (
DatabaseName = "komputer"
collectionName = "jokes"
GuildIDKey = "guildID"
)
Expand Down Expand Up @@ -87,7 +86,7 @@ func (d DatabaseJokeService) Add(ctx context.Context, joke Joke) (string, error)
return "", err
}

res, err := mongodb.Database(DatabaseName).Collection(collectionName).InsertOne(ctx, joke)
res, err := mongodb.Database(db.DatabaseName).Collection(collectionName).InsertOne(ctx, joke)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -156,7 +155,7 @@ func (d DatabaseJokeService) Get(ctx context.Context, search SearchParameters) (
}

// SearchParameters
res, err := mongodb.Database(DatabaseName).Collection(collectionName).Aggregate(ctx, pipeline)
res, err := mongodb.Database(db.DatabaseName).Collection(collectionName).Aggregate(ctx, pipeline)
if err != nil {
return Joke{}, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package joke

import (
"context"
"github.com/wittano/komputer/db"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
Expand Down Expand Up @@ -41,7 +42,7 @@ func createMTest(t *testing.T) *mtest.T {
return mtest.New(t, mtest.NewOptions().
ClientType(mtest.Mock).
CollectionName(collectionName).
DatabaseName(DatabaseName))
DatabaseName(db.DatabaseName))
}

func TestJokeService_Add(t *testing.T) {
Expand Down Expand Up @@ -102,7 +103,7 @@ func TestJokeService_SearchButNotingFound(t *testing.T) {
mt := createMTest(t)

mt.Run("get new joke, but nothing was found", func(t *mtest.T) {
t.AddMockResponses(mtest.CreateCursorResponse(1, DatabaseName+"."+collectionName, mtest.FirstBatch, bson.D{}))
t.AddMockResponses(mtest.CreateCursorResponse(1, db.DatabaseName+"."+collectionName, mtest.FirstBatch, bson.D{}))

ctx := context.Background()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,47 +1,39 @@
//go:build testcontainers

package db
package joke

import (
"context"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/modules/mongodb"
"github.com/wittano/komputer/bot/internal/joke"
"github.com/wittano/komputer/db"
"go.mongodb.org/mongo-driver/bson/primitive"
"os"
"testing"
)

var testJoke = joke.Joke{
Question: "testQuestion",
Answer: "testAnswer",
Type: joke.Single,
Category: joke.Any,
GuildID: "",
}

type jokeSerchArgs struct {
name string
withID bool
search joke.SearchParameters
joke joke.Joke
search SearchParameters
joke Joke
}

func createJokeService(ctx context.Context) (db *mongodb.MongoDBContainer, service joke.DatabaseJokeService, err error) {
db, err = mongodb.RunContainer(ctx, testcontainers.WithEnv(map[string]string{
"MONGO_INITDB_DATABASE": joke.DatabaseName,
func createJokeService(ctx context.Context) (container *mongodb.MongoDBContainer, service DatabaseJokeService, err error) {
container, err = mongodb.RunContainer(ctx, testcontainers.WithEnv(map[string]string{
"MONGO_INITDB_DATABASE": db.DatabaseName,
}))
if err != nil {
return
}

mongodbURI, err := db.ConnectionString(ctx)
mongodbURI, err := container.ConnectionString(ctx)
if err != nil {
return
}
os.Setenv(mongodbURIKey, mongodbURI)
os.Setenv(db.MongodbURIKey, mongodbURI)

service = joke.NewDatabaseJokeService(NewMongodbDatabase(ctx))
service = NewDatabaseJokeService(db.Mongodb(ctx))

return
}
Expand Down Expand Up @@ -77,93 +69,93 @@ func TestJokeService_Get(t *testing.T) {
args := []jokeSerchArgs{
{
name: "find random joke",
search: joke.SearchParameters{},
search: SearchParameters{},
joke: testJoke,
},
{
name: "find by type",
search: joke.SearchParameters{Type: joke.TwoPart},
joke: joke.Joke{
Type: joke.TwoPart,
search: SearchParameters{Type: TwoPart},
joke: Joke{
Type: TwoPart,
Question: "testTwoPartQuestion",
Answer: "testTwoPartAnswer",
Category: joke.DARK,
Category: DARK,
GuildID: testGuildID,
},
},
{
name: "find by category",
search: joke.SearchParameters{Category: joke.MISC},
joke: joke.Joke{
Type: joke.Single,
search: SearchParameters{Category: MISC},
joke: Joke{
Type: Single,
Question: "testQuestion",
Answer: "testAnswer",
Category: joke.MISC,
Category: MISC,
GuildID: testGuildID,
},
},
{
name: "find by id",
withID: true,
search: joke.SearchParameters{}, // I added ID after adding entity to database
joke: joke.Joke{
Type: joke.Single,
search: SearchParameters{}, // I added ID after adding entity to database
joke: Joke{
Type: Single,
Question: "testQuestion",
Answer: "testAnswer",
Category: joke.PROGRAMMING,
Category: PROGRAMMING,
GuildID: testGuildID,
},
},
{
name: "find by id and category",
withID: true,
search: joke.SearchParameters{Category: joke.MISC},
joke: joke.Joke{
Type: joke.Single,
search: SearchParameters{Category: MISC},
joke: Joke{
Type: Single,
Question: "testQuestion",
Answer: "testAnswer",
Category: joke.MISC,
Category: MISC,
GuildID: testGuildID,
},
},
{
name: "find by id and type",
withID: true,
search: joke.SearchParameters{Type: joke.TwoPart},
joke: joke.Joke{
Type: joke.TwoPart,
search: SearchParameters{Type: TwoPart},
joke: Joke{
Type: TwoPart,
Question: "testTwoPartQuestion",
Answer: "testTwoPartAnswer",
Category: joke.DARK,
Category: DARK,
GuildID: testGuildID,
},
},
{
name: "find by type and category",
search: joke.SearchParameters{Type: joke.TwoPart, Category: joke.YOMAMA},
joke: joke.Joke{
Type: joke.TwoPart,
search: SearchParameters{Type: TwoPart, Category: YOMAMA},
joke: Joke{
Type: TwoPart,
Question: "testTwoPartQuestion",
Answer: "testTwoPartAnswer",
Category: joke.YOMAMA,
Category: YOMAMA,
GuildID: testGuildID,
},
},
{
name: "find by id, category and type",
withID: true,
search: joke.SearchParameters{Type: joke.TwoPart, Category: joke.DARK},
joke: joke.Joke{
Type: joke.TwoPart,
search: SearchParameters{Type: TwoPart, Category: DARK},
joke: Joke{
Type: TwoPart,
Question: "testTwoPartQuestion",
Answer: "testTwoPartAnswer",
Category: joke.DARK,
Category: DARK,
GuildID: testGuildID,
},
},
}

ctx := context.WithValue(context.Background(), joke.GuildIDKey, testGuildID)
ctx := context.WithValue(context.Background(), GuildIDKey, testGuildID)
db, service, err := createJokeService(ctx)
if err != nil {
t.Fatal(err)
Expand Down
2 changes: 1 addition & 1 deletion bot/internal/joke/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package joke

import (
"context"
"github.com/wittano/komputer/bot/internal/db"
"github.com/wittano/komputer/db"
"net/http"
"time"
)
Expand Down
9 changes: 4 additions & 5 deletions cmd/web/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ import (
const defaultConfigPath = "config.yml"

func main() {
configPath := flag.String("config", defaultConfigPath, "Path to web console configuration file")
configPath := flag.String("config", defaultConfigPath, "Path to web console configuration audio")
flag.Parse()

s, err := web.NewWebConsoleServer(*configPath)
e, err := web.NewWebConsoleServer(*configPath)
if err != nil {
log.Fatal(err)
}
defer s.Close()
defer e.Close()

log.Println("Server listening on 8080")
log.Fatal(s.ListenAndServe())
e.Logger.Fatal(e.Start(":8080"))
}
14 changes: 10 additions & 4 deletions bot/internal/db/mongo.go → db/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"time"
)

const mongodbURIKey = "MONGODB_URI"
const MongodbURIKey = "MONGODB_URI"

type MongodbDatabase struct {
uri string
Expand Down Expand Up @@ -61,14 +61,20 @@ func (m *MongodbDatabase) Client(ctx context.Context) (*mongo.Client, error) {
return m.db, nil
}

func NewMongodbDatabase(ctx context.Context) (db *MongodbDatabase) {
var db *MongodbDatabase

func Mongodb(ctx context.Context) *MongodbDatabase {
if db != nil {
return db
}

db = new(MongodbDatabase)

if uri, ok := os.LookupEnv(mongodbURIKey); ok {
if uri, ok := os.LookupEnv(MongodbURIKey); ok {
db.uri = uri
}

db.ctx = ctx

return
return db
}
12 changes: 6 additions & 6 deletions bot/internal/db/mongo_test.go → db/mongo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ import (
const testUri = "mongodb+srv://username:password@server/database"

func TestNewMongodbDatabase(t *testing.T) {
os.Setenv(mongodbURIKey, testUri)
os.Setenv(MongodbURIKey, testUri)

if db := NewMongodbDatabase(context.Background()); db.uri != testUri {
if db := Mongodb(context.Background()); db.uri != testUri {
t.Fatalf("Invalid URI. Expected: '%s', Result: '%s'", testUri, db.uri)
}
}

func TestNewMongodbDatabaseButMongodbURIMissing(t *testing.T) {
if db := NewMongodbDatabase(context.Background()); db.uri != testUri {
if db := Mongodb(context.Background()); db.uri != testUri {
t.Fatalf("Invalid URI. Expected: '%s', Result: '%s'", testUri, db.uri)
}
}

func TestMongodbDatabase_ClientButURIMissing(t *testing.T) {
ctx := context.Background()

db := NewMongodbDatabase(ctx)
db := Mongodb(ctx)

if _, err := db.Client(ctx); err == nil {
t.Fatal("mongodb connection was established!")
Expand All @@ -34,9 +34,9 @@ func TestMongodbDatabase_ClientButURIMissing(t *testing.T) {

func TestMongodbDatabase_ClientButConnectionFailed(t *testing.T) {
ctx := context.Background()
os.Setenv(mongodbURIKey, testUri)
os.Setenv(MongodbURIKey, testUri)

db := NewMongodbDatabase(ctx)
db := Mongodb(ctx)

if _, err := db.Client(ctx); err == nil {
t.Fatal("mongodb connection was established!")
Expand Down
Loading

0 comments on commit 98e38d4

Please sign in to comment.