1. 程式人生 > >beeblog創建博客

beeblog創建博客

自動 提示 scrip 每次 efault syncdb 技術分享 sql category

1、定義modles.go

這裏需要提前get一些包,否則會提示找不到哦,
models/models.go:5:2:

go get github.com/mattn/go-sqlite3
go get github.com/unknwon/com

package models

import (
    "os"
    "path"
    "time"

    "github.com/astaxie/beego/orm"
    _ "github.com/mattn/go-sqlite3"
    "github.com/unknwon/com"
)

const (
    _DB_NAME         = "data/beeblog.db"
    _SQLLITE3_DRIVER = "sqlite3"
)

type Categeory struct {
    Id              int64
    Title           string
    Created         time.Time `orm:"index"`
    views           int64     `orm:"index"`
    TopicTime       time.Time `orm:"index"`
    TopicCount      int64
    TopicLastUserId int64
}

type Topic struct {
    Id              int64
    Uid             int64
    Title           string
    Content         string `orm:"size(5000)"`
    Attachment      string
    Created         time.Time `orm:"index"`
    Updated         time.Time `orm:"index"`
    views           int64     `orm:"index"`
    Author          string
    ReplyTime       time.Time `orm:"index"`
    ReplyCount      int64
    ReplyLastUserId int64
}

func RegisterDB() {
    if !com.IsExist(_DB_NAME) {
        os.MkdirAll(path.Dir(_DB_NAME), os.ModePerm) //創建/a/b/c/d目錄
        os.Create(_DB_NAME)                          //數據庫文件不存在的時候會自動創建
    }
    orm.RegisterModel(new(Categeory), new(Topic))                   //註冊模型
    orm.RegisterDriver(_SQLLITE3_DRIVER, orm.DRSqlite)              //註冊驅動
    orm.RegisterDataBase("default", _SQLLITE3_DRIVER, _DB_NAME, 10) //註冊默認數據庫
}

2 定義home.html 前端的頁面

這裏需要下載bootsstrap文件到static文件下 https://github.com/twbs/bootstrap/releases/download/v3.3.7/bootstrap-3.3.7-dist.zip

<!DOCTYPE html>
<html>
  <head>
    <title>首頁 - 我的beego博客</title>>
    <link rel="short icon" href="/static/img/favicon.png">
    <link rel="stylesheet" type="text/css" href="/static/css/bootstrap.min.css">
  </head>

  <body>
    <div class="navbar navbar-default navbar-fixed-top">
      <div class="container">
        <a class="navbar-brand" href="/">我的博客</a>
        <ul class="nav navbar-nav">
          <li class="active"><a href="/">首頁</a></li>
          <li><a href="/category">分類</a></li>
          <li><a href="/topic">文章</a></li>
        </ul>
      </div>    
    </div>
  </body>
</html>

3 定義main.go

package main

import (
    "beeblog/models"
    _ "beeblog/routers"

    "github.com/astaxie/beego"
    "github.com/astaxie/beego/orm"
)

func init() {
    models.RegisterDB()
}

func main() {
    orm.Debug = true                      //debug在開發模式下設置為true
    orm.RunSyncdb("default", false, true) //必須有一個數據庫名字叫default,false表示不是每次都刪除重建表,首次建表即可,true表示打印建表信息
    beego.Run()
}

執行:bee run beeblog

beeblog

2018/07/31 16:18:19 SUCCESS  ? 0006 Built Successfully!
2018/07/31 16:18:19 INFO     ? 0007 Restarting ‘beeblog‘...
2018/07/31 16:18:19 SUCCESS  ? 0008 ‘./beeblog‘ is running...
create table `categeory`
    -- --------------------------------------------------
    --  Table Structure for `beeblog/models.Categeory`
    -- --------------------------------------------------
    CREATE TABLE IF NOT EXISTS `categeory` (
        `id` integer NOT NULL PRIMARY KEY AUTOINCREMENT,
        `title` varchar(255) NOT NULL DEFAULT ‘‘ ,
        `created` datetime NOT NULL,
        `topic_time` datetime NOT NULL,
        `topic_count` integer NOT NULL DEFAULT 0 ,
        `topic_last_user_id` integer NOT NULL DEFAULT 0
    );
    CREATE INDEX `categeory_created` ON `categeory` (`created`);
    CREATE INDEX `categeory_topic_time` ON `categeory` (`topic_time`);

create table `topic`
    -- --------------------------------------------------
    --  Table Structure for `beeblog/models.Topic`
    -- --------------------------------------------------
    CREATE TABLE IF NOT EXISTS `topic` (
        `id` integer NOT NULL PRIMARY KEY AUTOINCREMENT,
        `uid` integer NOT NULL DEFAULT 0 ,
        `title` varchar(255) NOT NULL DEFAULT ‘‘ ,
        `content` varchar(5000) NOT NULL DEFAULT ‘‘ ,
        `attachment` varchar(255) NOT NULL DEFAULT ‘‘ ,
        `created` datetime NOT NULL,
        `updated` datetime NOT NULL,
        `author` varchar(255) NOT NULL DEFAULT ‘‘ ,
        `reply_time` datetime NOT NULL,
        `reply_count` integer NOT NULL DEFAULT 0 ,
        `reply_last_user_id` integer NOT NULL DEFAULT 0
    );
    CREATE INDEX `topic_created` ON `topic` (`created`);
    CREATE INDEX `topic_updated` ON `topic` (`updated`);
    CREATE INDEX `topic_reply_time` ON `topic` (`reply_time`);

2018/07/31 16:18:19.789 [I] [asm_amd64.s:2337] http server Running on http://:8080
2018/07/31 16:18:41.650 [D] [server.go:2619] ::1 - - [31/Jul/2018 04:18:41] "GET / HTTP/1.1 200 0" 0.005279  Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36

2018/07/31 16:18:41.667 [D] [server.go:2619] ::1 - - [31/Jul/2018 04:18:41] "GET /static/css/bootstrap.min.css HTTP/1.1 200 0" 0.002514 http://localhost:8080/ Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36

2018/07/31 16:18:43.342 [D] [server.go:2619] ::1 - - [31/Jul/2018 04:18:43] "GET /category HTTP/1.1 404 0" 0.003407 http://localhost:8080/ Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36

beeblog
2018/07/31 16:23:05 SUCCESS  ? 0009 Built Successfully!
2018/07/31 16:23:05 INFO     ? 0010 Restarting ‘beeblog‘...
2018/07/31 16:23:05 SUCCESS  ? 0011 ‘./beeblog‘ is running...
table `categeory` already exists, skip //這裏表已經存在,跳過創建
table `topic` already exists, skip
2018/07/31 16:23:05.792 [I] [asm_amd64.s:2337] http server Running on http://:8080
2018/07/31 16:24:34.034 [D] [server.go:2619] ::1 - - [31/Jul/2018 04:24:34] "GET / HTTP/1.1 200 0" 0.011260  Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36

瀏覽器訪問:http://localhost:8080/
技術分享圖片

修改home.html

<!DOCTYPE html>
<html>
  <head>
    <title>首頁 - 我的beego博客</title>>
    <link rel="short icon" href="/static/img/favicon.png">
    <link rel="stylesheet" type="text/css" href="/static/css/bootstrap.min.css">
  </head>

  <body>
    <div class="navbar navbar-default navbar-fixed-top">
      <div class="container">
        <a class="navbar-brand" href="/">我的博客</a>
        <ul class="nav navbar-nav">
          <li class="active"><a href="/">首頁</a></li>
          <li><a href="/category">分類</a></li>
          <li><a href="/topic">文章</a></li>
        </ul>
      </div>    
    </div>
    <div class="container">
      <div class="page-header">
        <h1>我的第一篇博客</h1>
        <h6 class="text-muted">文章發表於2018年7月31日XX時XX分,共有100次瀏覽,200個評論</h6>
        <p>大家好,這是我的第一篇博客,這裏是博客內容,謝謝查看</p>
      </div>
    </div>
  </body>
</html>

瀏覽器訪問:http://localhost:8080/
技術分享圖片

4 如果使用js或者jquery,在home.html加上兩行:

其中,jquery使用的是七牛雲存儲提供的jquery地址,查詢jquery地址:http://www.staticfile.org/

<!DOCTYPE html>
<html>
  <head>
    <title>首頁 - 我的beego博客</title>>
    <link rel="short icon" href="/static/img/favicon.png">
    <link rel="stylesheet" type="text/css" href="/static/css/bootstrap.min.css">
  </head>

  <body>
    <div class="navbar navbar-default navbar-fixed-top">
      <div class="container">
        <a class="navbar-brand" href="/">我的博客</a>
        <ul class="nav navbar-nav">
          <li class="active"><a href="/">首頁</a></li>
          <li><a href="/category">分類</a></li>
          <li><a href="/topic">文章</a></li>
        </ul>
      </div>    
    </div>
    <div class="container">
      <div class="page-header">
        <h1>我的第一篇博客</h1>
        <h6 class="text-muted">文章發表於2018年7月31日XX時XX分,共有100次瀏覽,200個評論</h6>
        <p>大家好,這是我的第一篇博客,這裏是博客內容,謝謝查看</p>
      </div>
    </div>
     <script type="text/javascript" src="https://cdn.staticfile.org/jquery/2.0.3/jquery.min.js"></script>
     <script type="text/javascript" src="/static/js/bootstrap.min.js"></script>
  </body>
</html>

beeblog創建博客