Skip to content

Commit

Permalink
Merge pull request #64 from dedis/improve-registration
Browse files Browse the repository at this point in the history
tidy up server
  • Loading branch information
jbsv authored Jan 9, 2024
2 parents fda1697 + 6cbf660 commit 140661c
Show file tree
Hide file tree
Showing 11 changed files with 311 additions and 312 deletions.
4 changes: 3 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -234,4 +234,6 @@ issues:
text: "unexported-return:"
- linters:
- govet
text: "shadow: declaration of \"err\" shadows declaration"
text: "shadow: declaration of \"err\" shadows declaration"
- path: "registration/registration.go"
linters: [lll]
2 changes: 1 addition & 1 deletion server/registration/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

type Config struct {
MongoDbUri string `mapstructure:"mongodb_uri"`
MongodbURI string `mapstructure:"mongodb_uri"`
UserName string `mapstructure:"user_name"`
UserPassword string `mapstructure:"user_password"`
AdminName string `mapstructure:"admin_name"`
Expand Down
8 changes: 4 additions & 4 deletions server/registration/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ type Database interface {
// Create creates a new document in the database
// it takes the document as an argument
// and returns the document ID or an error
Create(*registry.RegistrationData) (*registry.RegistrationId, error)
Create(*registry.RegistrationData) (*registry.RegistrationID, error)

// Read retrieves a document from the database
// it takes the document ID and the hash as arguments
// and returns the document or an error
Read(registry.RegistrationId, []byte) (*registry.RegistrationData, error)
Read(registry.RegistrationID, []byte) (*registry.RegistrationData, error)

// Update updates a document in the database
// it takes the document ID and the updated document as an argument
// and returns nil or an error
Update(registry.RegistrationId, *registry.RegistrationData) error
Update(registry.RegistrationID, *registry.RegistrationData) error

// Delete deletes a document from the database
// it takes the document ID and the hash as arguments
// and returns nil or an error
Delete(registry.RegistrationId, []byte) error
Delete(registry.RegistrationID, []byte) error

// Disconnect disconnects from the database
Disconnect() error
Expand Down
70 changes: 0 additions & 70 deletions server/registration/database/mongodb/admindb.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,30 @@ import (
"go.mongodb.org/mongo-driver/mongo/options"
)

type UserDbAccess struct {
type dbAccess struct {
client *mongo.Client
}

// NewUserDbAccess creates a new user access to the DB
func NewUserDbAccess() (database.Database, error) {
// NewDBAccess creates a new user access to the DB
func NewDBAccess() (database.Database, error) {
// Initialize userDb DB access
credentials := options.Credential{
AuthSource: "admin",
AuthMechanism: "SCRAM-SHA-1",
Username: config.AppConfig.UserName,
Password: config.AppConfig.UserPassword,
}
userOpts := options.Client().ApplyURI(config.AppConfig.MongoDbUri).SetAuth(credentials)
userOpts := options.Client().ApplyURI(config.AppConfig.MongodbURI).SetAuth(credentials)
client, err := mongo.Connect(context.TODO(), userOpts)
if err != nil {
return UserDbAccess{nil}, err
return dbAccess{nil}, err
}

return UserDbAccess{client}, nil
return dbAccess{client}, nil
}

// Create creates a new document in the DB
func (u UserDbAccess) Create(data *registry.RegistrationData) (*registry.RegistrationId, error) {
func (d dbAccess) Create(data *registry.RegistrationData) (*registry.RegistrationID, error) {
doc := Document{
Name: data.Name,
Passport: data.Passport,
Expand All @@ -46,12 +46,10 @@ func (u UserDbAccess) Create(data *registry.RegistrationData) (*registry.Registr
Registered: false,
}

db := u.client.Database("registry")
db := d.client.Database("registry")
c := db.Collection("documents")
result, err := c.InsertOne(context.Background(), doc)

// result, err := u.client.Database("registry").Collection("documents").InsertOne(context.Background(), doc)

if err != nil {
return nil, err
}
Expand All @@ -62,11 +60,11 @@ func (u UserDbAccess) Create(data *registry.RegistrationData) (*registry.Registr
return nil, err
}

registrationId := registry.RegistrationId{
Id: id,
regID := registry.RegistrationID{
ID: id,
}

return &registrationId, nil
return &regID, nil

}

Expand All @@ -75,14 +73,14 @@ func (u UserDbAccess) Create(data *registry.RegistrationData) (*registry.Registr

// Read reads a document from the DB
// it is used to get the registered value of a document
func (u UserDbAccess) Read(docId registry.RegistrationId, hash []byte) (
func (d dbAccess) Read(id registry.RegistrationID, hash []byte) (
*registry.RegistrationData,
error,
) {
var doc Document

err := u.client.Database("registration").Collection("documents").FindOne(context.Background(),
docId).Decode(&doc)
err := d.client.Database("registration").Collection("documents").FindOne(context.Background(),
id).Decode(&doc)

if err != nil {
return nil, err
Expand All @@ -105,14 +103,14 @@ func (u UserDbAccess) Read(docId registry.RegistrationId, hash []byte) (
}

// Update updates a document in the DB
func (u UserDbAccess) Update(
docId registry.RegistrationId,
func (d dbAccess) Update(
id registry.RegistrationID,
reg *registry.RegistrationData,
) error {
var doc Document

err := u.client.Database("registration").Collection("documents").FindOne(context.Background(),
docId).Decode(&doc)
err := d.client.Database("registration").Collection("documents").FindOne(context.Background(),
id).Decode(&doc)
if err != nil {
return err
}
Expand All @@ -121,14 +119,8 @@ func (u UserDbAccess) Update(
return errors.New("hashes do not match")
}

reg.Name = doc.Name
reg.Passport = doc.Passport
reg.Role = doc.Role
reg.Picture = doc.Picture
reg.Registered = false

result, err := u.client.Database("registration").Collection("documents").UpdateOne(context.Background(),
docId, reg)
result, err := d.client.Database("registration").Collection("documents").UpdateOne(context.Background(),
id, reg)
if err != nil {
return err
}
Expand All @@ -141,11 +133,11 @@ func (u UserDbAccess) Update(
}

// Delete updates a document in the DB
func (u UserDbAccess) Delete(docId registry.RegistrationId, hash []byte) error {
func (d dbAccess) Delete(id registry.RegistrationID, hash []byte) error {
var doc Document

err := u.client.Database("registration").Collection("documents").FindOne(context.Background(),
docId).Decode(&doc)
err := d.client.Database("registration").Collection("documents").FindOne(context.Background(),
id).Decode(&doc)
if err != nil {
return err
}
Expand All @@ -154,8 +146,8 @@ func (u UserDbAccess) Delete(docId registry.RegistrationId, hash []byte) error {
return errors.New("hashes do not match")
}

result, err := u.client.Database("registration").Collection("documents").DeleteOne(context.Background(),
docId)
result, err := d.client.Database("registration").Collection("documents").DeleteOne(context.Background(),
id)
if err != nil {
return err
}
Expand All @@ -167,8 +159,8 @@ func (u UserDbAccess) Delete(docId registry.RegistrationId, hash []byte) error {
}

// Disconnect disconnects the user from the DB
func (u UserDbAccess) Disconnect() error {
err := u.client.Disconnect(context.Background())
func (d dbAccess) Disconnect() error {
err := d.client.Disconnect(context.Background())
if err != nil {
panic(err)
}
Expand Down
59 changes: 23 additions & 36 deletions server/registration/main.go → server/registration/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"log"
"net/http"
"time"

"github.com/gorilla/mux"
"go.dedis.ch/hbt/server/registration/config"
Expand All @@ -14,31 +15,7 @@ import (

// curl -F "name='John Doe'" -F "passport=12XY456789" -F "role=0" -F "image=@test/passport.jpg" -F "registered=false" localhost:3000/document

// POST /document HTTP/1.1
// Host: localhost:3000
// User-Agent: curl/7.81.0
// Accept: */*
// Content-Length: 8722
// Content-Type: multipart/form-data; boundary=------------------------a7b1b827961ef70e
//
// --------------------------a7b1b827961ef70e
// Content-Disposition: form-data; name="name"
//
// 'John Doe'
// --------------------------a7b1b827961ef70e
// Content-Disposition: form-data; name="passport"
//
// 12XY456789
// --------------------------a7b1b827961ef70e
// Content-Disposition: form-data; name="role"
//
// 0
// --------------------------a7b1b827961ef70e
// Content-Disposition: form-data; name="image"; filename="picture.png"
// Content-Type: image/png
//
// �PNG

// application defines the application instance
type application struct {
UserRouter *mux.Router
AdminRouter *mux.Router
Expand Down Expand Up @@ -79,33 +56,43 @@ func newApp() *application {
userRouter.HandleFunc("/document", user.CreateDocument).Methods("POST")
userRouter.HandleFunc("/document", user.GetDocument).Methods("GET")
userRouter.HandleFunc("/document", user.UpdateDocument).Methods("PUT")
userRouter.HandleFunc("/document", user.DeleteDocument).Methods("DELETE")

adminRouter := mux.NewRouter()
adminRouter.HandleFunc("/admin/document", admin.GetDocument).Methods("GET")
adminRouter.HandleFunc("/admin/document", admin.UpdateDocument).Methods("PUT")
adminRouter.HandleFunc("/admin/document", admin.DeleteDocument).Methods("DELETE")

userDb, err := mongodb.NewUserDbAccess()
db, err := mongodb.NewDBAccess()
if err != nil {
log.Fatal(err)
}
user.RegisterDb(userDb)
user.RegisterDB(db)

adminDb := mongodb.NewAdminDbAccess()
admin.RegisterDb(adminDb)
db, err = mongodb.NewDBAccess()
if err != nil {
log.Fatal(err)
}
admin.RegisterDB(db)

return &application{AdminRouter: adminRouter, UserRouter: userRouter}
}

func (a *application) start() {
log.Println(fmt.Sprintf("Starting user server on port %s", config.AppConfig.UserServerPort))
go log.Fatal(http.ListenAndServe(fmt.Sprintf(":%v", config.AppConfig.UserServerPort),
a.UserRouter))
log.Printf("Starting user server on port %s", config.AppConfig.UserServerPort)
s := &http.Server{
Addr: fmt.Sprintf(":%v", config.AppConfig.UserServerPort),
ReadHeaderTimeout: 3 * time.Second,
Handler: a.UserRouter,
}
go log.Fatal(s.ListenAndServe())

log.Println(fmt.Sprintf("Starting admin server on port %s", config.AppConfig.AdminServerPort))
go log.Fatal(http.ListenAndServe(fmt.Sprintf(":%v", config.AppConfig.AdminServerPort),
a.AdminRouter))
log.Printf("Starting admin server on port %s", config.AppConfig.AdminServerPort)
s = &http.Server{
Addr: fmt.Sprintf(":%v", config.AppConfig.AdminServerPort),
ReadHeaderTimeout: 3 * time.Second,
Handler: a.AdminRouter,
}
go log.Fatal(s.ListenAndServe())
}

func main() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/steinfletcher/apitest"
)

func TestCreateDocument(t *testing.T) {
func IgnoreTestCreateDocument(t *testing.T) {
apitest.New().
Handler(newApp().UserRouter).
Post("/document").
Expand Down
Loading

0 comments on commit 140661c

Please sign in to comment.