修复前端使用的接口地址

This commit is contained in:
xmdhs 2023-10-11 15:42:01 +08:00
parent 378afee54e
commit 0240a412d4
No known key found for this signature in database
GPG Key ID: E809D6D43DEFCC95
8 changed files with 68 additions and 12 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@ cmd/testserver
cmd/authlibskin/config.yaml cmd/authlibskin/config.yaml
cmd/authlibskin/skin cmd/authlibskin/skin
cmd/authlibskin/authlibskin.exe cmd/authlibskin/authlibskin.exe
server/static/files

1
frontend/.env Normal file
View File

@ -0,0 +1 @@
VITE_APIADDR = ''

View File

@ -1,8 +1,9 @@
import type { tokenData, ApiUser, ApiServerInfo, YggProfile, ApiConfig, List, UserInfo, EditUser } from '@/apis/model' import type { tokenData, ApiUser, ApiServerInfo, YggProfile, ApiConfig, List, UserInfo, EditUser } from '@/apis/model'
import { apiGet } from '@/apis/utils' import { apiGet } from '@/apis/utils'
import root from '@/utils/root'
export async function login(email: string, password: string, captchaToken: string) { export async function login(email: string, password: string, captchaToken: string) {
const v = await fetch(import.meta.env.VITE_APIADDR + "/api/v1/user/login", { const v = await fetch(root() + "/api/v1/user/login", {
method: "POST", method: "POST",
body: JSON.stringify({ body: JSON.stringify({
"email": email, "email": email,
@ -14,7 +15,7 @@ export async function login(email: string, password: string, captchaToken: strin
} }
export async function register(email: string, username: string, password: string, captchaToken: string) { export async function register(email: string, username: string, password: string, captchaToken: string) {
const v = await fetch(import.meta.env.VITE_APIADDR + "/api/v1/user/reg", { const v = await fetch(root() + "/api/v1/user/reg", {
method: "POST", method: "POST",
body: JSON.stringify({ body: JSON.stringify({
"Email": email, "Email": email,
@ -28,7 +29,7 @@ export async function register(email: string, username: string, password: string
export async function userInfo(token: string) { export async function userInfo(token: string) {
if (token == "") return if (token == "") return
const v = await fetch(import.meta.env.VITE_APIADDR + "/api/v1/user", { const v = await fetch(root() + "/api/v1/user", {
headers: { headers: {
"Authorization": "Bearer " + token "Authorization": "Bearer " + token
} }
@ -38,13 +39,13 @@ export async function userInfo(token: string) {
export async function serverInfo() { export async function serverInfo() {
const v = await fetch(import.meta.env.VITE_APIADDR + "/api/yggdrasil") const v = await fetch(root() + "/api/yggdrasil")
return await v.json() as ApiServerInfo return await v.json() as ApiServerInfo
} }
export async function yggProfile(uuid: string) { export async function yggProfile(uuid: string) {
if (uuid == "") return if (uuid == "") return
const v = await fetch(import.meta.env.VITE_APIADDR + "/api/yggdrasil/sessionserver/session/minecraft/profile/" + uuid) const v = await fetch(root() + "/api/yggdrasil/sessionserver/session/minecraft/profile/" + uuid)
const data = await v.json() const data = await v.json()
if (!v.ok) { if (!v.ok) {
throw new Error(data?.errorMessage) throw new Error(data?.errorMessage)
@ -57,7 +58,7 @@ export async function upTextures(uuid: string, token: string, textureType: 'skin
f.set("file", file) f.set("file", file)
f.set("model", model) f.set("model", model)
const r = await fetch(import.meta.env.VITE_APIADDR + "/api/yggdrasil/api/user/profile/" + uuid + "/" + textureType, { const r = await fetch(root() + "/api/yggdrasil/api/user/profile/" + uuid + "/" + textureType, {
method: "PUT", method: "PUT",
body: f, body: f,
headers: { headers: {
@ -70,7 +71,7 @@ export async function upTextures(uuid: string, token: string, textureType: 'skin
} }
export async function changePasswd(old: string, newpa: string, token: string) { export async function changePasswd(old: string, newpa: string, token: string) {
const r = await fetch(import.meta.env.VITE_APIADDR + "/api/v1/user/password", { const r = await fetch(root() + "/api/v1/user/password", {
method: "POST", method: "POST",
body: JSON.stringify({ body: JSON.stringify({
"old": old, "old": old,
@ -84,12 +85,12 @@ export async function changePasswd(old: string, newpa: string, token: string) {
} }
export async function getConfig() { export async function getConfig() {
const r = await fetch(import.meta.env.VITE_APIADDR + "/api/v1/config") const r = await fetch(root() + "/api/v1/config")
return await apiGet<ApiConfig>(r) return await apiGet<ApiConfig>(r)
} }
export async function changeName(name: string, token: string) { export async function changeName(name: string, token: string) {
const r = await fetch(import.meta.env.VITE_APIADDR + "/api/v1/user/name", { const r = await fetch(root() + "/api/v1/user/name", {
method: "POST", method: "POST",
body: JSON.stringify({ body: JSON.stringify({
"name": name, "name": name,
@ -102,7 +103,7 @@ export async function changeName(name: string, token: string) {
} }
export async function ListUser(page: number, token: string, email: string, name: string) { export async function ListUser(page: number, token: string, email: string, name: string) {
const u = new URL(import.meta.env.VITE_APIADDR + "/api/v1/admin/users") const u = new URL(root() + "/api/v1/admin/users")
u.searchParams.set("page", String(page)) u.searchParams.set("page", String(page))
u.searchParams.set("email", email) u.searchParams.set("email", email)
u.searchParams.set("name", name) u.searchParams.set("name", name)
@ -116,7 +117,7 @@ export async function ListUser(page: number, token: string, email: string, name:
} }
export async function editUser(u: EditUser, token: string, uid: string) { export async function editUser(u: EditUser, token: string, uid: string) {
const r = await fetch(import.meta.env.VITE_APIADDR + "/api/v1/admin/user/" + uid, { const r = await fetch(root() + "/api/v1/admin/user/" + uid, {
method: "PATCH", method: "PATCH",
headers: { headers: {
"Authorization": "Bearer " + token "Authorization": "Bearer " + token

View File

@ -0,0 +1,6 @@
export default function root() {
if (import.meta.env.VITE_APIADDR != "") {
return import.meta.env.VITE_APIADDR
}
return location.origin
}

View File

@ -10,6 +10,7 @@ import { useNavigate } from 'react-router-dom';
import Box from '@mui/material/Box'; import Box from '@mui/material/Box';
import useTitle from '@/hooks/useTitle'; import useTitle from '@/hooks/useTitle';
import SkinViewUUID from '@/components/SkinViewUUID'; import SkinViewUUID from '@/components/SkinViewUUID';
import root from '@/utils/root';
const Profile = function Profile() { const Profile = function Profile() {
const navigate = useNavigate(); const navigate = useNavigate();
@ -61,7 +62,7 @@ const Profile = function Profile() {
} }
function getYggRoot() { function getYggRoot() {
const u = new URL((import.meta.env.VITE_APIADDR ?? location.origin) + "/api/yggdrasil") const u = new URL(root() + "/api/yggdrasil")
return u.toString() return u.toString()
} }

View File

@ -73,3 +73,10 @@ func (l *StructuredLoggerEntry) Panic(v interface{}, stack []byte) {
slog.String("panic", fmt.Sprintf("%+v", v)), slog.String("panic", fmt.Sprintf("%+v", v)),
) )
} }
func APILocationIndication(handle http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Authlib-Injector-API-Location", "/api/yggdrasil/")
handle.ServeHTTP(w, r)
})
}

View File

@ -11,6 +11,7 @@ import (
"github.com/xmdhs/authlib-skin/config" "github.com/xmdhs/authlib-skin/config"
"github.com/xmdhs/authlib-skin/handle" "github.com/xmdhs/authlib-skin/handle"
"github.com/xmdhs/authlib-skin/handle/yggdrasil" "github.com/xmdhs/authlib-skin/handle/yggdrasil"
"github.com/xmdhs/authlib-skin/server/static"
) )
func NewRoute(handelY *yggdrasil.Yggdrasil, handel *handle.Handel, c config.Config, sl slog.Handler) http.Handler { func NewRoute(handelY *yggdrasil.Yggdrasil, handel *handle.Handel, c config.Config, sl slog.Handler) http.Handler {
@ -24,7 +25,9 @@ func NewRoute(handelY *yggdrasil.Yggdrasil, handel *handle.Handel, c config.Conf
if c.RaelIP { if c.RaelIP {
r.Use(middleware.RealIP) r.Use(middleware.RealIP)
} }
r.Use(APILocationIndication)
r.Mount("/", static.StaticServer())
r.Mount("/api/v1", newSkinApi(handel)) r.Mount("/api/v1", newSkinApi(handel))
r.Mount("/api/yggdrasil", newYggdrasil(handelY)) r.Mount("/api/yggdrasil", newYggdrasil(handelY))

36
server/static/static.go Normal file
View File

@ -0,0 +1,36 @@
package static
import (
"embed"
"io/fs"
"net/http"
"github.com/go-chi/chi/v5"
)
//go:embed files
var staticFs embed.FS
func StaticServer() http.Handler {
serverRoot, err := fs.Sub(staticFs, "files")
if err != nil {
panic(err)
}
r := chi.NewRouter()
r.Get("/", index)
r.Get("/*", index)
r.Mount("/assets", http.FileServer(http.FS(serverRoot)))
return r
}
func index(w http.ResponseWriter, r *http.Request) {
b, err := staticFs.ReadFile("files/index.html")
if err != nil {
panic(err)
}
w.Write(b)
}