All Projects → hb-go → Echo Web

hb-go / Echo Web

Licence: mit
Go web framework Echo example. 在线演示☞迁移ing❌

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to Echo Web

Istio Micro
istio 微服务示例代码 grpc+protobuf+echo+websocket+mysql+redis+kafka+docker-compose
Stars: ✭ 194 (-52.57%)
Mutual labels:  echo, mysql
go-echo-server-sandbox
A scaffold of golang web server using labstack/echo
Stars: ✭ 12 (-97.07%)
Mutual labels:  echo, gorm
Goweibo
Go Weibo App
Stars: ✭ 243 (-40.59%)
Mutual labels:  echo, gorm
Logrus
Hooks for logrus logging
Stars: ✭ 110 (-73.11%)
Mutual labels:  echo, gorm
Echo Swagger
echo middleware to automatically generate RESTful API documentation with Swagger 2.0.
Stars: ✭ 314 (-23.23%)
Mutual labels:  echo, middleware
Phalgo
phalgo 已经更换为 https://github.com/sunmi-OS/gocore phalgo不在维护
Stars: ✭ 110 (-73.11%)
Mutual labels:  echo, gorm
go-webapp-sample
golang, echo, gorm
Stars: ✭ 194 (-52.57%)
Mutual labels:  echo, gorm
Cetus
Cetus is a high performance middleware that provides transparent routing between your application and any backend MySQL Servers.
Stars: ✭ 1,199 (+193.15%)
Mutual labels:  middleware, mysql
Yiigo
🔥 Go 轻量级开发通用库 🚀🚀🚀
Stars: ✭ 304 (-25.67%)
Mutual labels:  toml, mysql
Shineframe
高性能超轻量级C++开发库及服务器编程框架
Stars: ✭ 274 (-33.01%)
Mutual labels:  echo, mysql
Echo Logrus
echo-logrus is a middleware that provides logrus logger support for echo.
Stars: ✭ 27 (-93.4%)
Mutual labels:  echo, middleware
Go Project Sample
Introduce the best practice experience of Go project with a complete project example.通过一个完整的项目示例介绍Go语言项目的最佳实践经验.
Stars: ✭ 344 (-15.89%)
Mutual labels:  gorm, opentracing
My Review
主要存放平时理论学习,比如java jdk源码分析、并发理论;面试、数据库、Linux、中间件、分布式、网络协议等方向
Stars: ✭ 237 (-42.05%)
Mutual labels:  middleware, mysql
Go Http Metrics
Go modular http middleware to measure HTTP requests independent of metrics backend (with Prometheus and OpenCensus as backend implementations) and http framework/library
Stars: ✭ 128 (-68.7%)
Mutual labels:  echo, middleware
Bbs node
node后端服务,node+express+mysql, 搭建token-权限-管理完整的web服务, 对应页面: https://www.lyh.red/admin \ https://www.lyh.red/bbs \ 接口地址https://www.lyh.red/apidoc/index.html
Stars: ✭ 78 (-80.93%)
Mutual labels:  middleware, mysql
opentracing-gorm
OpenTracing instrumentation for GORM.
Stars: ✭ 46 (-88.75%)
Mutual labels:  gorm, opentracing
Gorm Bulk Insert
implement BulkInsert using gorm, just pass a Slice of Struct. Simple and compatible.
Stars: ✭ 241 (-41.08%)
Mutual labels:  gorm, mysql
Altair
Lightweight and Robust API Gateway written in Go
Stars: ✭ 34 (-91.69%)
Mutual labels:  middleware, mysql
echo-gorm-realworld-app
realworld application built with Golang + Echo + Gorm
Stars: ✭ 18 (-95.6%)
Mutual labels:  echo, gorm
Gopli
DB replication tool to synchronize data with multi environments written in Golang.
Stars: ✭ 327 (-20.05%)
Mutual labels:  toml, mysql

Echo Web

Slack

Go web framework Echo example.
masterv4版本,v3切换到v3分支。

Requires

Slack Slack Slack

Echo中文文档 go-echo.org

在线演示

目录

环境配置

1.源码下载
$ go get github.com:hb-go/echo-web
2.依赖安装
go mod

GFWgoproxy.io

Glide

glide工具安装

.glide/mirrors.yaml设置参考

# 编辑mirrors.yaml,或命令行glide mirror set [original] [replacement] --vcs [type]
$ vi ~/.glide/mirrors.yaml

# 查看glide mirror
$ glide mirror list

$ cd $GOPATH/src/github.com/hb-go
$ glide install
3.MySQL配置
# ./conf/conf.toml
[database]
name = "goweb_db"
user_name = "goweb_dba"
pwd  = "123456"
host = "127.0.0.1"
port = "3306"

# 测试数据库SQL脚本
./echo-web/res/db_structure.sql
4.Redis、Memcached配置,可选

可选需修改session、cache的store配置

  • session_store = "FILE"或"COOKIE"
  • cache_store = "IN_MEMORY"
# ./conf/conf.toml
[redis]
server = "127.0.0.1:6379"
pwd = "123456"

[memcached]
server = "localhost:11211"
5.子域名
# ./conf/conf.toml
[server]
addr = ":8080"
domain_api = "echo.api.localhost.com"
domain_web = "echo.www.localhost.com"
domain_socket = "echo.socket.localhost.com"

# 改host
$ vi /etc/hosts
127.0.0.1       echo.api.localhost.com
127.0.0.1       echo.www.localhost.com
127.0.0.1       echo.www.localhost.com

# Nginx配置,可选
server{
    listen       80;
    server_name  echo.www.localhost.com echo.api.localhost.com echo.socket.localhost.com;

    charset utf-8;

    location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host $host;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}
6.Bindata打包工具,可选(运行可选,打包必选)

Bindata安装

运行

# 首次运行必须带 -a -t,更新assets.go、template.go的资源路径为本地
$ ./run.sh [-a] [-t]        # -a -t 可选(须安装Bindata),以debug方式更新assets、template的Bindata资源包

# 浏览器访问
http://echo.www.localhost.com      # Nginx代理
http://echo.www.localhost.com:8080 # 无代理

# OpenTracing
http://localhost:8700/traces

打包

打包静态资源及模板文件须安装Bindata

# 打包
$ ./build.sh 		    # 默认本机
$ ./build.sh -l		    # 打包Linux平台

# 运行
$ ./echo-web -c conf/conf.toml      # -c指定配置文件,默认conf/conf.toml

目录结构

assets          Web服务静态资源
conf            项目配置
middleware      中间件
model           模型,数据库连接&ORM
  └ orm         ORM扩展
module          模块封装
  ├ auth        Auth授权
  ├ cache       缓存
  ├ log         日志
  ├ render      渲染
  ├ session     Session
  └ tmpl        Web模板
res             项目资源
  └ db          数据
router          路由
  └ api         接口路由
    ├context    自定义Context,便于扩展API层扩展
    └router     路由
  ├ socket      socket示范
  └ web         Web路由
    ├context    自定义Context,便于扩展Webb层扩展
    └router     路由        
template        模板
  └ pongo2      pongo2模板
util            公共工具
  ├ conv        类型转换
  ├ crypt       加/解密
  └ sql         SQL

框架功能

功能 描述
配置 toml配置文件
子域名部署 子域名区分模块
缓存 Redis、Memcached、Memory
Session Redis、File、Cookie,支持Flash
ORM Fork gormFirstSQLLastSQLFindSQLCountSQL支持构造查询SQL
查询缓存 支持FirstLastFindCount的查询缓存
模板 支持html/template(示例模板未维护)、pongo2,模板支持打包bindata
静态 静态资源,支持打包bindata
安全 CORS、CSRF、XSS、HSTS、验证码等
OpenTracing Tracer支持Jaeger、Appdash,在Request、ORM层做跟踪,可在conf配置开启)
监控 Prometheus Grafana Graphite InfluxDB
其他 JWT、Socket演示

Supervisord部署

# Max open files问题,修改supervisord配置
$ vi /etc/supervisor/supervisord.conf
[supervisord]
minfds = 10240

$ vi /etc/supervisor/conf.d/echo-web.conf
配置参考
[program:echo-web]
directory = /{path}/echo-web                                            ; 程序的启动目录
command =  /{path}/echo-web/echo-web -c /{path}/echo-web/conf/conf.toml ; 启动命令,与手动在命令行启动的命令是一样的
process_name=%(program_name)s                                           ; process_name expr (default %(program_name)s)
numprocs=1           ; number of processes copies to start (def 1)
autostart = true     ; 在 supervisord 启动的时候也自动启动
startsecs = 5        ; 启动 5 秒后没有异常退出,就当作已经正常启动了
autorestart = true   ; 程序异常退出后自动重启
startretries = 3     ; 启动失败自动重试次数,默认是 3
user = root          ; 用哪个用户启动
redirect_stderr = true          ; 把 stderr 重定向到 stdout,默认 false
stdout_logfile_maxbytes = 20MB  ; stdout 日志文件大小,默认 50MB
stdout_logfile_backups = 7      ; stdout 日志文件备份数
; stdout 日志文件,需要注意当指定目录不存在时无法正常启动,所以需要手动创建目录(supervisord 会自动创建日志文件)
stdout_logfile = /var/log/echo-web/stdout.log
; 这一配置项的作用是:如果supervisord管理的进程px又产生了若干子进程,使用supervisorctl停止px进程,停止信号会传播给px产生的所有子进程,确保子进程也一起停止。这一配置项对希望停止所有进程的需求是非常有用的。
stopasgroup=false
killasgroup=false
environment=GOGC=1000

Confd管理配置

# 安装confd
$ go get github.com/kelseyhightower/confd
# 将配置写入etcd,统一前缀echo-web
$ etcdctl ls --recursive --sort /echo-web
/echo-web/app
/echo-web/app/name
......
/echo-web/tmpl/suffix
/echo-web/tmpl/type

$ cd {pwd}/echo-web
$ confd -onetime -confdir conf  -backend etcd -node http://127.0.0.1:4001 -prefix echo-web

OpenTracing

在conf.toml开启或禁用

[opentracing]
disable = false

# jaeger or appdash
type = "jaeger"

# jaeger serviceName
service_name = "echo-web"

# jaeger-agent 127.0.0.1:6831
# appdash http://localhost:8700
address = "127.0.0.1:6831"

Metrics

在conf.toml开启或禁用

[metrics]
disable = false
freq_sec = 10
address = "127.0.0.1:2003"  # Graphite

Grafana

浏览器:localhost:3000

docker run --name grafana -d -p 3000:3000 grafana/grafana

Prometheus

浏览器:localhost:9090

docker run -d --name prometheus -p 9090:9090 -v ~/tmp/prometheus.yml:/etc/prometheus/prometheus.yml \
              prom/prometheus
              
# Dashboard JSON
# middleware/metrics/prometheus/grafana.json

更多参考middleware/metrics/README.md

Docker部署

Dockerfile含两种方式,源码方式镜像包偏大

# 创建镜像,注意修改配置
$ docker build -t hbchen/echo-web:v0.0.1 .

# 运行容器
$ docker run  \
     -p 8080:8080 \
     --name=echo-web \
     hbchen/echo-web:v0.0.1

*MySQL、Redis、Memcached等服务配置问题

# 如果是服务在宿主机需要配置服务IP为主机IP,127.0.0.1/localhost网络不通

# hbchen/echo-web使用的配置,在宿主机host上做个映射(192.168.1.8为主机IP)
# 192.168.1.8 mysql.localhost.com
# 192.168.1.8 redis.localhost.com
# 192.168.1.8 memcached.localhost.com
[database]
host = "mysql.localhost.com"

[redis]
server = "redis.localhost.com:6379"

[memcached]
server = "memcached.localhost.com:11211"
自动修改/etc/hosts,映射自定义域名到主机IP
$ vi ~/.profile

# 添加
grep -v "etcd.localhost.com\|consul.localhost.com\|mysql.localhost.com\|redis.localhost.com\|memcached.localhost.com" /etc/hosts > ~/hosts_temp
cat ~/hosts_temp > /etc/hosts
LC_ALL=C ifconfig en0 | grep 'inet ' | cut -d ' ' -f2 | awk '{print $1 " etcd.localhost.com\n" $1 " consul.localhost.com\n" $1 " mysql.localhost.com\n"$1     " redis.localhost.com\n" $1 " memcached.localhost.com"}' >> /etc/hosts

# 修改配置或切换网络时更新
$ source ~/.profile

pprof

import "github.com/hb-go/echo-web/middleware/pprof"

echo.Pre(pprof.Serve())
/*或*/
echo.Use(pprof.Serve())

Web浏览prof信息

http://{HOST}/debug/pprof/

google/pprof

$ go get -u github.com/google/pprof

$ pprof -http=[host]:[port] [main_binary] profile.pb.gz

# 使用echo-web演示
$ pprof -http=localhost:8080 --alloc_space http://echo.www.hbchen.com/debug/pprof/heap
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].