添加mysql SSL登陆

master
a74589669 2019-06-23 19:33:45 +08:00
parent c5a4c7edab
commit 6466e9a6e3
7 changed files with 397 additions and 150 deletions

View File

@ -3,9 +3,11 @@
<component name="ChangeListManager">
<list default="true" id="882ad3e2-74c3-43b0-b9f1-5497ec2bf4ca" name="Default Changelist" comment="">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/controller/blog.go" beforeDir="false" afterPath="$PROJECT_DIR$/controller/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$/model/blog.go" beforeDir="false" afterPath="$PROJECT_DIR$/model/blog.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/model/model.go" beforeDir="false" afterPath="$PROJECT_DIR$/model/model.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" />
@ -14,62 +16,114 @@
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="FUSProjectUsageTrigger">
<session id="-1603790603">
<usages-collector id="statistics.lifecycle.project">
<counts>
<entry key="project.closed" value="8" />
<entry key="project.open.time.1" value="1" />
<entry key="project.open.time.10" value="1" />
<entry key="project.open.time.11" value="1" />
<entry key="project.open.time.12" value="1" />
<entry key="project.open.time.14" value="1" />
<entry key="project.open.time.17" value="1" />
<entry key="project.open.time.20" value="1" />
<entry key="project.open.time.24" value="1" />
<entry key="project.open.time.31" value="1" />
<entry key="project.open.time.4" value="1" />
<entry key="project.open.time.7" value="2" />
<entry key="project.open.time.8" value="3" />
<entry key="project.open.time.9" value="2" />
<entry key="project.opened" value="17" />
</counts>
</usages-collector>
<usages-collector id="statistics.file.extensions.edit">
<counts>
<entry key="go" value="377" />
<entry key="yaml" value="97" />
</counts>
</usages-collector>
<usages-collector id="statistics.file.types.edit">
<counts>
<entry key="Go" value="377" />
<entry key="YAML" value="97" />
</counts>
</usages-collector>
<usages-collector id="statistics.file.extensions.open">
<counts>
<entry key="go" value="13" />
<entry key="yaml" value="1" />
</counts>
</usages-collector>
<usages-collector id="statistics.file.types.open">
<counts>
<entry key="Go" value="13" />
<entry key="YAML" value="1" />
</counts>
</usages-collector>
</session>
</component>
<component name="FileEditorManager">
<leaf SIDE_TABS_SIZE_LIMIT_KEY="300">
<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="418">
<caret line="44" column="41" selection-start-line="44" selection-start-column="41" selection-end-line="44" selection-end-column="41" />
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/config/config.go">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="352">
<caret line="21" column="26" selection-start-line="21" selection-start-column="26" selection-end-line="21" selection-end-column="26" />
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/db/sqlManager.go">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="395">
<caret line="36" column="20" selection-start-line="36" selection-start-column="20" selection-end-line="36" selection-end-column="20" />
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://F:/程序/GOROOT/src/crypto/tls/tls.go">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="3630">
<caret line="177" column="5" selection-start-line="177" selection-start-column="5" selection-end-line="177" selection-end-column="5" />
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/vendor/github.com/go-sql-driver/mysql/utils.go">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="880">
<caret line="53" column="32" selection-start-line="53" selection-start-column="22" selection-end-line="53" selection-end-column="32" />
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://F:/程序/GOROOT/src/database/sql/sql.go">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="12804">
<caret line="594" column="5" selection-start-line="594" selection-start-column="5" selection-end-line="594" selection-end-column="5" />
</state>
</provider>
</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="299">
<caret line="13" column="22" selection-start-line="13" selection-start-column="22" selection-end-line="13" selection-end-column="22" />
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/main.go">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="202">
<caret line="92" column="46" selection-start-line="92" selection-start-column="46" selection-end-line="92" selection-end-column="46" />
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/README.md">
<provider selected="true" editor-type-id="split-provider[text-editor;markdown-preview-editor]">
<state split_layout="SPLIT">
<first_editor />
<second_editor />
</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="248">
<caret line="78" column="19" selection-start-line="78" selection-start-column="19" selection-end-line="78" selection-end-column="19" />
<folding>
<element signature="e#20#97#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://C:/Go/src/strconv/atoi.go">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="187">
<caret line="201" column="5" selection-start-line="201" selection-start-column="5" selection-end-line="201" selection-end-column="5" />
</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="2093">
<caret line="116" column="18" selection-start-line="116" selection-start-column="18" selection-end-line="116" selection-end-column="18" />
<state relative-caret-position="484">
<caret line="22" column="11" selection-start-line="22" selection-start-column="11" selection-end-line="22" selection-end-column="11" />
</state>
</provider>
</entry>
@ -77,8 +131,8 @@
<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="989">
<caret line="50" column="5" selection-start-line="50" selection-start-column="5" selection-end-line="50" selection-end-column="5" />
<state relative-caret-position="946">
<caret line="50" column="61" selection-start-line="50" selection-start-column="61" selection-end-line="50" selection-end-column="61" />
<folding>
<element signature="e#15#109#0" expanded="true" />
</folding>
@ -86,11 +140,14 @@
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="true">
<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="115">
<caret line="5" selection-start-line="5" selection-end-line="5" />
<state relative-caret-position="616">
<caret line="32" column="8" selection-start-line="32" selection-start-column="8" selection-end-line="32" selection-end-column="8" />
<folding>
<element signature="e#15#68#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
@ -98,21 +155,12 @@
<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">
<state relative-caret-position="2772">
<caret line="135" column="22" selection-start-line="135" selection-start-column="22" selection-end-line="135" selection-end-column="22" />
</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="59179">
<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">
@ -122,24 +170,41 @@
</list>
</option>
</component>
<component name="GOROOT" path="C:\Go" />
<component name="FindInProjectRecents">
<findStrings>
<find>rootCertPool</find>
<find>clientCert</find>
</findStrings>
</component>
<component name="GOROOT" path="F:\程序\GOROOT" />
<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$/main.go" />
<option value="$PROJECT_DIR$/controller/blog.go" />
<option value="$PROJECT_DIR$/model/blog.go" />
<option value="$PROJECT_DIR$/model/model.go" />
<option value="$PROJECT_DIR$/config/config.go" />
<option value="$PROJECT_DIR$/main.go" />
<option value="$PROJECT_DIR$/vendor/github.com/go-sql-driver/mysql/utils.go" />
<option value="$PROJECT_DIR$/db/sqlManager.go" />
<option value="$PROJECT_DIR$/user.yaml" />
</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 name="JsBuildToolGruntFileManager" detection-done="true" sorting="DEFINITION_ORDER" />
<component name="JsBuildToolPackageJson" detection-done="true" sorting="DEFINITION_ORDER" />
<component name="JsGulpfileManager">
<detection-done>true</detection-done>
<sorting>DEFINITION_ORDER</sorting>
</component>
<component name="ProjectFrameBounds" extendedState="7">
<option name="x" value="-7" />
<option name="y" value="40" />
<option name="width" value="1294" />
<option name="height" value="1047" />
</component>
<component name="ProjectView">
<navigator proportions="" version="1">
@ -147,7 +212,17 @@
</navigator>
<panes>
<pane id="Scope" />
<pane id="ProjectPane" />
<pane id="ProjectPane">
<subPane>
<expand>
<path>
<item name="background" type="b2602c69:ProjectViewProjectNode" />
<item name="background" type="462c0819:PsiDirectoryNode" />
</path>
</expand>
<select />
</subPane>
</pane>
</panes>
</component>
<component name="PropertiesComponent">
@ -159,11 +234,11 @@
<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="last_opened_file_path" value="$PROJECT_DIR$/../damon" />
<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" />
<property name="settings.editor.selected.configurable" value="go.sdk" />
</component>
<component name="RunDashboard">
<option name="ruleStates">
@ -195,23 +270,22 @@
</recent_temporary>
</component>
<component name="ToolWindowManager">
<frame x="-8" y="32" width="1936" height="1056" extended-state="6" />
<editor active="true" />
<frame x="-8" y="32" width="2576" height="1056" extended-state="6" />
<layout>
<window_info content_ui="combo" id="Project" order="0" visible="true" weight="0.2524013" />
<window_info content_ui="combo" id="Project" order="0" visible="true" weight="0.11535402" />
<window_info id="Structure" order="1" side_tool="true" weight="0.25" />
<window_info id="Favorites" order="2" 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 active="true" anchor="bottom" id="Run" order="2" visible="true" weight="0.32915717" />
<window_info anchor="bottom" id="Debug" order="3" weight="0.3997722" />
<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="bottom" id="LuaCheck" order="7" />
<window_info anchor="bottom" id="Docker" order="8" show_stripe_button="false" />
<window_info anchor="bottom" id="Version Control" order="9" />
<window_info anchor="bottom" id="Database Changes" order="10" />
<window_info anchor="bottom" id="Database Changes" order="10" show_stripe_button="false" />
<window_info anchor="bottom" id="Terminal" order="11" />
<window_info anchor="bottom" id="Event Log" order="12" side_tool="true" />
<window_info anchor="right" id="Commander" internal_type="SLIDING" order="0" type="SLIDING" weight="0.4" />
@ -225,41 +299,26 @@
<component name="TypeScriptGeneratedFilesManager">
<option name="version" value="1" />
</component>
<component name="VcsContentAnnotationSettings">
<option name="myLimit" value="2678400000" />
</component>
<component name="editorHistoryManager">
<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$/controller/file.go">
<provider selected="true" editor-type-id="text-editor" />
</entry>
<entry file="file://$PROJECT_DIR$/controller/user.go">
<entry file="file://C:/Go/src/database/sql/sql.go" />
<entry file="file://C:/Go/src/strconv/atoi.go" />
<entry file="file://$PROJECT_DIR$/controller/middle/auth.go">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="2093">
<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://$PROJECT_DIR$/model/model.go">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="989">
<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://C:/Go/src/database/sql/sql.go">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="59179">
<caret line="2586" column="5" selection-start-line="2586" selection-start-column="5" selection-end-line="2586" selection-end-column="5" />
<state relative-caret-position="220">
<caret line="17" column="30" selection-start-line="17" selection-start-column="30" selection-end-line="17" selection-end-column="30" />
</state>
</provider>
</entry>
@ -271,48 +330,107 @@
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/db/db.go">
<entry file="file://$PROJECT_DIR$/controller/blog.go">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="285">
<caret line="135" column="22" selection-start-line="135" selection-start-column="22" selection-end-line="135" selection-end-column="22" />
<state relative-caret-position="792">
<caret line="43" column="10" selection-start-line="43" selection-start-column="10" selection-end-line="43" selection-end-column="10" />
</state>
</provider>
</entry>
<entry file="file://C:/Go/src/strconv/atoi.go">
<entry file="file://$PROJECT_DIR$/controller/user.go">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="187">
<caret line="201" column="5" selection-start-line="201" selection-start-column="5" selection-end-line="201" selection-end-column="5" />
<state relative-caret-position="2002">
<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://$PROJECT_DIR$/../gopkg.in/mgo.v2/session.go">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="3476">
<caret line="174" column="2" selection-start-line="174" selection-start-column="2" selection-end-line="174" selection-end-column="2" />
</state>
</provider>
</entry>
<entry file="file://F:/程序/GOROOT/src/crypto/x509/cert_pool.go">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="2354">
<caret line="111" column="19" selection-start-line="111" selection-start-column="19" selection-end-line="111" selection-end-column="19" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/config/config.go">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="352">
<caret line="21" column="26" selection-start-line="21" selection-start-column="26" selection-end-line="21" selection-end-column="26" />
</state>
</provider>
</entry>
<entry file="file://F:/程序/GOROOT/src/crypto/tls/tls.go">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="3630">
<caret line="177" column="5" selection-start-line="177" selection-start-column="5" selection-end-line="177" selection-end-column="5" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/vendor/github.com/go-sql-driver/mysql/utils.go">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="880">
<caret line="53" column="32" selection-start-line="53" selection-start-column="22" selection-end-line="53" selection-end-column="32" />
</state>
</provider>
</entry>
<entry file="file://F:/程序/GOROOT/src/database/sql/sql.go">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="12804">
<caret line="594" column="5" selection-start-line="594" selection-start-column="5" selection-end-line="594" selection-end-column="5" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/user.yaml">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="299">
<caret line="13" column="22" selection-start-line="13" selection-start-column="22" selection-end-line="13" selection-end-column="22" />
<state relative-caret-position="484">
<caret line="22" column="11" selection-start-line="22" selection-start-column="11" selection-end-line="22" selection-end-column="11" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/main.go">
<entry file="file://$PROJECT_DIR$/model/model.go">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="202">
<caret line="92" column="46" selection-start-line="92" selection-start-column="46" selection-end-line="92" selection-end-column="46" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/controller/blog.go">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="248">
<caret line="78" column="19" selection-start-line="78" selection-start-column="19" selection-end-line="78" selection-end-column="19" />
<state relative-caret-position="946">
<caret line="50" column="61" selection-start-line="50" selection-start-column="61" selection-end-line="50" selection-end-column="61" />
<folding>
<element signature="e#20#97#0" expanded="true" />
<element signature="e#15#109#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/model/blog.go">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="115">
<caret line="5" selection-start-line="5" selection-end-line="5" />
<state relative-caret-position="616">
<caret line="32" column="8" selection-start-line="32" selection-start-column="8" selection-end-line="32" selection-end-column="8" />
<folding>
<element signature="e#15#68#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/db/db.go">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="2772">
<caret line="135" column="22" selection-start-line="135" selection-start-column="22" selection-end-line="135" selection-end-column="22" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/db/sqlManager.go">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="395">
<caret line="36" column="20" selection-start-line="36" selection-start-column="20" selection-end-line="36" selection-end-column="20" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/main.go">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="418">
<caret line="44" column="41" selection-start-line="44" selection-start-column="41" selection-end-line="44" selection-end-column="41" />
</state>
</provider>
</entry>

View File

@ -20,7 +20,8 @@ type ConfAPI struct {
Redis1 EntityRedis `yaml:"redis1"`
Mysql MysqlConfig `yaml:"mysql"` // 认证配置
Mysql1 MysqlConfig `yaml:"mysql1"` // 认证配置
MongoConf MongoConfig `yaml:"mongo"`
CaCert string `yaml:"ca_cert"`
init bool
}
@ -49,6 +50,16 @@ type MysqlConfig struct {
MaxOpen int `yaml:"max_open"`
MaxIdle int `yaml:"max_idle"`
}
// mongodb://myuser:mypass@localhost:40001
type MongoConfig struct {
Addr string `yaml:"addr"`
Port int64 `yaml:"port"`
UserName string `yaml:"user"`
Password string `yaml:"password"`
Db string `yaml:"db"`
MaxOpen int `yaml:"max_open"`
MaxIdle int `yaml:"max_idle"`
}
var gConf ConfAPI
@ -90,6 +101,16 @@ func GetMysqlBlogConfig() *MysqlConfig {
return nil
}
}
func GETCaCert() string{
return gConf.CaCert
}
func GetMongoDBConfig() *MongoConfig{
if gConf.init{
return &gConf.MongoConf
} else {
return nil
}
}
func GetRedis1() *EntityRedis {
if gConf.init {
return &gConf.Redis1
@ -104,3 +125,4 @@ func GetLogConfig() *LogConfig {
return nil
}
}

View File

@ -1,38 +1,115 @@
package db
import (
"background/config"
"background/logs"
"crypto/tls"
"crypto/x509"
"database/sql"
"fmt"
"github.com/go-sql-driver/mysql"
_ "github.com/go-sql-driver/mysql"
"background/config"
"gopkg.in/mgo.v2"
"io/ioutil"
"log"
"os"
)
var gDb Database
var blogDb Database
var gMongo *mgo.Session
func Init() {
mysqlconf := config.GetMysqlConfig()
blogConf := config.GetMysqlBlogConfig()
gDb = Database{Type: string(""), DB: initMysql(mysqlconf)}
InitMongoDb()
gDb = Database{Type: string(""), DB: initMysqlTLS(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,
cnn := fmt.Sprintf("%s:%s@tcp(%s:3306)/%s?charset=utf8", mysqlconf.UserName, mysqlconf.Password,
mysqlconf.Addr, mysqlconf.Db)
log.Print("Connect to mysql " + cnn)
_db, err := sql.Open("mysql", cnn)
if err != nil {
fmt.Println("connect sql server ", err.Error())
os.Exit(200)
}
e := _db.Ping()
if nil != e {
fmt.Println(e.Error())
os.Exit(200)
}
return _db
}
func initMysqlTLS(mysqlconf *config.MysqlConfig) *sql.DB {
rootCertPool := x509.NewCertPool()
pem, err := ioutil.ReadFile("D:\\data\\腾讯云测试服mysql证书\\ca.pem")
if err != nil {
log.Fatal(err)
}
if ok := rootCertPool.AppendCertsFromPEM(pem); !ok {
log.Fatal("Failed to append PEM.")
}
clientCert := make([]tls.Certificate, 0, 1)
certs, err := tls.LoadX509KeyPair("D:\\data\\腾讯云测试服mysql证书\\client-cert.pem", "D:\\data\\腾讯云测试服mysql证书\\client-key.pem")
if err != nil {
log.Fatal(err)
}
clientCert = append(clientCert, certs)
mysql.RegisterTLSConfig("custom", &tls.Config{
RootCAs: rootCertPool,
Certificates: clientCert,
InsecureSkipVerify: true,
})
cnn := fmt.Sprintf("%s:%s@tcp(%s:3306)/%s?charset=utf8&tls=custom", mysqlconf.UserName, mysqlconf.Password,
mysqlconf.Addr, mysqlconf.Db)
log.Print("Connect to mysql " + cnn)
_db, err := sql.Open("mysql", cnn)
if err != nil {
fmt.Println("connect sql server ", err.Error())
os.Exit(200)
}
e := _db.Ping()
if nil != e {
fmt.Println(e.Error())
os.Exit(200)
}
return _db
}
func initMongoDb(conf *config.MongoConfig) *mgo.Session{
url := fmt.Sprintf("mongodb://%s:%s@%s:%d/%s",conf.UserName,conf.Password,conf.Addr,conf.Port,conf.Db)
log.Print("connect to url " + url)
logs.Debug("connect to url " + url)
mgo,err := mgo.Dial(url)
if nil != err{
logs.Error(err.Error())
return nil
}
return mgo
}
func GetMysqlClient() *Database {
return &gDb
}
func GetBlogMysql() *Database{
return &blogDb
}
func GetMongoDb() *mgo.Session {
return gMongo
}
func InitMongoDb() {
mongoConf := config.GetMongoDBConfig()
gMongo = initMongoDb(mongoConf)
if gMongo == nil{
log.Print("error mongo initial")
os.Exit(25)
}
}

View File

@ -64,6 +64,9 @@ func main() {
InitRedis()
InitMysql()
mgo := db.GetMongoDb()
mgo = mgo
config.GetMongoDBConfig()
r := gin.Default()
store := sessions.NewCookieStore([]byte("secret123"))
r.Use(sessions.Middleware("sess_store", store))

View File

@ -31,9 +31,9 @@ func GetArticlesType() []ArticleType{
return ret
}
func CreateDoc(doc Doc) error {
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.GetBlogMysql().Query(sql)
sql := fmt.Sprintf(`insert into doc(title,type,content,author) values ('%s','%d','%s','%s')
on duplicate key update title = '%s',type = '%d',content = '%s',author = '%s';`,doc.Title,doc.Type,doc.Content,doc.Author,doc.Title,doc.Type,doc.Content,doc.Author)
_,e := db.GetMysqlClient().Query(sql)
if nil != e{
logs.Error(e.Error())
return e

View File

@ -18,7 +18,7 @@ type Users struct {
DisplayName string `sql:"display_name" json:"display_name"`
EmailAddress string `sql:"email_address" json:"email_address"`
Tel string `sql:"tel" json:"tel"`
Avatar string `sql:"Avatar" json:"avatar"`
Avatar string `sql:"avatar" json:"avatar"`
}
func GetUsers(limit int32, offsetPage int32, name string) ([]Users, int32) {

View File

@ -12,16 +12,43 @@ redis1:
db: 1
mysql:
addr: 118.24.238.198
user: server
password: server
user: caiyu
password: 123456
db: background
max_open: 100
MaxIdle: 99
mysql2:
addr: 35.240.179.248
mysql1:
addr: 127.0.0.1
user: root
password: a7458969
password:
db: blog
max_open: 100
MaxIdle: 99
mongo:
addr: 118.24.238.198
port: 27017
user: root
password: root
db: test
max_open: 100
MaxIdle: 99
ca_cert: "
-----BEGIN CERTIFICATE-----
MIIDAzCCAeugAwIBAgIBATANBgkqhkiG9w0BAQsFADA8MTowOAYDVQQDDDFNeVNR
TF9TZXJ2ZXJfNS43LjIzX0F1dG9fR2VuZXJhdGVkX0NBX0NlcnRpZmljYXRlMB4X
DTE5MDYwNDE2NDE1N1oXDTI5MDYwMTE2NDE1N1owPDE6MDgGA1UEAwwxTXlTUUxf
U2VydmVyXzUuNy4yM19BdXRvX0dlbmVyYXRlZF9DQV9DZXJ0aWZpY2F0ZTCCASIw
DQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAJjj52pBNXVoBFTiE72puVNcxGAO
b23PLrjA1ezkLJLDhZ0OVhNviwxeRGItlhSR7fubXI3IWd/vQ2xFlFwJISar91e6
dUT8D+j2nXBcZhAzXRvghSlBwm9hiFpTWfcfMK3JqqSknCtTvFfkdqioabJ0evMO
CCjtjA17rHi8UJa5EU+W0kBOFIKRQmkjFTB94zdvzcBmIVk5pPXdum+JWX25PGc9
F+7x4NJ2TsChuA5Pt460+Q8yyzR1LBECZHSbk9AGzh1zR6qR2qVPvSXtOpeVezDq
f5iBPiqYHl9sbpZ73jjm4IY3PDsBdsKGoucmRy+wfARo3qmnLm6ejtglwf8CAwEA
AaMQMA4wDAYDVR0TBAUwAwEB/zANBgkqhkiG9w0BAQsFAAOCAQEAPz5Emud9fGJv
rBWEVLVtSYTLV6zfmV0iHP/ABU4J/460u6Rw+yAwuSvJEZy5h7/WkZ4t3sxBCu5i
OZ0JdiJWZLOVflBBSGQ0DJEyjiP4bqWHljwf4KW7Q8UTv1rBmSz+kgnNuEdlQx4F
TyEd/URi1888h1M/IjSw+5NN5xb8Zs+j9flEFpS/l33WBhRPeCBBTXqZFeOflqCq
s04QNvfpr91LHG7oQ0bzpkYop4tMaDC7F6nyOZnTBBkZGDcSkCYdIEzYxuYUuyp2
OaN8tRmoZsleJaKgsrTV7owbOP8nShp/GYJnUc5qG74Xtb3CTYx43tkPgFSmR+2/
VAYNsNQ4mw==
-----END CERTIFICATE-----"