forked from alienscience/imapsrv
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Functional configuration refactoring. Fixes alienscience#5
Inspired by @davecheney post: http://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis
- Loading branch information
Alexandru Plugaru
committed
Nov 4, 2014
1 parent
d3b4006
commit 845b64d
Showing
6 changed files
with
229 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,53 @@ | ||
|
||
package main | ||
|
||
import ( | ||
imap "github.com/alienscience/imapsrv" | ||
) | ||
|
||
// A dummy mailstore used for demonstrating the IMAP server | ||
type Mailstore struct { | ||
} | ||
|
||
func main() { | ||
// Configure an IMAP server on localhost port 1193 | ||
config := imap.DefaultConfig() | ||
config.Interface = "127.0.0.1:1193" | ||
|
||
// Configure a dummy mailstore | ||
mailstore := &Mailstore{} | ||
config.Store = mailstore | ||
|
||
// Start the server | ||
server := imap.Create(config) | ||
server.Start() | ||
|
||
// The simplest possible server - zero config | ||
// It will find a free tcp port, create some temporary directories.. - just give me a server! | ||
//s := imap.NewServer() | ||
//s.Start() | ||
|
||
// More advanced config | ||
m := &imap.DummyMailstore{} | ||
|
||
s := imap.NewServer( | ||
imap.Listen("127.0.0.1:1193"), | ||
imap.Store(m), | ||
) | ||
s.Start() | ||
} | ||
|
||
//------ Dummy Mailstore ------------------------------------------------------- | ||
// A dummy mailstore used for demonstrating the IMAP server | ||
type DummyMailstore struct { | ||
} | ||
|
||
// Get mailbox information | ||
func (m *Mailstore) GetMailbox(name string) (*imap.Mailbox, error) { | ||
func (m *DummyMailstore) GetMailbox(name string) (*imap.Mailbox, error) { | ||
return &imap.Mailbox{ | ||
Name: "inbox", | ||
Id: 1, | ||
Id: 1, | ||
}, nil | ||
} | ||
|
||
// Get the sequence number of the first unseen message | ||
func (m *Mailstore) FirstUnseen(mbox int64) (int64, error) { | ||
func (m *DummyMailstore) FirstUnseen(mbox int64) (int64, error) { | ||
return 4, nil | ||
} | ||
|
||
// Get the total number of messages in an IMAP mailbox | ||
func (m *Mailstore) TotalMessages(mbox int64) (int64, error) { | ||
func (m *DummyMailstore) TotalMessages(mbox int64) (int64, error) { | ||
return 8, nil | ||
} | ||
|
||
// Get the total number of unread messages in an IMAP mailbox | ||
func (m *Mailstore) RecentMessages(mbox int64) (int64, error) { | ||
func (m *DummyMailstore) RecentMessages(mbox int64) (int64, error) { | ||
return 4, nil | ||
} | ||
|
||
// Get the next available uid in an IMAP mailbox | ||
func (m *Mailstore) NextUid(mbox int64) (int64, error) { | ||
func (m *DummyMailstore) NextUid(mbox int64) (int64, error) { | ||
return 9, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package imapsrv | ||
|
||
// A service that is needed to read mail messages | ||
type Mailstore interface { | ||
// Get IMAP mailbox information | ||
// Returns nil if the mailbox does not exist | ||
GetMailbox(name string) (*Mailbox, error) | ||
// Get the sequence number of the first unseen message | ||
FirstUnseen(mbox int64) (int64, error) | ||
// Get the total number of messages in an IMAP mailbox | ||
TotalMessages(mbox int64) (int64, error) | ||
// Get the total number of unread messages in an IMAP mailbox | ||
RecentMessages(mbox int64) (int64, error) | ||
// Get the next available uid in an IMAP mailbox | ||
NextUid(mbox int64) (int64, error) | ||
} | ||
|
||
// A dummy mailstore used for demonstrating the IMAP server | ||
type DummyMailstore struct { | ||
} | ||
|
||
// Get mailbox information | ||
func (m *DummyMailstore) GetMailbox(name string) (*Mailbox, error) { | ||
return &Mailbox{ | ||
Name: "inbox", | ||
Id: 1, | ||
}, nil | ||
} | ||
|
||
// Get the sequence number of the first unseen message | ||
func (m *DummyMailstore) FirstUnseen(mbox int64) (int64, error) { | ||
return 4, nil | ||
} | ||
|
||
// Get the total number of messages in an IMAP mailbox | ||
func (m *DummyMailstore) TotalMessages(mbox int64) (int64, error) { | ||
return 8, nil | ||
} | ||
|
||
// Get the total number of unread messages in an IMAP mailbox | ||
func (m *DummyMailstore) RecentMessages(mbox int64) (int64, error) { | ||
return 4, nil | ||
} | ||
|
||
// Get the next available uid in an IMAP mailbox | ||
func (m *DummyMailstore) NextUid(mbox int64) (int64, error) { | ||
return 9, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
|
||
package imapsrv | ||
|
||
import ( | ||
|
Oops, something went wrong.