-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
183 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package business | ||
|
||
import ( | ||
"github.com/magic003/alice/example/client" | ||
"github.com/magic003/alice/example/persist" | ||
) | ||
|
||
// WebPageManager manages web pages. | ||
type WebPageManager struct { | ||
httpClient client.HTTPClient | ||
webPageDao persist.WebPageDao | ||
} | ||
|
||
// NewWebPageManager returns a new instance of WebPageManager. | ||
func NewWebPageManager(httpClient client.HTTPClient, webPageDao persist.WebPageDao) *WebPageManager { | ||
if httpClient == nil || webPageDao == nil { | ||
panic("httpClient or webPageDao cannot be nil") | ||
} | ||
return &WebPageManager{ | ||
httpClient: httpClient, | ||
webPageDao: webPageDao, | ||
} | ||
} |
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,26 @@ | ||
package client | ||
|
||
// HTTPClient is an interface for fetching web page. | ||
type HTTPClient interface { | ||
// Fetch fetches a web page by url. | ||
Fetch(url string) string | ||
} | ||
|
||
// NewHTTPClient returns an instance of HTTPClient. | ||
func NewHTTPClient(retries int) HTTPClient { | ||
if retries <= 0 { | ||
panic("retries should be greater than 0") | ||
} | ||
return &httpClient{ | ||
retries: retries, | ||
} | ||
} | ||
|
||
// httpClient is a dummy implementation of HTTPClient. | ||
type httpClient struct { | ||
retries int | ||
} | ||
|
||
func (client *httpClient) Fetch(url string) string { | ||
return "" | ||
} |
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,28 @@ | ||
package example | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/magic003/alice" | ||
"github.com/magic003/alice/example/business" | ||
"github.com/magic003/alice/example/module" | ||
) | ||
|
||
func TestExample(t *testing.T) { | ||
c := alice.CreateContainer( | ||
&module.ConfigModule{}, &module.PersistModule{}, &module.ClientModule{}, &module.BusinessModule{}) | ||
|
||
retries := c.InstanceByName("Retries") | ||
if retries != 3 { | ||
t.Errorf("bad retries: got %v, expected %v", retries, 3) | ||
} | ||
|
||
table := c.InstanceByName("Table") | ||
if table != "example_table" { | ||
t.Errorf("bad table: got %s, expected %s", table, "example_table") | ||
} | ||
|
||
// should not panic | ||
c.Instance(reflect.TypeOf((*business.WebPageManager)(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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package module | ||
|
||
import ( | ||
"github.com/magic003/alice" | ||
"github.com/magic003/alice/example/business" | ||
"github.com/magic003/alice/example/client" | ||
"github.com/magic003/alice/example/persist" | ||
) | ||
|
||
// BusinessModule is the module for business objects. | ||
type BusinessModule struct { | ||
alice.BaseModule | ||
WebPageDao persist.WebPageDao `alice:""` | ||
HTTPClient client.HTTPClient `alice:"HTTPClient"` | ||
} | ||
|
||
// WebPageManager returns an instance of WebPageManager. | ||
func (m *BusinessModule) WebPageManager() *business.WebPageManager { | ||
return business.NewWebPageManager(m.HTTPClient, m.WebPageDao) | ||
} |
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,17 @@ | ||
package module | ||
|
||
import ( | ||
"github.com/magic003/alice" | ||
"github.com/magic003/alice/example/client" | ||
) | ||
|
||
// ClientModule is the module for clients. | ||
type ClientModule struct { | ||
alice.BaseModule | ||
Retries int `alice:"Retries"` | ||
} | ||
|
||
// HTTPClient returns an instance fo HTTPClient. | ||
func (m *ClientModule) HTTPClient() client.HTTPClient { | ||
return client.NewHTTPClient(m.Retries) | ||
} |
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,20 @@ | ||
package module | ||
|
||
import ( | ||
"github.com/magic003/alice" | ||
) | ||
|
||
// ConfigModule is the module for configurations. | ||
type ConfigModule struct { | ||
alice.BaseModule | ||
} | ||
|
||
// Retries returns the client retry attempts. | ||
func (m *ConfigModule) Retries() int { | ||
return 3 | ||
} | ||
|
||
// Table is the table name. | ||
func (m *ConfigModule) Table() string { | ||
return "example_table" | ||
} |
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,17 @@ | ||
package module | ||
|
||
import ( | ||
"github.com/magic003/alice" | ||
"github.com/magic003/alice/example/persist" | ||
) | ||
|
||
// PersistModule is the module for persistent APIs. | ||
type PersistModule struct { | ||
alice.BaseModule | ||
Table string `alice:"Table"` | ||
} | ||
|
||
// WebPageDao returns the WebPageDao. | ||
func (m *PersistModule) WebPageDao() persist.WebPageDao { | ||
return persist.NewWebPageDao(m.Table) | ||
} |
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,32 @@ | ||
package persist | ||
|
||
// WebPageDao defines the interface to access web pages saved in database. | ||
type WebPageDao interface { | ||
// Find looks up a web page by url. | ||
Find(url string) string | ||
// Save saves a web page. | ||
Save(url string, content string) error | ||
} | ||
|
||
// NewWebPageDao returns an instance of WebPageDao. | ||
func NewWebPageDao(table string) WebPageDao { | ||
if table == "" { | ||
panic("table cannot be empty") | ||
} | ||
return &webPageDao{ | ||
table: table, | ||
} | ||
} | ||
|
||
// webPageDao is a dummy implementation for WebPageDao. | ||
type webPageDao struct { | ||
table string | ||
} | ||
|
||
func (dao *webPageDao) Find(url string) string { | ||
return "" | ||
} | ||
|
||
func (dao *webPageDao) Save(url string, content string) error { | ||
return nil | ||
} |