Last active
October 14, 2015 17:24
-
-
Save firstrow/6d260e9eb5a57625f37c to your computer and use it in GitHub Desktop.
Sample API
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
func main() { | |
app := NewApplication("development.toml") | |
app.Start(":8080") | |
} | |
type Application struct { | |
Config *tomlConfig | |
PagesController *ControllerInterface | |
PagesStorage *StorageInterface | |
UsersStorage *StorageInterface | |
TagsController *ControllerInterface | |
Mailer *STMPMailer | |
Db *Database | |
Logger LoggerInterface | |
} | |
func NewApplication(config string) { | |
database := NewPostgresSesssion(config) | |
logger := log.NewFileLogger() | |
app := new(Application) | |
app.Config = NewConfig(config) | |
app.PagesController = products.NewPagesController(app) | |
app.PagesStorge = storage.NewPagesStorage(app, database) | |
app.UserStorage = storage.NewUserStorage(app, database) | |
app.TagsController = tags.NewTagsController(app, other_dependency) | |
app.Logger = logger | |
app.Mailer = &SMTPMailer{app: app} | |
// Many other services | |
app.Db = &Database{app: app} | |
return app | |
} | |
// controllers/pages_controller.go | |
// package controllers | |
type PagesController struct { | |
*base.Controller | |
app *base.Application | |
} | |
func NewPagesController(app *Application) *PagesController { | |
ctrl := &PagesController{app} | |
// Register controller routes | |
app.AddRoute("/page/:id", ctrl.ViewPage) | |
return ctrl | |
} | |
func (c *PagesController) View(ctx *base.Context) { | |
page, err := ctx.app.PagesStorage.GetById(ctx.Param("id")) | |
if err != nil { | |
ctx.Render("404") | |
} | |
ctx.Render("pages/view", ctx.D{ | |
"page": page, | |
}) | |
} | |
type UserStorage struct { | |
database DatabaseInterface | |
} | |
// user_storage.go | |
func NewUserStorage(database DatabaseInterface) *UserStorage { | |
return &UserStorage{database} | |
} | |
func (s *UserStorage) GetByEmail(email string) (*User, error) { | |
user = new(User) | |
err := s.database.SelectOne("SELECT * FROM users WHERE email=?", email).Encode(user) | |
if err != nil { | |
return nil, err | |
} | |
return user, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment