Skip to content

Commit

Permalink
代码格式, cache 工具包
Browse files Browse the repository at this point in the history
  • Loading branch information
moocss committed Sep 25, 2020
1 parent 6ffa13a commit e96f96d
Show file tree
Hide file tree
Showing 55 changed files with 1,543 additions and 422 deletions.
8 changes: 8 additions & 0 deletions README_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,12 @@

```bash

```

### ginhttp
```
s := ginhttp.NewServer(ginhttp.Addr(":4000))
s.AddBeforeServerStartFunc(bs.InitPprof(), bs.InitExpvar())
s.AddAfterServerStopFunc(bs.CloseLogger())
s.Serve();
```
6 changes: 3 additions & 3 deletions RESTClient.http
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
### 用户查询
GET http://localhost:9090/user HTTP/1.1
GET https://localhost:4000/user HTTP/1.1
content-type: application/json
Authorization: eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MiwidXNlcm5hbWUiOiJhZG1pbiIsImlhdCI6MTU4ODA1MzA3MCwiZXhwIjoxNTg4MTM5NDcwfQ.gO0bQfXQilekso0s3L_JXWPCXQNSLZQRc0OsE7qSAn2BjvptuWFbXrFuJ9Hht3qPmIihTX-Ab1bskC-DRUrHWVHJvrWbyMVMnbd5XnbOdxCnwviRXaIDEGAJrAX2CkYCq6q9WDuMDrxl2S6FVGjXNt8JZiW6fsPfFdakV2Z_rh5EAkzWGMTWNP7ip-LqpBXuldY1nzwI14sCxMt4LonH_XR_Q9MCylD4eHgOzBV7bNbRE-4cj0Cig9zD2vS9Wc-yhZfnmr5MN4ggleSgpb81LDN3yWgsJCUGwG07L7wiqOLcXEA2OWVVK99VGPPnWqUVL1k2SEwcjIbMzoUkR_MU-w

### 用户登录
POST http://localhost:9090/user/login HTTP/1.1
POST https://localhost:4000/user/login HTTP/1.1
content-type: application/json

{
Expand All @@ -14,7 +14,7 @@ content-type: application/json


### 用户注册
POST http://localhost:9090/user/register HTTP/1.1
POST https://localhost:4000/user/register HTTP/1.1
content-type: application/json

{
Expand Down
7 changes: 4 additions & 3 deletions app/conf/config.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package conf

import (
"github.com/go-impatient/gaia/pkg/conf"
"github.com/pkg/errors"
"github.com/spf13/viper"

"github.com/go-impatient/gaia/pkg/conf"
)

// Config ...
Expand Down Expand Up @@ -48,9 +49,9 @@ func InitConfig(name string, format string) (*ConfigTpl, error) {
MailConfig = NewMailConfig(mail)

// 缓存配置
cache := viper.Sub("cache")
cache := viper.Sub("xcache")
if cache == nil {
return nil, errors.New("No found `cache` in the configuration")
return nil, errors.New("No found `xcache` in the configuration")
}
CacheConfig = NewCacheConfig(cache)

Expand Down
15 changes: 8 additions & 7 deletions app/conf/options.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package conf

import (
"github.com/spf13/viper"
"time"

"github.com/spf13/viper"
)

// App ...
type App struct {
Mode string `json:"mode" yaml:"mode"` // "dev" | "prod" | "test"
Mode string `json:"mode" yaml:"mode"` // "dev" | "prod" | "test"
Grace bool `json:"grace" yaml:"grace"` // 默认 false, 可选 pprof 为空不使用
Host string `json:"host" yaml:"host"`
Port int `json:"port" yaml:"port"`
ReadTimeout time.Duration `json:"read_timeout" yaml:"read_timeout"`
Expand All @@ -21,16 +23,15 @@ type App struct {

// TLS ...
type TLS struct {
Port int `json:"port" yaml:"port"`
CertPath string `json:"cert_path" yaml:"cert_path"`
KeyPath string `json:"key_path" yaml:"key_path"`
}

// AutoTLS ...
type AutoTLS struct {
Enabled bool `json:"enabled" yaml:"enabled"` //Automatically install TLS certificates from Let's Encrypt.
Enabled bool `json:"enabled" yaml:"enabled"` // Automatically install TLS certificates from Let's Encrypt.
Folder string `json:"folder" yaml:"folder"` // folder for storing TLS certificates
Host string `json:"host" yaml:"string"` // which domains the Let's Encrypt will attempt
Host string `json:"host" yaml:"string"` // invalid hosts will be silently ignored.
}

// DB ...
Expand Down Expand Up @@ -86,20 +87,20 @@ type ConfigTpl struct {
Database *DB `json:"database" yaml:"database"`
Log *Log `json:"log" yaml:"log"`
Mail *Mail `json:"mail" yaml:"mail"`
Cache *Cache `json:"cache" yaml:"cache"`
Cache *Cache `json:"xcache" yaml:"xcache"`
}

// NewAppConfig ...
func NewAppConfig(cfg *viper.Viper) *App {
return &App{
Grace: cfg.GetBool("grace"),
Mode: cfg.GetString("mode"),
Host: cfg.GetString("host"),
Port: cfg.GetInt("port"),
ReadTimeout: cfg.GetDuration("read_timeout"),
WriteTimeout: cfg.GetDuration("write_timeout"),
IdleTimeout: cfg.GetDuration("idle_timeout"),
TLS: &TLS{
Port: cfg.GetInt("tls.port"),
CertPath: cfg.GetString("tls.cert_path"),
KeyPath: cfg.GetString("tls.key_path"),
},
Expand Down
4 changes: 3 additions & 1 deletion app/handler/user.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package handler

import (
"net/http"

"github.com/gin-gonic/gin"

"github.com/go-impatient/gaia/internal/service"
"net/http"
)

// UserHandler ...
Expand Down
5 changes: 3 additions & 2 deletions app/middleware/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package middleware

import (
"fmt"
"github.com/go-impatient/gaia/internal/model"
"github.com/go-playground/validator/v10"
"net/http"
"strings"
"unicode"

"github.com/gin-gonic/gin"

"github.com/go-impatient/gaia/internal/model"
"github.com/go-playground/validator/v10"
)

// Handler creates a gin middleware for handling errors.
Expand Down
2 changes: 1 addition & 1 deletion app/middleware/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
// NoCache is a middleware function that appends headers
// to prevent the client from caching the HTTP response.
func NoCache(c *gin.Context) {
c.Header("Cache-Control", "no-cache, no-store, max-age=0, must-revalidate, value")
c.Header("Cache-Control", "no-xcache, no-store, max-age=0, must-revalidate, value")
c.Header("Expires", "Thu, 01 Jan 1970 00:00:00 GMT")
c.Header("Last-Modified", time.Now().UTC().Format(http.TimeFormat))
c.Next()
Expand Down
8 changes: 5 additions & 3 deletions app/router/router.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package router

import (
"net/http"

"github.com/gin-gonic/gin"

"github.com/go-impatient/gaia/app/handler"
sdHandle "github.com/go-impatient/gaia/app/handler/sd"
"github.com/go-impatient/gaia/app/middleware"
"github.com/go-impatient/gaia/internal/model"
"github.com/go-impatient/gaia/internal/service"
"net/http"
)

func rootHandler(c *gin.Context) {
Expand All @@ -27,8 +29,8 @@ func NotFound() gin.HandlerFunc {
}
}

// NewRouter ...
func NewRouter(router *gin.Engine, services *service.Services) {
// RegisterRoutes ...
func RegisterRoutes(router *gin.Engine, services *service.Services) {
// 使用中间件.
router.Use(gin.Logger())
router.Use(gin.Recovery())
Expand Down
3 changes: 2 additions & 1 deletion cmd/gaia/create/cmd.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package create

import (
"github.com/urfave/cli/v2"
"log"

"github.com/urfave/cli/v2"
)

var Cmd = &cli.Command{
Expand Down
10 changes: 6 additions & 4 deletions cmd/gaia/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package main

import (
"fmt"
"github.com/go-impatient/gaia/cmd/gaia/create"
"github.com/go-impatient/gaia/cmd/gaia/server"
"github.com/urfave/cli/v2"
"os"
"time"

"github.com/urfave/cli/v2"

"github.com/go-impatient/gaia/cmd/gaia/create"
"github.com/go-impatient/gaia/cmd/gaia/server"
)

const Version = "v1.0.0"
Expand All @@ -26,7 +28,7 @@ func run() {
app.Version = Version
app.Compiled = time.Now()
app.Authors = []*cli.Author{
&cli.Author{
{
Name: "moocss",
Email: "moocss@gmail.com",
},
Expand Down
42 changes: 25 additions & 17 deletions cmd/gaia/server/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ package server
import (
"errors"
"fmt"
"path"

"github.com/rs/zerolog/log"
"github.com/urfave/cli/v2"
"golang.org/x/sync/errgroup"

"github.com/go-impatient/gaia/app/conf"
"github.com/go-impatient/gaia/app/router"
"github.com/go-impatient/gaia/internal/repository"
"github.com/go-impatient/gaia/internal/service"
"github.com/go-impatient/gaia/pkg/http/ginhttp"
"github.com/go-impatient/gaia/pkg/logger"
"github.com/rs/zerolog/log"
"github.com/urfave/cli/v2"
"golang.org/x/sync/errgroup"
"path"
)

var flags = []cli.Flag{
Expand Down Expand Up @@ -49,7 +51,7 @@ var Cmd = &cli.Command{

// 运行服务
func start(c *cli.Context) error {
var group errgroup.Group
// var group errgroup.Group
fileName := c.String("config")
if len(fileName) == 0 {
return errors.New("server s -c ./../../config/config.json 或者 server start -config ./../../config/config.json")
Expand All @@ -76,6 +78,7 @@ func start(c *cli.Context) error {

// 3. 初始化数据库
sql := service.NewSQL()
defer sql.Close()

// 4. 初始化 repositories and services
userRepo := repository.NewUserRepository(sql.DB)
Expand All @@ -86,24 +89,29 @@ func start(c *cli.Context) error {

// 5. 初始化应用服务
serve := ginhttp.NewServer()
ginhttp.SetRuntimeMode(cfg.App.Mode)
// 5.1 初始化路由
g := serve.GetGinEngine()
router.NewRouter(g, s)
r := serve.Router()
router.RegisterRoutes(r, s)

// 5.2 启动服务
group.Go(func() error {
return serve.RunHTTPServer()
})
serve.Serve()
//group.Go(func() error {
// return serve.RunHTTPServer()
//})

// 5.3 健康检查
group.Go(func() error {
return serve.PingServer()
})
//group.Go(func() error {
// return serve.PingServer()
//})

if err := group.Wait(); err != nil {
log.Error().Msg(fmt.Sprintf("接口服务停止了:%v", err))
}
//if err := group.Wait(); err != nil {
// log.Error().Msg(fmt.Sprintf("接口服务停止了:%v", err))
//}
//
//return group.Wait()

return group.Wait()
return nil
}

// 编译和部署
Expand Down
14 changes: 7 additions & 7 deletions config/config.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
{
"app": {
"mode": "dev",
"grace": false,
"host": "0.0.0.0",
"port": 9090,
"port": 4000,
"read_timeout": "60s",
"write_timeout": "60s",
"idle_timeout": "60s",
"max_ping_count": 2,
"jwt_secret": "Rtg8BPKNEf2mB4mgvKONGPZZQSaJWNLijxR42qRgq0iBb5",
"tls": {
"port": 9098,
"cert_path": "",
"key_path": ""
"cert_path": "./../../config/gaia.crt",
"key_path": "./../../config/gaia.key"
},
"auto_tls": {
"enabled": false,
"folder": ".cache",
"host": ""
"enabled": true,
"folder": "/.cache",
"host": "example1.com"
}
},
"database": {
Expand Down
6 changes: 3 additions & 3 deletions config/config.yaml
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
app:
mode: "dev"
grace: false
host: "0.0.0.0"
port: 9090
port: 4000
read_timeout: "60s"
write_timeout: "60s"
idle_timeout: "60s"
max_ping_count: 2
jwt_secret: "Rtg8BPKNEf2mB4mgvKONGPZZQSaJWNLijxR42qRgq0iBb5"
tls:
port: 9098
cert_path: ""
key_path: ""
auto_tls:
enabled: false
folder: ".cache"
folder: ".xcache"
host: ""
database:
dialect: "mysql"
Expand Down
11 changes: 8 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,23 @@ require (
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect
github.com/allegro/bigcache v1.2.1 // indirect
github.com/asaskevich/govalidator v0.0.0-20200819183940-29e1ff8eb0bb
github.com/bradfitz/gomemcache v0.0.0-20190913173617-a41fca850d0b
github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
github.com/fsnotify/fsnotify v1.4.9
github.com/gin-gonic/gin v1.6.3
github.com/go-ole/go-ole v1.2.4 // indirect
github.com/go-playground/validator/v10 v10.3.0
github.com/go-redis/redis v6.15.9+incompatible // indirect
github.com/go-redis/redis/v7 v7.4.0
github.com/go-redis/redis/v8 v8.1.3
github.com/gofiber/fiber/v2 v2.0.1
github.com/golang/protobuf v1.4.2 // indirect
github.com/google/martian v2.1.0+incompatible
github.com/gorilla/schema v1.2.0 // indirect
github.com/jackc/pgproto3/v2 v2.0.4 // indirect
github.com/jpillora/overseer v1.1.6
github.com/json-iterator/go v1.1.10 // indirect
github.com/klauspost/compress v1.11.0 // indirect
github.com/magiconair/properties v1.8.2 // indirect
github.com/mitchellh/mapstructure v1.3.3 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
Expand All @@ -33,12 +38,12 @@ require (
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.7.1
github.com/stretchr/objx v0.3.0 // indirect
github.com/stretchr/testify v1.6.1 // indirect
github.com/stretchr/testify v1.6.1
github.com/urfave/cli/v2 v2.2.0
go.uber.org/dig v1.10.0
golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208
golang.org/x/sys v0.0.0-20200831180312-196b9ba8737a // indirect
golang.org/x/sys v0.0.0-20200916084744-dbad9cb7cb7a // indirect
golang.org/x/tools v0.0.0-20200904185747-39188db58858 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/protobuf v1.25.0 // indirect
Expand Down
Loading

0 comments on commit e96f96d

Please sign in to comment.