Last active
December 21, 2015 06:29
-
-
Save levicole/6264580 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package controllers | |
import( | |
"fmt" | |
"github.com/levicole/lev.io/models" | |
"github.com/astaxie/beego" | |
"database/sql" | |
_ "github.com/go-sql-driver/mysql" | |
"github.com/coopernurse/gorp" | |
) | |
type MainController struct { | |
beego.Controller | |
Db *sql.DB | |
Dbmap *gorp.DbMap | |
} | |
// This function does not have access to Dbmap for some reason. | |
func (this *MainController) Get() { | |
if this.Db == nil { | |
fmt.Println("i have no idea why this is nil") | |
} | |
if _, ok := this.Ctx.Params[":url*"]; ok { | |
if this.Dbmap == nil { | |
this.Ctx.WriteString(this.Ctx.Params[":url*"]) | |
} else { | |
query := "select Id, Url from links l where slug='asdf'" | |
var links []*models.LinkView | |
_, e := this.Dbmap.Select(&links, query) | |
if e != nil{ | |
fmt.Println(e) | |
} | |
this.Ctx.Redirect(302, links[0].Url) | |
} | |
} else { | |
this.Ctx.WriteString("couldn't find url") | |
} | |
} |
This file contains hidden or 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
package main | |
import ( | |
"fmt" | |
"github.com/levicole/lev.io/controllers" | |
"github.com/levicole/lev.io/models" | |
"log" | |
"os" | |
"database/sql" | |
_ "github.com/go-sql-driver/mysql" | |
"github.com/astaxie/beego" | |
"github.com/coopernurse/gorp" | |
) | |
var ( | |
db *sql.DB | |
dbmap *gorp.DbMap | |
Links *gorp.TableMap | |
) | |
func init() { | |
var mysqlerr error | |
db, mysqlerr = sql.Open("mysql", "root@unix(/opt/boxen/data/mysql/socket)/levio") | |
if mysqlerr != nil { | |
fmt.Printf("some error:%s\n", mysqlerr.Error()) | |
panic(mysqlerr) | |
} | |
db.Ping() | |
dbmap = &gorp.DbMap{Db: db, Dialect: gorp.MySQLDialect{"InnoDB", "UTF8"}} | |
dbmap.CreateTablesIfNotExists() | |
Links = dbmap.AddTableWithName(models.Link{}, "links").SetKeys(true, "Id") | |
dbmap.TraceOn("[gorp]", log.New(os.Stdout, "myapp:", log.Lmicroseconds)) | |
} | |
func main() { | |
linksController := &controllers.LinksController{Db: db, Dbmap: dbmap} | |
mainController := &controllers.MainController{Db: db, Dbmap: dbmap} | |
if linksController.Db == nil { | |
fmt.Println("foo") | |
} | |
beego.RESTRouter("/links", linksController) | |
beego.Router("/:url*", mainController) | |
beego.Run() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment