feat: complete template

This commit is contained in:
Rogee
2024-11-29 17:05:05 +08:00
parent aa7ad01ecc
commit 7bc082abea
36 changed files with 1336 additions and 252 deletions

View File

@@ -1,42 +1,56 @@
package http
import (
"backend/modules/users"
"backend/providers/app"
"backend/providers/http/fiber"
"backend/providers/http"
"backend/providers/postgres"
"git.ipao.vip/rogeecn/atom"
"git.ipao.vip/rogeecn/atom/container"
"git.ipao.vip/rogeecn/atom/contracts"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"go.uber.org/dig"
)
func Default(providers ...container.ProviderContainer) container.Providers {
func defaultProviders(providers ...container.ProviderContainer) container.Providers {
return append(container.Providers{
app.DefaultProvider(),
fiber.DefaultProvider(),
http.DefaultProvider(),
postgres.DefaultProvider(),
}, providers...)
}
func Command() atom.Option {
providers := defaultProviders().With(
users.Provide,
)
return atom.Command(
atom.Name("serve"),
atom.Short("run http server"),
atom.RunE(Serve),
atom.Providers(Default()),
atom.Providers(providers),
)
}
type Http struct {
dig.In
Service contracts.HttpService
Service *http.Service
Initials []contracts.Initial `group:"initials"`
Routes []contracts.HttpRoute `group:"routes"`
}
func Serve(cmd *cobra.Command, args []string) error {
return container.Container.Invoke(func(http Http) error {
for _, route := range http.Routes {
if err := route.Register(); err != nil {
log.Fatal(err)
}
}
return http.Service.Serve()
})
}

View File

@@ -0,0 +1,101 @@
package http
import (
"backend/providers/wechat"
"github.com/gofiber/fiber/v3"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
type Middlewares struct {
client *wechat.Client
}
func Init(client *wechat.Client) *Middlewares {
return &Middlewares{
client: client,
}
}
func (f *Middlewares) Verify(c fiber.Ctx) error {
// get the query parameters
signature := c.Query("signature")
timestamp := c.Query("timestamp")
nonce := c.Query("nonce")
echostr := c.Query("echostr")
if signature == "" || timestamp == "" || nonce == "" || echostr == "" {
return c.Next()
}
log.Infof(
"begin verify signature, signature: %s, timestamp: %s, nonce: %s, echostr: %s",
signature,
timestamp,
nonce,
echostr,
)
// verify the signature
if err := f.client.Verify(signature, timestamp, nonce); err != nil {
return c.SendString(err.Error())
}
return c.SendString(echostr)
}
func (f *Middlewares) SilentAuth(c fiber.Ctx) error {
// if cookie not exists key "openid", then redirect to the wechat auth page
sid := c.Cookies("sid", "")
if sid != "" {
// TODO: verify sid
return c.Next()
}
// get current full url
url := c.BaseURL()
url = "https://qvyun.mp.jdwan.com"
log.WithField("module", "middleware.SilentAuth").Debug("url:", url)
to, err := f.client.ScopeAuthorizeURL(
wechat.ScopeAuthorizeURLWithRedirectURI(url),
wechat.ScopeAuthorizeURLWithState("sns_basic_auth"),
)
if err != nil {
return errors.Wrap(err, "failed to get wechat auth url")
}
log.WithField("module", "middleware.SilentAuth").Debug("redirectTo: ", to.String())
return c.Redirect().To(to.String())
}
func (f *Middlewares) AuthUserInfo(c fiber.Ctx) error {
state := c.Query("state")
code := c.Query("code")
if state == "" && code == "" {
return c.Next()
}
if state != "sns_basic_auth" {
return c.Next()
}
log.WithField("module", "middleware.AuthUserInfo").Debug("code", code)
// get the openid
token, err := f.client.AuthorizeCode2Token(code)
if err != nil {
return errors.Wrap(err, "failed to get openid")
}
// TODO: store the openid to the session
// set the openid to the cookie
c.Cookie(&fiber.Cookie{
Name: "sid",
Value: token.Openid,
HTTPOnly: true,
})
return c.Redirect().To("/")
}