-
Notifications
You must be signed in to change notification settings - Fork 30
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
9 changed files
with
93 additions
and
3 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
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
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,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 | ||
} |
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
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,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" .}} |