1、ginBlog 初始化项目

创建项目仓库拉取到本地

╰─➤ git clone https://gitee.com/GoProgect/ginBlog.git

# 使用go mod 管理项目
╰─➤  cd go/ginBlog
╰─➤ go mod init ginBlog

打开GoLand设置go mod及配置

GOPROXY=https://goproxy.cn,direct

image
image

安装gin

╰─➤ go get -u github.com/gin-gonic/gin

使用 ini 管理配置文件

# 文档地址
https://ini.unknwon.io/


# 安装
╰─➤ go get -u gopkg.in/ini.v1

设置程序参数配置文件

╰─➤  cat config/config.ini 

[server]
# "debug" 开发模式, "release" 线上模式
AppMode = debug
HttpPort = :9000

[database]
DbType = mysql
DbHost = 127.0.0.1
DbPort = 3306
DbUser = rootcat
DbPassword = ts123456
DbName = ginBlog

读取配置文件

╰─➤  cat utils/setting.go 
package utils

import (
        "fmt"
        "gopkg.in/ini.v1"
)

var (
        AppMode string
        HttpPort string

        DbType string
        DbHost string
        DbPort string
        DbUser string
        DbPassword string
        DbName string
)

func init()  {
        file, err := ini.Load("config/config.ini")
        if err != nil {
                fmt.Println("未读取到配置文件,检查文件路径查看文件是否存在:", err)
        }

        LoadServer(file)
        LoadDb(file)
}

func LoadServer(file *ini.File)  {
        AppMode = file.Section("server").Key("AppMode").MustString("debug")
        HttpPort = file.Section("server").Key("HttpPort").MustString(":9000")
}

func LoadDb(file *ini.File)  {
        DbType = file.Section("database").Key("DbType").MustString("mysql")
        DbHost = file.Section("database").Key("DbHost").MustString("127.0.0.1")
        DbPort = file.Section("database").Key("DbPort").MustString("3306")
        DbUser = file.Section("database").Key("DbUser").MustString("root")
        DbPassword = file.Section("database").Key("DbPassword").MustString("ts123456")
        DbName = file.Section("database").Key("DbName").MustString("ginBlog")
}

配置路由

╰─➤  cat routers/router.go 
package routers

import (
        "ginBlog/utils"
        "github.com/gin-gonic/gin"
        "net/http"
)

func InitRouter()  {
        gin.SetMode(utils.AppMode)
        r := gin.Default()

        routerV1 := r.Group("api/v1")
        {
                routerV1.GET("hello", func(c *gin.Context) {
                        c.JSON(http.StatusOK, gin.H{
                                "msg": "ok",
                        })
                })
        }

        r.Run(utils.HttpPort)
}

配置程序入口文件

╰─➤  cat main.go 
package main

import "ginBlog/routers"

func main()  {
        routers.InitRouter()
}


目录

╰─➤  tree ../ginBlog 
../ginBlog
├── LICENSE
├── README.en.md
├── README.md
├── api
├── config
│   └── config.ini
├── go.mod
├── go.sum
├── main.go
├── middleware
├── model
├── routers
│   └── router.go
├── upload
├── utils
│   └── setting.go
└── web


运行代码 或者Goland直接运行

go run main.go

image

浏览器访问

image

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值