initial crm server
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
package initialize
|
||||
|
||||
import (
|
||||
"crm/global"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/schema"
|
||||
)
|
||||
|
||||
// 初始化MySQl数据库
|
||||
func Mysql() {
|
||||
m := global.Config.Mysql
|
||||
s := "%s:%s@tcp(%s:%v)/%s?charset=utf8&parseTime=True&loc=Local"
|
||||
var dsn = fmt.Sprintf(s, m.Username, m.Password, m.Host, m.Port, m.Dbname)
|
||||
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{
|
||||
NamingStrategy: schema.NamingStrategy{SingularTable: true},
|
||||
})
|
||||
if err != nil {
|
||||
fmt.Printf("mysql error: %s", err)
|
||||
return
|
||||
}
|
||||
sqlDb, err := db.DB()
|
||||
if err != nil {
|
||||
fmt.Printf("mysql error: %s", err)
|
||||
}
|
||||
sqlDb.SetMaxIdleConns(m.MaxIdleConns)
|
||||
sqlDb.SetMaxOpenConns(m.MaxOpenConns)
|
||||
sqlDb.SetConnMaxLifetime(time.Duration(m.ConnMaxLifetime))
|
||||
global.Db = db
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package initialize
|
||||
|
||||
import (
|
||||
"crm/global"
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
// 加载配置文件
|
||||
func LoadConfig() {
|
||||
viper.AddConfigPath("./")
|
||||
viper.SetConfigName("config")
|
||||
viper.SetConfigType("yaml")
|
||||
if err := viper.ReadInConfig(); err != nil {
|
||||
fmt.Printf("Fatal error resources file: %s \n", err.Error())
|
||||
}
|
||||
if err := viper.Unmarshal(&global.Config); err != nil {
|
||||
fmt.Printf("unable to decode into struct %s \n", err.Error())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package initialize
|
||||
|
||||
import (
|
||||
"crm/global"
|
||||
"fmt"
|
||||
|
||||
"github.com/go-redis/redis/v9"
|
||||
)
|
||||
|
||||
// 初始化Redis数据库
|
||||
func Redis() {
|
||||
r := global.Config.Redis
|
||||
rdb := redis.NewClient(&redis.Options{
|
||||
Addr: fmt.Sprintf("%s:%v", r.Host, r.Port),
|
||||
Password: r.Password,
|
||||
DB: r.Database,
|
||||
})
|
||||
global.Rdb = rdb
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package initialize
|
||||
|
||||
import (
|
||||
"crm/api"
|
||||
"crm/global"
|
||||
"crm/middleware"
|
||||
"fmt"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func Router() {
|
||||
engine := gin.Default()
|
||||
|
||||
// 开启跨域
|
||||
engine.Use(middleware.Cors())
|
||||
|
||||
route := engine.Group("/api")
|
||||
|
||||
{
|
||||
// 用户模块
|
||||
route.GET("/user/verifycode", api.NewUserApi().GetVerifyCode)
|
||||
route.GET("/user/info", api.NewUserApi().GetInfo)
|
||||
route.PUT("/user/mail", api.NewUserApi().UpdateMail)
|
||||
route.PUT("/user/buy", api.NewUserApi().Buy)
|
||||
route.POST("/user/login", api.NewUserApi().Login)
|
||||
route.POST("/user/register", api.NewUserApi().Register)
|
||||
route.POST("/user/pass", api.NewUserApi().ForgotPass)
|
||||
route.DELETE("/user/logout", api.NewUserApi().Logout)
|
||||
route.DELETE("/user/delete", api.NewUserApi().Delete)
|
||||
|
||||
// Jwt中间件
|
||||
route.Use(middleware.JwtAuth())
|
||||
|
||||
// 客户模块
|
||||
route.GET("/customer/list", api.NewCustomerApi().QueryList)
|
||||
route.GET("/customer/info", api.NewCustomerApi().QueryInfo)
|
||||
route.GET("/customer/option", api.NewCustomerApi().QueryOption)
|
||||
route.POST("/customer/create", api.NewCustomerApi().Create)
|
||||
route.PUT("/customer/update", api.NewCustomerApi().Update)
|
||||
route.DELETE("/customer/delete", api.NewCustomerApi().Delete)
|
||||
|
||||
// 合同模块
|
||||
route.GET("/contract/list", api.NewContractApi().QueryList)
|
||||
route.GET("/contract/info", api.NewContractApi().QueryInfo)
|
||||
route.GET("/contract/plist", api.NewContractApi().QueryPlist)
|
||||
route.PUT("/contract/update", api.NewContractApi().Update)
|
||||
route.POST("/contract/create", api.NewContractApi().Create)
|
||||
route.DELETE("/contract/delete", api.NewContractApi().Delete)
|
||||
|
||||
// 产品模块
|
||||
route.GET("/product/list", api.NewProductApi().QueryList)
|
||||
route.GET("/product/info", api.NewProductApi().QueryInfo)
|
||||
route.POST("/product/create", api.NewProductApi().Create)
|
||||
route.PUT("/product/update", api.NewProductApi().Update)
|
||||
route.DELETE("/product/delete", api.NewProductApi().Delete)
|
||||
|
||||
// 仪表盘模块
|
||||
route.GET("/dashboard/sum", api.NewDashboardApi().Summary)
|
||||
|
||||
}
|
||||
|
||||
// 启动、监听端口
|
||||
_ = engine.Run(fmt.Sprintf(":%v", global.Config.Server.Port))
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package initialize
|
||||
|
||||
func Run() {
|
||||
LoadConfig()
|
||||
Mysql()
|
||||
Redis()
|
||||
Router()
|
||||
}
|
||||
Reference in New Issue
Block a user