LoginSignup
6
6

More than 5 years have passed since last update.

Beegoを使ってみた

Last updated at Posted at 2015-05-31

初期設定もろもろ

☆Beegoのインストールについて

こちらでサクッとインストール
[http://qiita.com/macococo/items/e5ace2550418ccced9ac#2-2]

☆scaffoldingしてみる

強力なscaffoldingがないと
使わないつもりだったんですがあったのでbeegoを使ってます。

プログラムのコードでDB接続周りがカオスな作りになると
いい思い出が一切ないのでこの辺は自動生成に任せつつ。。。

下記URLのgenerateの記述を確認
[https://github.com/beego/bee]
appcodeを実行すると各それぞれのプログラムが
既存データベースのスキーマから作成可能です。

Main.goに初期設定をコーディング

main.go

package main

import (
    "fmt"
    "github.com/astaxie/beego"
    "github.com/astaxie/beego/orm"
    _ "github.com/go-sql-driver/mysql"
    // cronのようにスケジューラを使ったtasksを使う場合はここで呼んでおく
    _ "project/tasks" // this is the task import
    // routersを読み込み
    _ "project/routers"
)

func init() {
    initDatabase()
    initTemplateExt()
}

func main() {
    beego.Run()
}

func initDatabase() {
    // project/conf/app.confに定義した値を取得
    dbLink  := fmt.Sprintf(
        "%s:%s@%s/%s?charset=%s",
        beego.AppConfig.String("masterUser"),
        beego.AppConfig.String("masterPass"),
        beego.AppConfig.String("masterHost"),
        beego.AppConfig.String("masterDb"),
        beego.AppConfig.String("dbCharset"),
    )

    // 各種初期設定
    orm.RegisterDriver("mysql", orm.DR_MySQL)
    orm.RegisterDataBase("default", "mysql", dbLink)
    orm.SetMaxIdleConns("default", 100)
    orm.SetMaxOpenConns("default", 100)
}

func initTemplateExt() {
    // テンプレートの拡張子をhtmlでも使えるように
    beego.AddTemplateExt(".html")
}
6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6