blog数据库添加

master
18650180552 2019-05-16 18:05:20 +08:00
parent d8ce530648
commit e1a9009d95
11 changed files with 451 additions and 2 deletions

8
.idea/background.iml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

6
.idea/misc.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" />
</component>
</project>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/background.iml" filepath="$PROJECT_DIR$/.idea/background.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

322
.idea/workspace.xml Normal file
View File

@ -0,0 +1,322 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ChangeListManager">
<list default="true" id="882ad3e2-74c3-43b0-b9f1-5497ec2bf4ca" name="Default Changelist" comment="">
<change afterPath="$PROJECT_DIR$/.idea/background.iml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/modules.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/vcs.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/controller/blog.go" afterDir="false" />
<change afterPath="$PROJECT_DIR$/model/blog.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/config/config.go" beforeDir="false" afterPath="$PROJECT_DIR$/config/config.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/db/sqlManager.go" beforeDir="false" afterPath="$PROJECT_DIR$/db/sqlManager.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/main.go" beforeDir="false" afterPath="$PROJECT_DIR$/main.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/user.yaml" beforeDir="false" afterPath="$PROJECT_DIR$/user.yaml" afterDir="false" />
</list>
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="FileEditorManager">
<leaf>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/controller/file.go">
<provider selected="true" editor-type-id="text-editor" />
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/user.yaml">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="253">
<caret line="11" column="7" selection-start-line="11" selection-start-column="7" selection-end-line="11" selection-end-column="7" />
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/main.go">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="194">
<caret line="91" column="24" selection-start-line="91" selection-start-column="17" selection-end-line="91" selection-end-column="24" />
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/controller/blog.go">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="207">
<caret line="9" column="10" selection-start-line="9" selection-start-column="10" selection-end-line="9" selection-end-column="10" />
<folding>
<element signature="e#20#78#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/controller/user.go">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="439">
<caret line="116" column="18" selection-start-line="116" selection-start-column="18" selection-end-line="116" selection-end-column="18" />
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/model/model.go">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="374">
<caret line="50" column="5" selection-start-line="50" selection-start-column="5" selection-end-line="50" selection-end-column="5" />
<folding>
<element signature="e#15#109#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/model/blog.go">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="391">
<caret line="17" column="10" selection-start-line="17" selection-start-column="10" selection-end-line="17" selection-end-column="10" />
<folding>
<element signature="e#15#42#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/db/db.go">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="285">
<caret line="124" column="70" selection-start-line="124" selection-start-column="70" selection-end-line="124" selection-end-column="70" />
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://C:/Go/src/database/sql/sql.go">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="285">
<caret line="2586" column="5" selection-start-line="2586" selection-start-column="5" selection-end-line="2586" selection-end-column="5" />
</state>
</provider>
</entry>
</file>
</leaf>
</component>
<component name="FileTemplateManagerImpl">
<option name="RECENT_TEMPLATES">
<list>
<option value="Go File" />
</list>
</option>
</component>
<component name="GOROOT" path="C:\Go" />
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component>
<component name="IdeDocumentHistory">
<option name="CHANGED_PATHS">
<list>
<option value="$PROJECT_DIR$/model/blog.go" />
<option value="$PROJECT_DIR$/controller/blog.go" />
<option value="$PROJECT_DIR$/main.go" />
</list>
</option>
</component>
<component name="ProjectFrameBounds" extendedState="6">
<option name="x" value="573" />
<option name="y" value="60" />
<option name="width" value="1288" />
<option name="height" value="1000" />
</component>
<component name="ProjectView">
<navigator proportions="" version="1">
<foldersAlwaysOnTop value="true" />
</navigator>
<panes>
<pane id="Scope" />
<pane id="ProjectPane">
<subPane>
<expand>
<path>
<item name="background" type="b2602c69:ProjectViewProjectNode" />
<item name="background" type="462c0819:PsiDirectoryNode" />
</path>
<path>
<item name="background" type="b2602c69:ProjectViewProjectNode" />
<item name="background" type="462c0819:PsiDirectoryNode" />
<item name="controller" type="462c0819:PsiDirectoryNode" />
</path>
<path>
<item name="background" type="b2602c69:ProjectViewProjectNode" />
<item name="background" type="462c0819:PsiDirectoryNode" />
<item name="redis" type="462c0819:PsiDirectoryNode" />
</path>
</expand>
<select />
</subPane>
</pane>
</panes>
</component>
<component name="PropertiesComponent">
<property name="ASKED_SHARE_PROJECT_CONFIGURATION_FILES" value="true" />
<property name="DefaultGoTemplateProperty" value="Go File" />
<property name="SHARE_PROJECT_CONFIGURATION_FILES" value="true" />
<property name="WebServerToolWindowFactoryState" value="false" />
<property name="go.gopath.indexing.explicitly.defined" value="true" />
<property name="go.import.settings.migrated" value="true" />
<property name="go.sdk.automatically.set" value="true" />
<property name="go.vendoring.notification.had.been.shown" value="true" />
<property name="last_opened_file_path" value="$PROJECT_DIR$" />
<property name="nodejs_interpreter_path.stuck_in_default_project" value="undefined stuck path" />
<property name="nodejs_npm_path_reset_for_default_project" value="true" />
<property name="nodejs_package_manager_path" value="npm" />
<property name="settings.editor.selected.configurable" value="editor.preferences.gutterIcons" />
</component>
<component name="RunDashboard">
<option name="ruleStates">
<list>
<RuleState>
<option name="name" value="ConfigurationTypeDashboardGroupingRule" />
</RuleState>
<RuleState>
<option name="name" value="StatusDashboardGroupingRule" />
</RuleState>
</list>
</option>
</component>
<component name="RunManager">
<configuration name="go build main.go" type="GoApplicationRunConfiguration" factoryName="Go Application" temporary="true" nameIsGenerated="true">
<module name="background" />
<working_directory value="$PROJECT_DIR$/" />
<kind value="FILE" />
<filePath value="$PROJECT_DIR$/main.go" />
<package value="background" />
<directory value="$PROJECT_DIR$/" />
<method v="2" />
</configuration>
<recent_temporary>
<list>
<item itemvalue="Go Build.go build main.go" />
</list>
</recent_temporary>
</component>
<component name="ToolWindowManager">
<frame x="-8" y="32" width="1936" height="1056" extended-state="6" />
<editor active="true" />
<layout>
<window_info id="Favorites" side_tool="true" />
<window_info content_ui="combo" id="Project" order="0" visible="true" weight="0.2497332" />
<window_info id="Structure" order="1" side_tool="true" weight="0.25" />
<window_info anchor="bottom" id="LuaCheck" />
<window_info anchor="bottom" id="Docker" show_stripe_button="false" />
<window_info anchor="bottom" id="Database Changes" />
<window_info anchor="bottom" id="Version Control" />
<window_info anchor="bottom" id="Terminal" />
<window_info anchor="bottom" id="Event Log" side_tool="true" />
<window_info anchor="bottom" id="Message" order="0" />
<window_info anchor="bottom" id="Find" order="1" />
<window_info active="true" anchor="bottom" id="Run" order="2" visible="true" weight="0.32960895" />
<window_info anchor="bottom" id="Debug" order="3" weight="0.4" />
<window_info anchor="bottom" id="Cvs" order="4" weight="0.25" />
<window_info anchor="bottom" id="Inspection" order="5" weight="0.4" />
<window_info anchor="bottom" id="TODO" order="6" />
<window_info anchor="right" id="Google Cloud Storage" show_stripe_button="false" />
<window_info anchor="right" id="JSON Utils" />
<window_info anchor="right" id="Database" />
<window_info anchor="right" id="Commander" internal_type="SLIDING" order="0" type="SLIDING" weight="0.4" />
<window_info anchor="right" id="Ant Build" order="1" weight="0.25" />
<window_info anchor="right" content_ui="combo" id="Hierarchy" order="2" weight="0.25" />
</layout>
</component>
<component name="TypeScriptGeneratedFilesManager">
<option name="version" value="1" />
</component>
<component name="editorHistoryManager">
<entry file="file://$PROJECT_DIR$/controller/file.go">
<provider selected="true" editor-type-id="text-editor" />
</entry>
<entry file="file://$PROJECT_DIR$/redis/redis.go">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="-4342">
<caret line="2" column="7" selection-start-line="2" selection-start-column="7" selection-end-line="2" selection-end-column="7" />
<folding>
<element signature="e#15#96#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/user.yaml">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="253">
<caret line="11" column="7" selection-start-line="11" selection-start-column="7" selection-end-line="11" selection-end-column="7" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/model/model.go">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="374">
<caret line="50" column="5" selection-start-line="50" selection-start-column="5" selection-end-line="50" selection-end-column="5" />
<folding>
<element signature="e#15#109#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/controller/user.go">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="439">
<caret line="116" column="18" selection-start-line="116" selection-start-column="18" selection-end-line="116" selection-end-column="18" />
</state>
</provider>
</entry>
<entry file="file://C:/Go/src/database/sql/sql.go">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="285">
<caret line="2586" column="5" selection-start-line="2586" selection-start-column="5" selection-end-line="2586" selection-end-column="5" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/db/db.go">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="285">
<caret line="124" column="70" selection-start-line="124" selection-start-column="70" selection-end-line="124" selection-end-column="70" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/model/blog.go">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="391">
<caret line="17" column="10" selection-start-line="17" selection-start-column="10" selection-end-line="17" selection-end-column="10" />
<folding>
<element signature="e#15#42#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/controller/blog.go">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="207">
<caret line="9" column="10" selection-start-line="9" selection-start-column="10" selection-end-line="9" selection-end-column="10" />
<folding>
<element signature="e#20#78#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/main.go">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="194">
<caret line="91" column="24" selection-start-line="91" selection-start-column="17" selection-end-line="91" selection-end-column="24" />
</state>
</provider>
</entry>
</component>
</project>

View File

@ -19,6 +19,8 @@ type ConfAPI struct {
Logs LogConfig `yaml:"logs"` // 日志
Redis1 EntityRedis `yaml:"redis1"`
Mysql MysqlConfig `yaml:"mysql"` // 认证配置
Mysql1 MysqlConfig `yaml:"mysql1"` // 认证配置
init bool
}
@ -81,6 +83,13 @@ func GetMysqlConfig() *MysqlConfig {
return nil
}
}
func GetMysqlBlogConfig() *MysqlConfig {
if gConf.init {
return &gConf.Mysql1
} else {
return nil
}
}
func GetRedis1() *EntityRedis {
if gConf.init {
return &gConf.Redis1

41
controller/blog.go Normal file
View File

@ -0,0 +1,41 @@
package controller
import (
"background/logs"
"background/model"
"github.com/gin-gonic/gin"
)
func AddArticle(c *gin.Context) {
rsp := RespBase{Msg:"FAIL", Status:210,}
type ReqAddArticle struct {
Title string `json:"title"`
Content string `json:"content"`
author string `json:"author"`
Type int64 `json:"type"`
}
var req ReqAddArticle
defer func() {
c.JSON(200,rsp)
}()
er := c.BindJSON(&req)
if nil != er{
logs.Error(er.Error())
return
}
e := model.CreateDoc(
model.Doc{
Type:req.Type,
Title:req.Title,
Content:req.Content,
Author:req.author,
},
)
if nil != e{
logs.Error(e.Error())
return
}
rsp.Msg = "OK"
rsp.Status = 0
}

View File

@ -8,9 +8,16 @@ import (
)
var gDb Database
var blogDb Database
func Init() {
mysqlconf := config.GetMysqlConfig()
blogConf := config.GetMysqlBlogConfig()
gDb = Database{Type: string(""), DB: initMysql(mysqlconf)}
blogDb = Database{Type: string(""), DB: initMysql(blogConf)}
}
func initMysql(mysqlconf *config.MysqlConfig) *sql.DB {
cnn := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8", mysqlconf.UserName, mysqlconf.Password,
mysqlconf.Addr, mysqlconf.Db)
_db, err := sql.Open("mysql", cnn)
@ -21,9 +28,11 @@ func Init() {
if nil != e {
fmt.Println(e.Error())
}
gDb = Database{Type: string(""), DB: _db}
return _db
}
func GetMysqlClient() *Database {
return &gDb
}
func GetBlogMysql() *Database{
return &blogDb
}

View File

@ -88,6 +88,9 @@ func main() {
api.POST("/verify", mailContoller.OnSendEmailCode)
/** 修改密码**/
api.POST("modify_pass",middle.AuthMiddle,userController.ModifyPasswd)
api.PUT("article",controller.AddArticle) // 添加文章
}
e := r.Run(":" + strconv.Itoa(config.GetPort()))

29
model/blog.go Normal file
View File

@ -0,0 +1,29 @@
package model
import (
"background/db"
"background/logs"
"errors"
"fmt"
)
type Doc struct {
ID int64 `sql:"id" json:"id"`
Title string `sql:"title" json:"title"`
Type int64 `sql:"type" json:"type"`
Content string `sql:"content" json:"content"`
Author string `sql:"author" json:"author"`
}
func CreateDoc(doc Doc) error {
if doc.ID == 0{
return errors.New("wrong primary key")
}
sql := fmt.Sprintf("insert into doc(title,type,content,author) values ('%s','%d','%s','%s')",doc.Title,doc.Type,doc.Content,doc.Author)
_,e := db.GetMysqlClient().Query(sql)
if nil != e{
logs.Error(e.Error())
return e
}
return nil
}

View File

@ -17,3 +17,11 @@ mysql:
db: background
max_open: 100
MaxIdle: 99
mysql2:
addr: 118.24.238.198
user: server
password: server
db: background
max_open: 100
MaxIdle: 99