Compare commits

..

No commits in common. "main" and "v0.0.9" have entirely different histories.
main ... v0.0.9

10 changed files with 26 additions and 302 deletions

View File

@ -4,99 +4,5 @@
# 环境变量
| 环境变量 | 描述 |
| --- | --- |
| AEUS_DEBUG | 是否开启debug模式 |
| HTTP_PORT | http服务端口 |
| GRPC_PORT | grpc服务端口 |
| CLI_PORT | cli服务端口 |
# 快速开始
## 创建一个项目
创建项目可以使用`aeus`命令行工具进行生成:
```
aeus new github.com/your-username/your-project-name
```
如果需要创建一个带管理后台的应用, 可以使用`--admin`参数:
```
aeus new github.com/your-username/your-project-name --admin
```
## 生成`Proto`文件
服务使用`proto3`作为通信协议,因此需要生成`Proto`文件。
```
make proto
```
清理生成的文件使用:
```
make proto-clean
```
## 编译项目
编译项目可以使用`make`命令进行编译:
```
make build
```
# 目录结构
```
├── api
│ └── v1
├── cmd
│ ├── main.go
├── config
│ ├── config.go
│ └── config.yaml
├── deploy
│ └── docker
├── go.mod
├── go.sum
├── internal
│ ├── models
│ ├── scope
│ ├── service
├── Makefile
├── README.md
├── third_party
│ ├── aeus
│ ├── errors
│ ├── google
│ ├── openapi
│ ├── README.md
│ └── validate
├── vendor
├── version
│ └── version.go
├── web
└── webhook.yaml
```
| 目录 | 描述 |
| --- | --- |
| api | api定义目录 |
| cmd | 启动命令目录 |
| config | 配置目录 |
| deploy | 部署目录 |
| internal | 内部文件目录 |
| internal.service | 服务定义目录 |
| internal.models | 模型定义目录 |
| internal.scope | 服务scope定义目录,主要有全局的变量(比如DB,Redis等) |
| third_party | 第三方proto文件目录 |
| web | 前端资源目录 |

11
app.go
View File

@ -6,7 +6,6 @@ import (
"os/signal"
"reflect"
"runtime"
"strconv"
"sync/atomic"
"syscall"
"time"
@ -36,19 +35,12 @@ func (s *Service) Name() string {
}
func (s *Service) Debug() bool {
if s.opts != nil {
return s.opts.debug
}
return false
}
func (s *Service) Version() string {
if s.opts != nil {
return s.opts.version
}
return ""
}
func (s *Service) Metadata() map[string]string {
if s.service == nil {
return nil
@ -122,7 +114,7 @@ func (s *Service) injectVars(v any) {
}
func (s *Service) preStart(ctx context.Context) (err error) {
s.Logger().Info(ctx, "starting")
s.Logger().Info(s.ctx, "starting")
for _, ptr := range s.opts.servers {
s.refValues = append(s.refValues, reflect.ValueOf(ptr))
}
@ -263,7 +255,6 @@ func New(cbs ...Option) *Service {
registrarTimeout: time.Second * 30,
},
}
s.opts.debug, _ = strconv.ParseBool("AEUS_DEBUG")
s.opts.metadata = make(map[string]string)
for _, cb := range cbs {
cb(s.opts)

View File

@ -47,26 +47,13 @@ func WithAllow(paths ...string) Option {
if o.allows == nil {
o.allows = make([]string, 0, 16)
}
for _, s := range paths {
s = strings.TrimSpace(s)
if len(s) == 0 {
continue
}
o.allows = append(o.allows, s)
}
o.allows = append(o.allows, paths...)
}
}
func WithClaims(claims any) Option {
func WithClaims(claims reflect.Type) Option {
return func(o *options) {
if tv, ok := claims.(reflect.Type); ok {
o.claims = tv
} else {
o.claims = reflect.TypeOf(claims)
if o.claims.Kind() == reflect.Ptr {
o.claims = o.claims.Elem()
}
}
o.claims = claims
}
}
@ -78,20 +65,20 @@ func WithValidate(fn Validate) Option {
// isAllowed check if the path is allowed
func isAllowed(uripath string, allows []string) bool {
for _, pattern := range allows {
n := len(pattern)
if pattern == uripath {
for _, str := range allows {
n := len(str)
if n == 0 {
continue
}
if n > 1 && str[n-1] == '*' {
if strings.HasPrefix(uripath, str[:n-1]) {
return true
}
if pattern == "*" {
return true
}
if n > 1 && pattern[n-1] == '*' {
if strings.HasPrefix(uripath, pattern[:n-1]) {
if str == uripath {
return true
}
}
}
return false
}
@ -121,7 +108,9 @@ func JWT(keyFunc jwt.Keyfunc, cbs ...Option) middleware.Middleware {
return err
}
}
token, _ = strings.CutPrefix(token, bearerWord)
if strings.HasPrefix(token, bearerWord) {
token = strings.TrimPrefix(token, bearerWord)
}
var (
ti *jwt.Token
)

View File

@ -2,7 +2,6 @@ package aeus
import (
"context"
"maps"
"time"
"git.nobla.cn/golang/aeus/pkg/logger"
@ -19,7 +18,6 @@ type options struct {
servers []Server
endpoints []string
scope Scope
debug bool
registrarTimeout time.Duration
registry registry.Registry
serviceLoader ServiceLoader
@ -43,7 +41,9 @@ func WithMetadata(metadata map[string]string) Option {
if o.metadata == nil {
o.metadata = make(map[string]string)
}
maps.Copy(o.metadata, metadata)
for k, v := range metadata {
o.metadata[k] = v
}
}
}
@ -71,12 +71,6 @@ func WithScope(scope Scope) Option {
}
}
func WithDebug(debug bool) Option {
return func(o *options) {
o.debug = debug
}
}
func WithServiceLoader(loader ServiceLoader) Option {
return func(o *options) {
o.serviceLoader = loader

View File

@ -4,8 +4,6 @@ import (
"context"
"encoding/json"
"time"
"git.nobla.cn/golang/aeus"
)
type redisCache struct {
@ -59,12 +57,7 @@ func (c *redisCache) String() string {
}
func NewCache(opts ...Option) *redisCache {
cache := &redisCache{
return &redisCache{
opts: newOptions(opts...),
}
app := aeus.FromContext(cache.opts.context)
if app != nil {
cache.opts.prefix = app.Name() + ":" + cache.opts.prefix
}
return cache
}

View File

@ -1,14 +1,11 @@
package redis
import (
"context"
"github.com/redis/go-redis/v9"
)
type (
options struct {
context context.Context
client *redis.Client
prefix string
}
@ -22,12 +19,6 @@ func WithClient(client *redis.Client) Option {
}
}
func WithContext(ctx context.Context) Option {
return func(o *options) {
o.context = ctx
}
}
func WithPrefix(prefix string) Option {
return func(o *options) {
o.prefix = prefix

View File

@ -22,10 +22,6 @@ func (c *Context) Context() context.Context {
return c.ctx.Request.Context()
}
func (c *Context) Gin() *gin.Context {
return c.ctx
}
func (c *Context) Request() *http.Request {
return c.ctx.Request
}
@ -42,14 +38,6 @@ func (c *Context) Param(key string) string {
return c.ctx.Param(key)
}
func (c *Context) Query(key string) string {
qs := c.ctx.Request.URL.Query()
if qs != nil {
return qs.Get(key)
}
return ""
}
func (c *Context) Bind(val any) (err error) {
// if params exists, try bind params first
if len(c.ctx.Params) > 0 {

View File

@ -3,17 +3,13 @@ package http
import (
"context"
"fmt"
"io"
"net"
"net/http"
"net/http/pprof"
"net/url"
"os"
"path"
"path/filepath"
"slices"
"strconv"
"strings"
"sync"
"time"
@ -110,61 +106,14 @@ func (s *Server) Webroot(prefix string, fs http.FileSystem) {
s.fs.SetIndexFile("/index.html")
}
func (s *Server) shouldCompress(req *http.Request) bool {
if !strings.Contains(req.Header.Get(headerAcceptEncoding), "gzip") ||
strings.Contains(req.Header.Get("Connection"), "Upgrade") {
return false
}
// Check if the request path is excluded from compression
extension := filepath.Ext(req.URL.Path)
if slices.Contains(assetsExtensions, extension) {
return true
}
return false
}
func (s *Server) staticHandle(ctx *gin.Context, fp http.File) {
uri := path.Clean(ctx.Request.URL.Path)
fi, err := fp.Stat()
if err != nil {
return
}
if !fi.IsDir() {
//https://github.com/gin-contrib/gzip
if s.shouldCompress(ctx.Request) && fi.Size() > 8192 {
gzWriter := newGzipWriter()
gzWriter.Reset(ctx.Writer)
ctx.Header(headerContentEncoding, "gzip")
ctx.Writer.Header().Add(headerVary, headerAcceptEncoding)
originalEtag := ctx.GetHeader("ETag")
if originalEtag != "" && !strings.HasPrefix(originalEtag, "W/") {
ctx.Header("ETag", "W/"+originalEtag)
}
ctx.Writer = &gzipWriter{ctx.Writer, gzWriter}
defer func() {
if ctx.Writer.Size() < 0 {
gzWriter.Reset(io.Discard)
}
gzWriter.Close()
if ctx.Writer.Size() > -1 {
ctx.Header("Content-Length", strconv.Itoa(ctx.Writer.Size()))
}
putGzipWriter(gzWriter)
}()
}
}
http.ServeContent(ctx.Writer, ctx.Request, path.Base(uri), s.fs.modtime, fp)
ctx.Abort()
return
}
func (s *Server) notFoundHandle(ctx *gin.Context) {
if s.fs != nil && ctx.Request.Method == http.MethodGet {
uri := path.Clean(ctx.Request.URL.Path)
if fp, err := s.fs.Open(uri); err == nil {
s.staticHandle(ctx, fp)
http.ServeContent(ctx.Writer, ctx.Request, path.Base(uri), s.fs.modtime, fp)
fp.Close()
ctx.Abort()
return
}
}
ctx.JSON(http.StatusNotFound, newResponse(errors.NotFound, "Not Found", nil))

View File

@ -1,14 +1,8 @@
package http
import (
"bufio"
"compress/gzip"
"context"
"errors"
"io"
"net"
"net/http"
"sync"
"git.nobla.cn/golang/aeus/pkg/logger"
"github.com/gin-gonic/gin"
@ -47,77 +41,6 @@ type (
}
)
const (
headerAcceptEncoding = "Accept-Encoding"
headerContentEncoding = "Content-Encoding"
headerVary = "Vary"
)
var (
gzPool sync.Pool
assetsExtensions = []string{".css", ".js", ".png", ".jpg", ".jpeg", ".gif", ".svg", ".ico", ".woff", ".woff2", ".ttf", ".eot", ".otf"}
)
type gzipWriter struct {
gin.ResponseWriter
writer *gzip.Writer
}
func (g *gzipWriter) WriteString(s string) (int, error) {
g.Header().Del("Content-Length")
return g.writer.Write([]byte(s))
}
func (g *gzipWriter) Write(data []byte) (int, error) {
g.Header().Del("Content-Length")
return g.writer.Write(data)
}
func (g *gzipWriter) Flush() {
_ = g.writer.Flush()
g.ResponseWriter.Flush()
}
// Fix: https://github.com/mholt/caddy/issues/38
func (g *gzipWriter) WriteHeader(code int) {
g.Header().Del("Content-Length")
g.ResponseWriter.WriteHeader(code)
}
var _ http.Hijacker = (*gzipWriter)(nil)
// Hijack allows the caller to take over the connection from the HTTP server.
// After a call to Hijack, the HTTP server library will not do anything else with the connection.
// It becomes the caller's responsibility to manage and close the connection.
//
// It returns the underlying net.Conn, a buffered reader/writer for the connection, and an error
// if the ResponseWriter does not support the Hijacker interface.
func (g *gzipWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
hijacker, ok := g.ResponseWriter.(http.Hijacker)
if !ok {
return nil, nil, errors.New("the ResponseWriter doesn't support the Hijacker interface")
}
return hijacker.Hijack()
}
func newGzipWriter() (writer *gzip.Writer) {
v := gzPool.Get()
if v == nil {
writer, _ = gzip.NewWriterLevel(io.Discard, gzip.DefaultCompression)
} else {
if w, ok := v.(*gzip.Writer); ok {
return w
} else {
writer, _ = gzip.NewWriterLevel(io.Discard, gzip.DefaultCompression)
}
}
return
}
func putGzipWriter(writer *gzip.Writer) {
gzPool.Put(writer)
}
func WithNetwork(network string) Option {
return func(o *options) {
o.network = network