温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

beeblog创建博客

发布时间:2020-07-25 10:36:06 来源:网络 阅读:999 作者:1350368559 栏目:开发技术

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/
beeblog创建博客

修改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">
        <h2>我的第一篇博客</h2>
        <h7 class="text-muted">文章发表于2018年7月31日XX时XX分,共有100次浏览,200个评论</h7>
        <p>大家好,这是我的第一篇博客,这里是博客内容,谢谢查看</p>
      </div>
    </div>
  </body>
</html>

浏览器访问:http://localhost:8080/
beeblog创建博客

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">
        <h2>我的第一篇博客</h2>
        <h7 class="text-muted">文章发表于2018年7月31日XX时XX分,共有100次浏览,200个评论</h7>
        <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>

博客的分类和登陆

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI