Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add CORS configuration #100

Merged
merged 3 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ build-css:
@npx tailwindcss -i ./assets/app.css -o ./public/output.css --minify

test:
@go test -v ./...
@go test -p 1 -v ./...
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ require (
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/go-chi/chi/v5 v5.0.11 // indirect
github.com/go-chi/cors v1.2.1 // indirect
github.com/gofiber/template v1.8.2 // indirect
github.com/gofiber/utils v1.1.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4
github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
github.com/go-chi/chi/v5 v5.0.11 h1:BnpYbFZ3T3S1WMpD79r7R5ThWX40TaFB7L31Y8xqSwA=
github.com/go-chi/chi/v5 v5.0.11/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/go-chi/cors v1.2.1 h1:xEC8UT3Rlp2QuWNEr4Fs/c2EAGVKBwy/1vHx3bppil4=
github.com/go-chi/cors v1.2.1/go.mod h1:sSbTewc+6wYHBBCW7ytsFSn836hqM7JxpglAy2Vzc58=
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/gofiber/fiber/v2 v2.51.0 h1:JNACcZy5e2tGApWB2QrRpenTWn0fq0hkFm6k0C86gKQ=
Expand Down
6 changes: 6 additions & 0 deletions internal/store/sessions_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package store

import (
"os"
"testing"
"visio/internal/database"
)

func SessionsMain(m *testing.M) {
code := m.Run()
os.Exit(code)
}

func TestSession(t *testing.T) {
sessionManager := database.NewSessionManager()
t.Run("Create session", func(t *testing.T) {
Expand Down
3 changes: 1 addition & 2 deletions internal/store/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ package store
import (
"database/sql"
"fmt"
"visio/internal/types"

"github.com/jmoiron/sqlx"
"visio/internal/types"
)

type Users struct {
Expand Down
56 changes: 36 additions & 20 deletions internal/store/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@ package store
import (
"errors"
"fmt"
"log"
"os"
"testing"
"time"
"visio/internal/types"

"github.com/jmoiron/sqlx"
"github.com/lib/pq"
_ "github.com/lib/pq"
"github.com/ory/dockertest/v3"
"github.com/stretchr/testify/require"
"log"
"os"
"testing"
"time"
"visio/internal/types"
)

const (
Expand Down Expand Up @@ -82,17 +81,31 @@ func TestMain(m *testing.M) {
os.Exit(code)
}

// TODO: As an improvement, we might consider to use a migration folder
//
// github.com/golang-migrate/migrate
func migrateTestDB(db *sqlx.DB) error {
q := `
create table if not exists users (
id text not null primary key,
email text not null unique,
password_hash text not null,
signup_date text not null
);`
id text not null primary key,
email text not null unique,
password_hash text not null,
signup_date text not null
);

create table if not exists keys (
id text not null primary key,
user_id text not null references users(id) on delete cascade,
prefix text not null unique,
key_hash text not null,
creation_date text not null
);

create table if not exists faces (
id text not null primary key,
label text not null,
user_id text not null references users(id) on delete cascade,
descriptor text not null,
unique (label, user_id)
);
`
_, err := db.Exec(q)
return err
}
Expand Down Expand Up @@ -122,7 +135,10 @@ func TestUsers_Insert(t *testing.T) {
// https://www.postgresql.org/docs/current/errcodes-appendix.html
require.Equal(t, "unique_violation", pqErr.Code.Name())

testDB.Exec("TRUNCATE users;")
testDB.Exec("delete from users cascade;")
if err != nil {
fmt.Println(err.Error())
}
})

t.Run("success", func(t *testing.T) {
Expand All @@ -142,7 +158,7 @@ func TestUsers_Insert(t *testing.T) {
})
require.NoError(t, err)

testDB.Exec("TRUNCATE users;")
testDB.Exec("delete from users cascade;")
})
}

Expand All @@ -154,7 +170,7 @@ func TestUsers_GetById(t *testing.T) {
require.Nil(t, user)
require.Equal(t, err, types.ErrUserNotFound)

testDB.Exec("TRUNCATE users;")
testDB.Exec("delete from users cascade;")
})

t.Run("user exists", func(t *testing.T) {
Expand All @@ -174,7 +190,7 @@ func TestUsers_GetById(t *testing.T) {
require.Equal(t, user.SignupDate, existingUser.SignupDate)
require.NoError(t, err)

testDB.Exec("TRUNCATE users;")
testDB.Exec("delete from users cascade;")
})
}

Expand All @@ -186,7 +202,7 @@ func TestUsers_GetByEmail(t *testing.T) {
require.Nil(t, user)
require.Equal(t, err, types.ErrUserNotFound)

testDB.Exec("TRUNCATE users;")
testDB.Exec("delete from users cascade;")
})

t.Run("user exists", func(t *testing.T) {
Expand All @@ -206,6 +222,6 @@ func TestUsers_GetByEmail(t *testing.T) {
require.Equal(t, user.SignupDate, existingUser.SignupDate)
require.NoError(t, err)

testDB.Exec("TRUNCATE users;")
testDB.Exec("delete from users cascade;")
})
}
11 changes: 10 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/Kagami/go-face"
"github.com/go-chi/chi/v5"
chiMiddleware "github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/cors"
"github.com/joho/godotenv"
"log/slog"
"net/http"
Expand Down Expand Up @@ -49,6 +50,14 @@ func main() {
uploadMiddleware := middlewares.NewUploadMiddleware(appLogger)

r := chi.NewRouter()
r.Use(cors.Handler(cors.Options{
AllowedOrigins: []string{"https://*", "http://*"},
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
ExposedHeaders: []string{"Link"},
AllowCredentials: false,
MaxAge: 300,
}))
r.Use(chiMiddleware.RequestID)

r.Get("/public/output.css", func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -72,7 +81,7 @@ func main() {
r.Post("/auth", authHandler.Authenticate)
})

r.With(authMiddleware.CookieAuth).Delete("/keys", appHandler.RevokeKey)
r.With(authMiddleware.CookieAuth).Delete("/keys", appHandler.RevokeKey)

r.Route("/faces", func(r chi.Router) {
r.With(authMiddleware.KeyAuth).With(uploadMiddleware.HandleUploads(1)).Post("/", faceHandler.SaveFace)
Expand Down
4 changes: 2 additions & 2 deletions schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ create table if not exists users (

create table if not exists keys (
id text not null primary key,
user_id text not null references users(id),
user_id text not null references users(id) on delete cascade,
prefix text not null unique,
key_hash text not null,
creation_date text not null
Expand All @@ -16,7 +16,7 @@ create table if not exists keys (
create table if not exists faces (
id text not null primary key,
label text not null,
user_id text not null references users(id),
user_id text not null references users(id) on delete cascade,
descriptor text not null,
unique (label, user_id)
);
Loading