Skip to content

Server#4

Open
zayanh wants to merge 3 commits intomasterfrom
server
Open

Server#4
zayanh wants to merge 3 commits intomasterfrom
server

Conversation

@zayanh
Copy link
Owner

@zayanh zayanh commented Oct 29, 2016

No description provided.

// homeT will store the html template to be used in this website
var homeT *template.Template

var mu = &sync.Mutex{}
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move the mutex declaration near the data that it protects:

var d data
var dataMtx = &sync.Mutex{}

// Initialize map of usernames
mu.Lock()
d.UserNames = make(map[string]bool)
mu.Unlock()
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for lock.

// Open file to read and write. Create if it doesn't exist. Append if it does
f, err := os.OpenFile(filename, os.O_CREATE | os.O_RDWR | os.O_APPEND, 0666)
if err != nil {
//handle error
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

panic(err)

if err != nil {
//handle error
}
userFile = f // use global variable -- is this approach okay?
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine

for scanner.Scan() {
mu.Lock()
d.UserNames[scanner.Text()] = true
mu.Unlock()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for lock.

}
// Err returns the first non-EOF error encoutnered by the scanner
if err := scanner.Err(); err != nil {
// handle error
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

panic(err)

// Check if the username already exists
mu.Lock()
if _,ok := d.UserNames[username]; ok {
defer mu.Unlock() // is this okay?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

defer is redundant. Just do

mu.Unlock()

}

// Check if the username already exists
mu.Lock()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can do:

mu.Lock()
defer mu.Unlock()

d.Message = "User " + username + " added successfully"
}
http.Redirect(w, r, "/home", http.StatusFound)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good

duplicate entries would take O(n) time, while searching a map only takes O(1) time. */
UserNames map[string]bool
// Message will inform the user of the results from the previous attempt to add a new user
Message string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Messages map[*http.Request]string

Then down below:

func Setup() {
  ...
  Messages = make(map[*http.Request]string)
}

We can disucss

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants