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>
博客的分类和登陆
转载于:https://blog.51cto.com/daixuan/2152863