完整的GO工程

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
云数据库 RDS MySQL Serverless,价值2615元额度,1个月
简介:

www.toutiao.im

Intellij idea代码 收藏代码

mygo  
  |-bin  
  |-pkg  
  |-src  
    |-go.web.red  
      |-comm_log  
        comm_log.go  
      |-handler  
        red_handler.go  
        red_parameter_validate.go  
        user_handler.go  
      |-module  
        db_init.go  
        user_module.go  
      |-system_cfg  
        system_cfg_process.go  
      |-system_notify  
        system_notify_process.go  
      conf.ini  
      web.go  
   

comm_log.go

Java代码 收藏代码

package comm_log  
  
import (  
    "go.uber.org/zap"  
    "go.uber.org/zap/zapcore"  
    "time"  
)  
  
var default_log *Zlog  
  
type Zlog struct {  
    zap_log *zap.SugaredLogger  
    curr_log_level zap.AtomicLevel  
}  
  
func Init(program_name, log_level_str, log_path string) *Zlog {  
    encoder_cfg := zapcore.EncoderConfig{  
        TimeKey:        "T",  
        LevelKey:       "L",  
        NameKey:        "N",  
        CallerKey:      "C",  
        MessageKey:     "M",  
        StacktraceKey:  "S",  
        LineEnding:     zapcore.DefaultLineEnding,  
        EncodeLevel:    zapcore.CapitalLevelEncoder,  
        EncodeTime:     time_encoder,  
        EncodeDuration: zapcore.StringDurationEncoder,  
        EncodeCaller:   zapcore.ShortCallerEncoder,  
    }  
  
    log_level := zap.NewAtomicLevel()  
    log_level.UnmarshalText([]byte(log_level_str))  
  
    custom_cfg := zap.Config{  
        Level:            log_level,  
        Development:      true,  
        Encoding:         "console",  
        EncoderConfig:    encoder_cfg,  
        OutputPaths:      []string{log_path},  
        ErrorOutputPaths: []string{"stderr"},  
    }  
  
    logger, _ := custom_cfg.Build()  
    new_logger := logger.Named(program_name)  
    sugar := new_logger.Sugar()  
  
    default_log = &Zlog{  
        zap_log: sugar,  
        curr_log_level : log_level,  
    }  
  
    return default_log  
}  
  
func Sync()  {  
    default_log.zap_log.Sync()  
}  
  
func Debug(msg string, keysAndValues ...interface{}) {  
    default_log.zap_log.Debugw(msg, keysAndValues...)  
}  
  
func Info(msg string, keysAndValues ...interface{})  {  
    default_log.zap_log.Infow(msg, keysAndValues...)  
}  
  
func Warn(msg string, keysAndValues ...interface{}) {  
    default_log.zap_log.Warnw(msg, keysAndValues...)  
}  
  
func Error(msg string, keysAndValues ...interface{}) {  
    default_log.zap_log.Errorw(msg, keysAndValues...)  
}  
  
func Set_log_level(log_level string) {  
    default_log.curr_log_level.UnmarshalText([]byte(log_level))  
}  
  
func Get_log_level() string {  
    return default_log.curr_log_level.String()  
}  
  
func time_encoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) {  
    enc.AppendString("[" + t.Format("2006-01-02 15:04:05") + "]")  
}  

red_handler.go

Java代码 收藏代码

package handler  
  
import "github.com/labstack/echo"  
  
// http response status_code  
const(  
    SUCCESS         = "0"  
    DBERROR         = "1"  
    SERVERERROR     = "2"  
    REQUESTERROR    = "3"  
)  
  
const DATAFORMAT = "2006-01-02 15:04:05"  
  
type (  
    Base_req_entity struct {  
        TrackId     string  `validate:"lte_len=20"`  
    }  
  
    Base_res_entity struct {  
        Status_code     string `json:"status_code"`  
    }  
  
)  
type Red_handle interface {  
    Access_path() string  
    Support_method() []string  
    Handler(cont echo.Context) error  
}  
  
type Red_com_handle struct {  
    Path          string  
    Handler_works map[string]func(echo.Context)(error)  
}  
  
func (this *Red_com_handle)Access_path() string {  
    return this.Path  
}  
  
func (this *Red_com_handle)Support_method() []string {  
    sup_methods := []string{}  
    for method, _ := range this.Handler_works {  
        sup_methods = append(sup_methods, method)  
    }  
    return sup_methods  
}  
  
func (this *Red_com_handle)Handler(cont echo.Context) (ret error) {  
    return  this.Handler_works[cont.Request().Method](cont)  
}  

red_parameter_validate.go

Java代码 收藏代码

package handler  
  
import (  
    "github.com/go-playground/validator"  
    "reflect"  
    "strconv"  
    "unicode/utf8"  
    "fmt"  
)  
  
var Validate *validator.Validate  
  
func init()  {  
    Validate = validator.New()  
  
    Validate.RegisterValidation("lte_len", LteLengthOf)  
}  
  
func LteLengthOf(fl validator.FieldLevel) bool {  
  
    field := fl.Field()  
    param := fl.Param()  
  
    switch field.Kind() {  
  
    case reflect.String:  
        p, _ := strconv.ParseInt(param, 0, 64)  
  
        return int64(utf8.RuneCountInString(field.String())) <= p  
    }  
  
    panic(fmt.Sprintf("Bad field type %T", field.Interface()))  
}  

user_handler.go

Java代码 收藏代码

package handler  
  
import (  
    "github.com/labstack/echo"  
    "net/http"  
    "fmt"  
    "go.web.red/module"  
)  
  
var User_default *User_entity  
  
func init() {  
    User_default = &User_entity{  
        module: new(module.User),  
         Red_com_handle: Red_com_handle{  
            Path: "user",  
            Handler_works: make(map[string]func(echo.Context)(error)),  
        },  
    }  
  
    User_default.Handler_works[echo.GET] = User_default.get  
    User_default.Handler_works[echo.POST] = User_default.post  
    User_default.Handler_works[echo.DELETE] = User_default.delete  
    User_default.Handler_works[echo.PUT] = User_default.put  
}  
  
type User_entity struct {  
    Red_com_handle  
    module *module.User  
}  
  
type (  
    User_param struct {  
        Name     string         `json:"name" validate:"required,lte_len=50"`  
        Age      int                    `json:"age"`  
        Address  string                 `json:"address"`  
        Status   int            `json:"status"`  
        Created  string         `json:"create_time"`  
        Modified string         `json:"update_time"`  
    }  
  
    User_param_all struct {  
        User_param  
        Id      int     `json:"id" validate:"required,lte_len=10"`  
    }  
  
    User_get_req struct {  
        Base_req_entity  
        Id      int  
        Status      int  
    }  
  
    User_get_res struct {  
        Base_res_entity  
        Users []User_param_all `json:"users"`  
    }  
  
    User_post_req struct {  
        Base_req_entity  
        User_param  
    }  
  
    User_put_req struct {  
        Base_req_entity  
        User_param_all  
    }  
  
    User_delete_req struct {  
        Base_req_entity  
        Id  int `validate:"required"`  
    }  
)  
  
func (entity *User_entity) get(cont echo.Context) error {  
    req := User_get_req{  
        Base_req_entity: Base_req_entity{  
            TrackId: cont.QueryParam("trackId"),  
        },  
        Id: cont.QueryParam("id"),  
        Status: cont.QueryParam("status"),  
    }  
  
    res := User_get_res{}  
  
    if err := Validate.Struct(&req); err != nil {  
        res.Status_code = REQUESTERROR  
        return cont.JSON(http.StatusBadRequest, res)  
    }  
  
    queryParams := map[string]interface{}{}  
  
    queryParams["status"] = req.Status  
  
    users, err := entity.module.Query(req.TrackId, queryParams)  
  
    if err != nil {  
        res.Status_code = DBERROR  
        return cont.JSON(http.StatusBadRequest, res)  
    }  
  
    if len(users) == 0 {  
        res.Users = []User_param_all{}  
    } else {  
        for _, user := range users {  
            res.Users = append(res.Users, User_param_all{  
                User_param: User_param{  
                    Name: user.Name,  
                    Age: user.Age,  
                    Address: user.Address,  
                    Status: user.Status,  
                    Modified: user.Modified.Format(DATAFORMAT),  
                    Created: user.Created.Format(DATAFORMAT),  
                },  
                Id: user.Id,  
            })  
        }  
    }  
  
    res.Status_code = SUCCESS  
  
    return cont.JSON(http.StatusOK, res)  
}  
  
func (entity *User_entity) post(cont echo.Context) error {  
    req := new(User_post_req)  
    res := new(Base_res_entity)  
  
    if err := cont.Bind(req); err != nil {  
        fmt.Println(err.Error())  
        res.Status_code = REQUESTERROR  
        return cont.JSON(http.StatusBadRequest, res)  
    }  
  
    req.TrackId = cont.QueryParam("trackId")  
  
    if err := Validate.Struct(req); err != nil {  
        fmt.Println(err.Error())  
        res.Status_code = REQUESTERROR  
        return cont.JSON(http.StatusBadRequest, res)  
    }  
  
    user := module.User{  
        Name: req.Name,  
        Age: req.Age,  
        Address: req.Address,  
        Status: req.Status,  
    }  
  
    err := entity.module.Save(req.TrackId, user)  
  
    if err != nil {  
        fmt.Println(err.Error())  
        res.Status_code = DBERROR  
        return cont.JSON(http.StatusBadRequest, res)  
    }  
  
    res.Status_code = SUCCESS  
    return cont.JSON(http.StatusOK, res)  
}  
  
func (entity *User_entity) delete(cont echo.Context) error {  
    req := User_delete_req{  
        Base_req_entity: Base_req_entity{  
            TrackId: cont.QueryParam("trackId"),  
        },  
        Id: cont.QueryParam("id"),  
    }  
  
    res := Base_res_entity{}  
  
  
    if err := Validate.Struct(&req); err != nil {  
        res.Status_code = REQUESTERROR  
        return cont.JSON(http.StatusBadRequest, res)  
    }  
  
    err := entity.module.Delete(req.TrackId, req.Id)  
  
    if err != nil {  
        res.Status_code = DBERROR  
        return cont.JSON(http.StatusBadRequest, res)  
    }  
  
    res.Status_code = SUCCESS  
    return cont.JSON(http.StatusOK, res)  
}  
  
func (entity *User_entity) put(cont echo.Context) error {  
    req := new(User_put_req)  
    res := new(Base_res_entity)  
  
    if err := cont.Bind(req); err != nil {  
        fmt.Println(err.Error())  
        res.Status_code = REQUESTERROR  
        return cont.JSON(http.StatusBadRequest, res)  
    }  
  
    req.TrackId = cont.QueryParam("trackId")  
  
    if err := Validate.Struct(req); err != nil {  
        fmt.Println(err.Error())  
        res.Status_code = REQUESTERROR  
        return cont.JSON(http.StatusBadRequest, res)  
    }  
  
    user := module.User{  
        Id: req.Id,  
        Name: req.Name,  
        Age: req.Age,  
        Address: req.Address,  
        Status: req.Status,  
    }  
  
    err := entity.module.Update(req.TrackId, user)  
  
    if err != nil {  
        fmt.Println(err.Error())  
        res.Status_code = DBERROR  
        return cont.JSON(http.StatusBadRequest, res)  
    }  
  
    res.Status_code = SUCCESS  
    return cont.JSON(http.StatusOK, res)  
}  

db_init.go

Java代码 收藏代码

package module  
  
import (  
    "github.com/go-sql-driver/mysql"  
    "github.com/jinzhu/gorm"  
    "time"  
    "fmt"  
    "errors"  
    "go.web.red/system_cfg"  
)  
  
var Db_instance *gorm.DB  
  
var (  
    SAVE_AFFECTED_ZERO_ERROR = errors.New("save affected row 0")  
    UPDATE_AFFECTED_ZERO_ERROR = errors.New("update affected row 0")  
    DELETE_AFFECTED_ZERO_ERROR = errors.New("delete affected row 0")  
)  
  
const (  
    STATUS_NORMAL   = 1  
    STATUS_SUSPEND  = 2  
    STATUS_ABANDON  = 3  
)  
  
func init() {  
    ip,_ := system_cfg.System_cfg.GetValue("mysql", "ip")  
    port,_ := system_cfg.System_cfg.GetValue("mysql", "port")  
    user,_ := system_cfg.System_cfg.GetValue("mysql", "user")  
    pwd,_ := system_cfg.System_cfg.GetValue("mysql", "pwd")  
    db,_ := system_cfg.System_cfg.GetValue("mysql", "db")  
  
    dsn := mysql.Config{  
        Addr: ip+":"+port,  
        User: user,  
        Passwd: pwd,  
        Net: "tcp",  
        DBName: db,  
        Params: map[string]string{"charset": "utf8", "parseTime": "True", "loc": "Local"},  
        Timeout: time.Duration(5 * time.Second),  
    }  
  
    db, err := gorm.Open("mysql", dsn.FormatDSN())  
  
    if err != nil {  
        fmt.Println(err)  
        panic(err.Error())  
    }  
  
    Db_instance = db  
}  

user_module.go

Java代码 收藏代码

package module  
  
import (  
    "time"  
)  
  
type User struct {  
    Id          int `grom:"primary_key"`  
    Name        string `grom:"not null;unique"`  
    Age         int `grom:"not null"`  
    Address     string  
    Status      int `grom:"not null"`  
    Created     time.Time   `gorm:"column:created"`  
    Modified    time.Time   `gorm:"column:modified"`  
}  
  
func (user *User) Save(trackId string, user User) error {  
    res := Db_instance.Create(&user)  
  
    if res.Error != nil {  
        return res.Error  
    }  
  
    if res.RowsAffected == 0 {  
        return SAVE_AFFECTED_ZERO_ERROR  
    }  
  
    return nil  
}  
func (user *User) Update(trackId string, user User) error {  
    record := User{}  
    updates := map[string]interface{}{  
        "name": user.Name,  
        "age": user.Age,  
        "address": user.Address,  
        "status": user.Status,  
    }  
  
    res := Db_instance.Model(&record).Where("id=?", user.Id).Update(updates)  
  
    if res.Error != nil {  
        return res.Error  
    }  
  
    if res.RowsAffected == 0 {  
        return UPDATE_AFFECTED_ZERO_ERROR  
    }  
  
    return nil  
}  
func (user *User) Delete(trackId string, id int) error {  
    record := User{}  
  
    res := Db_instance.Where("id=?", id).Delete(record)  
  
    if res.Error != nil {  
        return res.Error  
    }  
  
    if res.RowsAffected == 0 {  
        return DELETE_AFFECTED_ZERO_ERROR  
    }  
  
    return nil  
}  
  
func (user *User) Query(trackId string, queryParams map[string]interface{}) ([]User, error) {  
    records := [] User{}  
  
    res := Db_instance.Where(queryParams).Find(&records)  
  
    if res.Error != nil {  
        return []User{}, res.Error  
    }  
  
    return records, nil  
}  

system_cfg_process.go

Java代码 收藏代码

package system_cfg  
  
import (  
    "github.com/Unknwon/goconfig"  
    "log"  
    "fmt"  
    "os"  
    "path/filepath"  
)  
  
var System_cfg *goconfig.ConfigFile  
  
func init()  {  
    fileAbsPath := filepath.Dir(os.Args[0])  
    fmt.Printf("current path = %s\n", fileAbsPath)  
    cfg, err := goconfig.LoadConfigFile(fileAbsPath + "/conf.ini")  
    if err != nil{  
        log.Fatalf("load config conf.ini error = %s", err)  
    }  
    System_cfg = cfg  
}  

system_notify_process.go

Java代码 收藏代码

package system_notify  
  
import (  
    "strings"  
    "github.com/go-gomail/gomail"  
    "html/template"  
    "bytes"  
    "os_adv_engine/system_cfg"  
    "strconv"  
    "fmt"  
    "encoding/json"  
    "net/http"  
    "io/ioutil"  
)  
  
type CsNotify interface {  
    SendNotify(string, string) bool  
}  
  
var notifyList []CsNotify  
  
func init() {  
    smtp_s_str, _ := system_cfg.System_cfg.GetValue("email_notify", "smtp_server")  
    smtp_p_str, _ := system_cfg.System_cfg.GetValue("email_notify", "smtp_port")  
    sender_str, _ := system_cfg.System_cfg.GetValue("email_notify", "sender")  
    passwd_str, _ := system_cfg.System_cfg.GetValue("email_notify", "passwd")  
  
    receivers := []string{}  
    receiversStr, _ := system_cfg.System_cfg.GetValue("email_notify", "receivers")  
    for _, receiverStr := range strings.Split(receiversStr, ";") {  
        receivers = append(receivers, strings.TrimSpace(receiverStr))  
    }  
  
    smtp_p_int, _ := strconv.Atoi(smtp_p_str)  
  
    en := &EmailNotify{  
        smtp_s: smtp_s_str,  
        smtp_p: smtp_p_int,  
        fromer: sender_str,  
        toers: receivers,  
        ccers: []string{},  
        e_user: strings.Split(sender_str, "@")[0],  
        e_passwd: passwd_str,  
    }  
    notifyList = append(notifyList, en)  
  
    ln := &LanxinNotify{  
        app_id:"",  
        app_secret:"",  
        host:"",  
        tousers:"",  
    }  
    notifyList = append(notifyList, ln)  
}  
  
func Notify(title string, content string) {  
    for _,value := range notifyList {  
        value.SendNotify(title, content)  
    }  
}  
  
type (  
    EmailNotify struct {  
        smtp_s string  
        smtp_p int  
        fromer string  
        toers  []string  
        ccers  []string  
        e_user string  
        e_passwd string  
    }  
  
    LanxinNotify struct {  
        app_id string  
        app_secret string  
        host string  
        tousers string  
    }  
)  
  
func (ln * LanxinNotify)SendNotify(title string, content string) bool {  
  
    return true  
}  
  
func (en *EmailNotify)SendNotify(title string, content string) bool {  
    msg := gomail.NewMessage()  
    msg.SetHeader("From", en.fromer)  
    msg.SetHeader("To", en.toers...)  
    msg.SetHeader("Subject", title)  
  
    msg.SetBody("text/html", en.renderNotify(content))  
  
    mailer := gomail.NewDialer(en.smtp_s, en.smtp_p, en.e_user, en.e_passwd)  
    if err := mailer.DialAndSend(msg); err != nil {  
        panic(err)  
    }  
    return true  
  
}  
  
func (en *EmailNotify) renderNotify(content string) string {  
    tplStr := `<html>  
<body>  
 {{.}}  
</table>  
</body>  
</html>`  
  
    outBuf := &bytes.Buffer{}  
    tpl := template.New("email notify template")  
    tpl, _ = tpl.Parse(tplStr)  
    tpl.Execute(outBuf, content)  
  
    return outBuf.String()  
}  
  
func httpGet(url string) string{  
    resp, err := http.Get(url)  
    if err != nil {  
        panic(err)  
    }  
    defer resp.Body.Close()  
    body, err := ioutil.ReadAll(resp.Body)  
    return fmt.Sprintf(string(body))  
}  

web.go

Java代码 收藏代码

package main  
  
import (  
    "github.com/labstack/echo"  
    "github.com/go-playground/validator"  
    "strings"  
    "log"  
    "go.web.red/red_handler"  
)  
  
type (  
    handle_entity struct {  
        h_func_set []string  
        handler func(echo.Context)(error)  
    }  
  
    handle_bundle struct {  
        root_url string  
        api_ver string  
        h_path_set map[string]handle_entity  
        validate *validator.Validate  
    }  
)  
  
var Default_hb *handle_bundle  
  
func init() {  
    Default_hb = &handle_bundle{  
        root_url: "/ads/api",  
        api_ver: "v1",  
        h_path_set: make(map[string]handle_entity),  
    }  
  
    var handlers []handler.Red_handle  
    handlers = append(handlers,  
        )  
}  
  
func main() {  
    es := echo.New()  
  
    for path, entity := range Default_hb.h_path_set {  
        paths := []string{Default_hb.root_url, Default_hb.api_ver, path}  
        full_path := strings.Join(paths, "/")  
        es.Match(entity.h_func_set, full_path, entity.handler)  
    }  
  
    log.Fatalln(es.Start(":8888"))  
}  

conf.ini

Xml代码 收藏代码

[email_notify]  
smtpsmtp_server = smtp.qq.com  
smtp_port = 25  
sender = xx@qq.com  
passwd =  
receivers = yy@qq.com;zz@qq.com  
  
[mysql]  
ip =  
port =  
user =  
pwd =  
db =  

代码见 http://git.toutiao.im

原文链接:[http://wely.iteye.com/blog/2382458]

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
7月前
|
测试技术 Go API
Go语言工程结构一文看懂 新手都需要知道
Go语言工程结构一文看懂 新手都需要知道
32 0
|
7月前
|
缓存 Go
Go语言依赖管理和工程结构完全攻略
Go语言依赖管理和工程结构完全攻略
26 0
|
8月前
|
机器学习/深度学习 安全 测试技术
Go语言进阶-工程进阶
Go语言进阶-工程进阶
Go语言进阶-工程进阶
|
JSON NoSQL 关系型数据库
go web 工程脚手架
go web 工程示例,可以作为一些项目工程结构参考,对一些常用功能做了简单封装。支持构建docker镜像。
307 0
|
Go Docker 容器
玩转Docker—使用Docker部署Go工程
玩转Docker—使用Docker部署Go工程
|
Go Docker 容器
玩转Docker—使用Docker部署Go工程
玩转Docker—使用Docker部署Go工程
204 0
|
1天前
|
存储 编译器 Go
Go语言学习12-数据的使用
【5月更文挑战第5天】本篇 Huazie 向大家介绍 Go 语言数据的使用,包含赋值语句、常量与变量、可比性与有序性
35 6
Go语言学习12-数据的使用