Skip to content

Commit

Permalink
pixel: init
Browse files Browse the repository at this point in the history
  • Loading branch information
wuhan005 committed Feb 11, 2024
1 parent 13fe456 commit 0b85a41
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 3 deletions.
4 changes: 4 additions & 0 deletions internal/conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ func Init() error {
return errors.Wrap(err, "map 'recaptcha'")
}

if err := File.Section("pixel").MapTo(&Pixel); err != nil {
return errors.Wrap(err, "map 'pixel'")
}

if err := File.Section("upload").MapTo(&Upload); err != nil {
return errors.Wrap(err, "map 'upload'")
}
Expand Down
4 changes: 4 additions & 0 deletions internal/conf/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ var (
TurnstileStyle bool `ini:"turnstile_style"`
}

Pixel struct {
Host string `ini:"host"`
}

Upload struct {
DefaultAvatarURL string `ini:"default_avatar"`
DefaultBackground string `ini:"default_background"`
Expand Down
5 changes: 4 additions & 1 deletion internal/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"net/http"
"reflect"
"strings"

"github.com/flamego/csrf"
"github.com/flamego/flamego"
Expand Down Expand Up @@ -158,7 +159,7 @@ func Contexter() flamego.Handler {
Template: t,
}

if ctx.Request().Method == http.MethodPost {
if ctx.Request().Method == http.MethodPost && !strings.HasPrefix(ctx.Request().URL.Path, "/api/v1/pixel/") {
x.Validate(ctx)
}

Expand All @@ -179,6 +180,8 @@ func Contexter() flamego.Handler {
c.Data["LoggedUserName"] = ""
}

c.Data["IsPixel"] = ctx.Request().URL.Path == "/pixel"

span := trace.SpanFromContext(ctx.Request().Context())
if span.IsRecording() {
span.SetAttributes(
Expand Down
4 changes: 4 additions & 0 deletions internal/route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
templatepkg "github.com/NekoWheel/NekoBox/internal/template"
"github.com/NekoWheel/NekoBox/route"
"github.com/NekoWheel/NekoBox/route/auth"
"github.com/NekoWheel/NekoBox/route/pixel"
"github.com/NekoWheel/NekoBox/route/question"
"github.com/NekoWheel/NekoBox/route/user"
"github.com/NekoWheel/NekoBox/static"
Expand Down Expand Up @@ -83,6 +84,7 @@ func New() *flamego.Flame {

f.Group("", func() {
f.Get("/", route.Home)
f.Get("/pixel", reqUserSignIn, pixel.Index)
f.Get("/sponsor", route.Sponsor)
f.Get("/change-logs", route.ChangeLogs)
f.Get("/robots.txt", func(c context.Context) {
Expand Down Expand Up @@ -137,6 +139,8 @@ func New() *flamego.Flame {
})
})
})

f.Any("/pixel/{**}", reqUserSignIn, pixel.Proxy)
}, context.APIEndpoint)
},
cache.Cacher(cache.Options{
Expand Down
62 changes: 62 additions & 0 deletions route/pixel/pixel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright 2024 E99p1ant. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package pixel

import (
"io"
"net/http"
"path"
"strconv"

"github.com/sirupsen/logrus"

"github.com/NekoWheel/NekoBox/internal/conf"
"github.com/NekoWheel/NekoBox/internal/context"
)

func Index(ctx context.Context) {
ctx.Success("pixel")
}

func Proxy(ctx context.Context) error {
uri := ctx.Param("**")
method := ctx.Request().Method
userID := strconv.Itoa(int(ctx.User.ID))

var body io.Reader
if method == http.MethodPost || method == http.MethodPut {
body = ctx.Request().Request.Body
}

req, err := http.NewRequest(method, "http://pixel/", body)
if err != nil {
logrus.WithContext(ctx.Request().Context()).WithError(err).Error("Failed to create request")
return ctx.ServerError()
}
req.URL.Host = conf.Pixel.Host
req.URL.Path = path.Join("/api/", uri)
req.Header.Set("neko-user-id", userID)

client := http.Client{}
resp, err := client.Do(req)
if err != nil {
logrus.WithContext(ctx.Request().Context()).WithError(err).Error("Failed to send request")
return ctx.ServerError()
}
defer func() { _ = resp.Body.Close() }()

for k, v := range resp.Header {
ctx.ResponseWriter().Header()[k] = v
}
ctx.ResponseWriter().WriteHeader(resp.StatusCode)

_, err = io.Copy(ctx.ResponseWriter(), resp.Body)
if err != nil {
logrus.WithContext(ctx.Request().Context()).WithError(err).Error("Failed to copy response")
return ctx.ServerError()
}

return nil
}
3 changes: 2 additions & 1 deletion templates/base/footer.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<p class="uk-text-right uk-text-muted uk-text-small">
<p class="{{ if .IsPixel }}uk-container{{end}} uk-text-right uk-text-muted uk-text-small">
Made with ❤️ by E99p1ant. {{ ICP }}
<br>
<a href="/pixel">画板</a> |
<a href="https://github.com/NekoWheel/NekoBox" target="_blank">GitHub</a> | <a
href="https://github.com/NekoWheel/NekoBox/commit/{{ CommitSHA }}" target="_blank"> {{ CommitSHAShort }}</a>
| <a href="https://support.qq.com/products/293656" target="_blank">吐槽反馈</a>
Expand Down
4 changes: 4 additions & 0 deletions templates/base/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,8 @@
</div>
</div>
</nav>
{{ if .IsPixel }}
<div>
{{ else }}
<div class="uk-container uk-container-xsmall">
{{ end }}
2 changes: 1 addition & 1 deletion templates/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ import (
"embed"
)

//go:embed auth base mail question user home.html sponsor.html change-logs.html
//go:embed auth base mail question user home.html sponsor.html change-logs.html pixel.html
var FS embed.FS
8 changes: 8 additions & 0 deletions templates/pixel.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{{template "base/header" .}}
<link rel="stylesheet" href="https://npm.onmicrosoft.cn/@e99p1ant/neko-pixel-umd@latest/style.css"/>
<div id="app"></div>
<script>
var NEKO_CONFIG = {pixelBaseURL: '/api/v1/pixel'}
</script>
<script src="https://npm.onmicrosoft.cn/@e99p1ant/neko-pixel-umd@latest/neko-pixel-app.umd.js"></script>
{{template "base/footer" .}}

0 comments on commit 0b85a41

Please sign in to comment.