-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
420 additions
and
318 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
|
||
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime" | ||
"github.com/movsb/taoblog/modules/utils" | ||
"github.com/movsb/taoblog/protocols/clients" | ||
"github.com/movsb/taoblog/protocols/go/proto" | ||
"google.golang.org/protobuf/encoding/protojson" | ||
) | ||
|
||
type _Protos struct { | ||
mux *runtime.ServeMux | ||
http.Handler | ||
} | ||
|
||
func New(ctx context.Context, client clients.Client) http.Handler { | ||
mux := runtime.NewServeMux( | ||
runtime.WithMarshalerOption( | ||
runtime.MIMEWildcard, | ||
&runtime.JSONPb{ | ||
MarshalOptions: protojson.MarshalOptions{ | ||
UseProtoNames: true, | ||
EmitUnpopulated: true, | ||
}, | ||
}, | ||
), | ||
) | ||
|
||
utils.Must(proto.RegisterUtilsHandlerClient(ctx, mux, client)) | ||
utils.Must(proto.RegisterTaoBlogHandlerClient(ctx, mux, client)) | ||
utils.Must(proto.RegisterSearchHandlerClient(ctx, mux, client)) | ||
|
||
return &_Protos{ | ||
mux: mux, | ||
Handler: mux, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package avatar | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"net/http" | ||
|
||
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime" | ||
"github.com/movsb/taoblog/modules/utils" | ||
"github.com/movsb/taoblog/protocols/go/proto" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
func New(server proto.TaoBlogServer) http.Handler { | ||
return &_Avatar{ | ||
server: server, | ||
} | ||
} | ||
|
||
type _Avatar struct { | ||
server proto.TaoBlogServer | ||
} | ||
|
||
// Params ... | ||
type Params struct { | ||
Headers http.Header | ||
} | ||
|
||
func (h *_Avatar) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
emailRsp, err := h.server.GetCommentEmailById( | ||
r.Context(), | ||
&proto.GetCommentEmailByIdRequest{ | ||
Id: int32(utils.MustToInt64(r.PathValue(`id`))), | ||
}, | ||
) | ||
if err != nil { | ||
http.Error(w, err.Error(), runtime.HTTPStatusFromCode(status.Code(err))) | ||
return | ||
} | ||
|
||
p := Params{ | ||
Headers: make(http.Header), | ||
} | ||
|
||
for _, name := range []string{ | ||
`If-Modified-Since`, | ||
`If-None-Match`, | ||
} { | ||
if h := r.Header.Get(name); h != "" { | ||
p.Headers.Add(name, h) | ||
} | ||
} | ||
|
||
// TODO 并没有限制获取未公开发表文章的评论。 | ||
rsp, err := github(context.TODO(), emailRsp.Email, &p) | ||
if err != nil { | ||
rsp, err = gravatar(context.TODO(), emailRsp.Email, &p) | ||
} | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
defer rsp.Body.Close() | ||
|
||
// TODO:内部缓存,只正向代理 body。 | ||
for _, k := range knownHeaders { | ||
if v := rsp.Header.Get(k); v != "" { | ||
w.Header().Set(k, v) | ||
} | ||
} | ||
|
||
// 客户端缓存失效了也可以继续用,后台慢慢刷新就行。 | ||
w.Header().Set(`Cache-Control`, `max-age=604800, stale-while-revalidate=604800`) | ||
|
||
w.WriteHeader(rsp.StatusCode) | ||
io.Copy(w, rsp.Body) | ||
} | ||
|
||
// 不再提供以下字段,官方更新太频繁,意义不大。 | ||
// `Expires`, | ||
// `Cache-Control`, | ||
var knownHeaders = []string{ | ||
`Content-Length`, | ||
`Content-Type`, | ||
`Last-Modified`, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
service/modules/webhooks/github_test.go → ...y/handlers/webhooks/github/github_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package webhooks | ||
package github | ||
|
||
import "testing" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package github | ||
|
||
import "net/http" | ||
|
||
func New(secret, reloaderPath string, sendNotify func(content string)) http.Handler { | ||
return handler(secret, reloaderPath, sendNotify) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package grafana_test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package grafana | ||
|
||
import ( | ||
"encoding/json" | ||
"io" | ||
"net/http" | ||
|
||
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime" | ||
"github.com/movsb/taoblog/modules/utils" | ||
"github.com/movsb/taoblog/protocols/go/proto" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
func New(client proto.UtilsServer) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
rc := http.MaxBytesReader(w, r.Body, 1<<20) | ||
defer rc.Close() | ||
body := utils.DropLast1(io.ReadAll(rc)) | ||
var m map[string]any | ||
json.Unmarshal(body, &m) | ||
var message string | ||
if x, ok := m[`message`]; ok { | ||
message, _ = x.(string) | ||
} | ||
_, err := client.InstantNotify(r.Context(), &proto.InstantNotifyRequest{ | ||
Title: `监控告警`, | ||
// https://grafana.com/docs/grafana/latest/alerting/configure-notifications/manage-contact-points/integrations/webhook-notifier/ | ||
Message: message, | ||
}) | ||
if err != nil { | ||
http.Error(w, err.Error(), runtime.HTTPStatusFromCode(status.Code(err))) | ||
return | ||
} | ||
}) | ||
} |
Oops, something went wrong.