From 1ad012b8fa54982de3f51d44ff0b42a0e30ee54c Mon Sep 17 00:00:00 2001 From: Alexander NeonXP Kiryukhin Date: Sun, 5 May 2024 22:26:52 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9E=D0=91=D0=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Caddyfile | 19 +++++++++ api/openapi.yaml | 13 ++++++ api/parts/game.yaml | 13 ++++++ api/server.go | 63 ++++++++++++++++++++---------- Dockerfile => build/api/Dockerfile | 2 +- build/caddy/Dockerfile | 3 ++ docker-compose.yml | 17 ++++++-- embeds.go | 17 -------- frontend/src/components/Layout.jsx | 7 +++- frontend/src/pages/Index.jsx | 8 +++- frontend/src/pages/Quests.jsx | 6 +-- main.go | 3 -- pkg/controller/file.go | 11 ++++++ pkg/service/file.go | 6 +++ 14 files changed, 136 insertions(+), 52 deletions(-) create mode 100644 Caddyfile rename Dockerfile => build/api/Dockerfile (97%) create mode 100644 build/caddy/Dockerfile delete mode 100644 embeds.go diff --git a/Caddyfile b/Caddyfile new file mode 100644 index 0000000..e8c3eaf --- /dev/null +++ b/Caddyfile @@ -0,0 +1,19 @@ +{ + debug +} + +http:// { + handle_path /file/* { + root * /app/store + file_server + } + handle /api* { + reverse_proxy http://app:8000 + } + handle { + root * /app/frontend/dist + file_server + try_files {path} /index.html + encode gzip zstd + } +} \ No newline at end of file diff --git a/api/openapi.yaml b/api/openapi.yaml index d91fe5a..fae1052 100644 --- a/api/openapi.yaml +++ b/api/openapi.yaml @@ -377,6 +377,19 @@ paths: responses: 200: $ref: '#/components/responses/taskResponse' + /file/{uid}: + get: + operationId: getFile + parameters: + - in: path + name: uid + required: true + schema: + format: uuid + type: string + response: + 307: + description: redirect /games: get: responses: diff --git a/api/parts/game.yaml b/api/parts/game.yaml index 9629fa7..61a9adc 100644 --- a/api/parts/game.yaml +++ b/api/parts/game.yaml @@ -40,4 +40,17 @@ paths: responses: 200: $ref: "#/components/responses/taskResponse" + /file/{uid}: + get: + operationId: getFile + parameters: + - name: uid + in: path + required: true + schema: + type: string + format: uuid + response: + 307: + description: redirect diff --git a/api/server.go b/api/server.go index 3b1054e..52b980a 100644 --- a/api/server.go +++ b/api/server.go @@ -43,6 +43,9 @@ type ServerInterface interface { // (POST /engine/{uid}/code) EnterCode(ctx echo.Context, uid openapi_types.UUID) error + // (GET /file/{uid}) + GetFile(ctx echo.Context, uid openapi_types.UUID) error + // (GET /games) GetGames(ctx echo.Context) error @@ -176,6 +179,24 @@ func (w *ServerInterfaceWrapper) EnterCode(ctx echo.Context) error { return err } +// GetFile converts echo context to params. +func (w *ServerInterfaceWrapper) GetFile(ctx echo.Context) error { + var err error + // ------------- Path parameter "uid" ------------- + var uid openapi_types.UUID + + err = runtime.BindStyledParameterWithOptions("simple", "uid", ctx.Param("uid"), &uid, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationPath, Explode: false, Required: true}) + if err != nil { + return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid format for parameter uid: %s", err)) + } + + ctx.Set(CookieAuthScopes, []string{}) + + // Invoke the callback with all the unmarshaled arguments + err = w.Handler.GetFile(ctx, uid) + return err +} + // GetGames converts echo context to params. func (w *ServerInterfaceWrapper) GetGames(ctx echo.Context) error { var err error @@ -262,6 +283,7 @@ func RegisterHandlersWithBaseURL(router EchoRouter, si ServerInterface, baseURL router.GET(baseURL+"/admin/games/:uid", wrapper.AdminGetGame) router.GET(baseURL+"/engine/:uid", wrapper.GameEngine) router.POST(baseURL+"/engine/:uid/code", wrapper.EnterCode) + router.GET(baseURL+"/file/:uid", wrapper.GetFile) router.GET(baseURL+"/games", wrapper.GetGames) router.GET(baseURL+"/user", wrapper.GetUser) router.POST(baseURL+"/user/login", wrapper.PostUserLogin) @@ -273,26 +295,27 @@ func RegisterHandlersWithBaseURL(router EchoRouter, si ServerInterface, baseURL // Base64 encoded, gzipped, json marshaled Swagger object var swaggerSpec = []string{ - "H4sIAAAAAAA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6fEG", - "bHEQW+20+zFhEbMbFC0zv9OGZ7VongECnXhXVRCkeR/CQ+XUPgGoCwhaQ/PUTLoOuvvx29VqiKSNXNhf", - "QdtZsM628b/qU6xc0xcedEK3ythqVmYIpS9WiHB6TZismt9UsjtYW7IiRZFzjSGp+TXh6NmsKMiOpRsh", - "ud551wTPHvX8JB3shC/KUNPFxol7Xlfqs53t7fE26BEG0LF+7jrMcGKWuXcojw2oeyXy8gSET4VIJvrH", - "OdSwTJeFo/or947FgAG5ERImMCEszqzgj4tI58bI8a0TXFiv/f5yOJMI+tQNaa8U4XH1NusuY94lyZFF", - "6UN6vLVV5bRcUyOTdu4asfjFzWVHtPz21VoZsPerd9MfdW/EWy6GqdoIOUy8P5Wxrl5YsaV4MTy159yY", - "b0on06ypZ87mi8UY1Ed49XyEO+1u3cFbFTgLcJLr+f++dxHGTBHHYMxPlepjPd77qGEjDDr+jnv5uZb8", - "rszYv3zrfTt/sfHsNI3dtpUfm2ujZ+2a2rsBva0PkEKnLGIhLTfluvwvAAD//8w97C1ZGwAA", + "H4sIAAAAAAAC/8xY30/kthP/V77yt48p2TtOqpQ3iig6FZ3aK9cXtEImGRYfiR3sCccW5X+vxk6yycb5", + "wZJy98SSTObHZz4znvEzi1WWKwkSDYuemQaTK2nA/gNaK/25ekIPYiURJNJPnuepiDkKJcOvRkl6ZuI7", + "yDj9yrXKQaNwemKV2M9xmwOLmJAIG9CsDFgGxvBN+6VBLeSGlWXANDwUQkPCoiunYie/Dmp5dfMVYmQl", + "fZCAibXIyScWMdJ/K1IwF8LgQVEIhMwG8JOGWxax/4c7sEInZkIy8REhI3OVT1xrvh1yacMzOEkyIQ9y", + "acwT0nyWCByz/N9iQRb+FvBtLhbIzf3iMJBS54TfaJGniicL0LooREJ/b5XOOLLIPQgmmGyF5vK3MKAX", + "B4iUDgNUBpWapnQtp6aKuo52T6Hn/SGgWWN90AL7wsaylH+lx0hT4j0js2IJmNJiIyRPP/HM75MR/3g7", + "5B4MVntHWfWp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tpb4o4BmP+V6k+1OOdjxo2wqDj77iXn2vJ78qM3cv33rfzlzXPntbYbVv5sbk2Oj+sqaUa", + "0I910y50yiIW0sJWrst/AwAA//8rjKvDLRwAAA==", } // GetSwagger returns the content of the embedded swagger specification file diff --git a/Dockerfile b/build/api/Dockerfile similarity index 97% rename from Dockerfile rename to build/api/Dockerfile index 324c107..e252b98 100644 --- a/Dockerfile +++ b/build/api/Dockerfile @@ -14,7 +14,6 @@ RUN apk update --no-cache && apk add --no-cache tzdata COPY go.mod go.sum ./ RUN go mod download && go mod verify COPY . . -COPY --from=frontend /app/dist /app/frontend/dist RUN go build -ldflags="-X gitrepo.ru/neonxp/nquest.Version=${VERSION}" -o nquest *.go # Runtime container @@ -24,6 +23,7 @@ RUN apk update --no-cache && apk add --no-cache ca-certificates COPY --from=backend /usr/share/zoneinfo/Europe/Moscow /usr/share/zoneinfo/Europe/Moscow COPY --from=backend /app/nquest /app/nquest +COPY --from=frontend /app/dist /app/frontend/dist ENV TZ Europe/Moscow diff --git a/build/caddy/Dockerfile b/build/caddy/Dockerfile new file mode 100644 index 0000000..f7c7841 --- /dev/null +++ b/build/caddy/Dockerfile @@ -0,0 +1,3 @@ +FROM caddy:2.8-alpine + +COPY Caddyfile /etc/caddy/Caddyfile \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 7458ce3..41bd55e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,19 +3,30 @@ version: '3.8' volumes: postgres-data: storage: + frontend: services: + caddy: + build: + context: . + dockerfile: build/caddy/Dockerfile + volumes: + - frontend:/app/frontend/dist:ro + - storage:/app/store:ro + ports: + - 8989:80 + depends_on: + - app app: build: context: . - dockerfile: Dockerfile - ports: - - 8989:8989 + dockerfile: build/api/Dockerfile depends_on: - db env_file: - .env volumes: + - frontend:/app/frontend/dist:rw - storage:/app/store:rw db: image: postgres:15-alpine3.17 diff --git a/embeds.go b/embeds.go deleted file mode 100644 index e695a92..0000000 --- a/embeds.go +++ /dev/null @@ -1,17 +0,0 @@ -package main - -import ( - "embed" - - "github.com/labstack/echo/v4" -) - -var ( - //go:embed all:frontend/dist - dist embed.FS - //go:embed frontend/dist/index.html - indexHTML embed.FS - - distDirFS = echo.MustSubFS(dist, "frontend/dist") - distIndexHtml = echo.MustSubFS(indexHTML, "frontend/dist") -) diff --git a/frontend/src/components/Layout.jsx b/frontend/src/components/Layout.jsx index 4d4f60e..dc490a1 100644 --- a/frontend/src/components/Layout.jsx +++ b/frontend/src/components/Layout.jsx @@ -4,7 +4,7 @@ import { UserProvider } from '../store/user' import { ajax } from '../utils/fetch' import { Layout, Menu } from 'antd' import { MenuOutlined } from '@ant-design/icons' -import { Content, Header } from 'antd/es/layout/layout' +import { Content, Footer, Header } from 'antd/es/layout/layout' import { useRole } from '../utils/roles' const AppLayout = () => { @@ -80,9 +80,12 @@ const AppLayout = () => { style={{ width: '100%' }} /> - + +
+ Сделал NeonXP в 2024. +
) } diff --git a/frontend/src/pages/Index.jsx b/frontend/src/pages/Index.jsx index d671bbc..96415e4 100644 --- a/frontend/src/pages/Index.jsx +++ b/frontend/src/pages/Index.jsx @@ -8,12 +8,16 @@ const Index = () => { const { user } = UserProvider.useContainer() const navigate = useNavigate() return (<> - NQuest + <img src="/assets/logo.png" />uest Привет! Это платформа для ARG игр. - Если ты попал сюда случайно, то скорее всего, для тебя здесь нет ничего интересного. + Если ты попал сюда случайно, то скорее всего, для тебя здесь нет ничего интересного.
А если ты знаешь зачем пришёл, то добро пожаловать!
+ + Телеграм-канал с новостями проекта: nquest_ru, рекомендую подписаться.
+ Там будут как roadmap проекта, так и анонсы новых квестов. +
{!user ? ( diff --git a/frontend/src/pages/Quests.jsx b/frontend/src/pages/Quests.jsx index 6154656..b1c9c4f 100644 --- a/frontend/src/pages/Quests.jsx +++ b/frontend/src/pages/Quests.jsx @@ -38,16 +38,14 @@ const renderItem = (user, navigate, item) => { , {item.taskCount} ур - , - <>{moment(item.createdAt).fromNow()}, - <>Автор {item.authors.map(a => a.username)} + ] let questAction = (Необходимо войти) if (user) { questAction = (user.games.find(x => x.id === item.id) - ? Вы уже прошли этот квест + ? Вы уже прошли этот квест : ) } diff --git a/main.go b/main.go index 36954b2..1f44d24 100644 --- a/main.go +++ b/main.go @@ -130,9 +130,6 @@ func main() { // --[ System ]-- e.GET("/metrics", echoprometheus.NewHandler()) - e.StaticFS("/file", afero.NewIOFS(storage)) - e.StaticFS("/*", distDirFS) - e.Logger.Debugf("backend version %s", Version) e.Logger.Fatal(e.Start(cfg.Listen)) } diff --git a/pkg/controller/file.go b/pkg/controller/file.go index cb77e2c..1ef54b9 100644 --- a/pkg/controller/file.go +++ b/pkg/controller/file.go @@ -1,6 +1,8 @@ package controller import ( + "fmt" + "github.com/google/uuid" "github.com/labstack/echo/v4" "gitrepo.ru/neonxp/nquest/api" @@ -57,3 +59,12 @@ func (u *File) AdminListFiles(c echo.Context, quest uuid.UUID) error { return c.JSON(200, list) } + +func (u *File) GetFile(c echo.Context, uid uuid.UUID) error { + f, err := u.FileService.GetFilesByID(c.Request().Context(), uid) + if err != nil { + return err + } + + return c.Redirect(307, fmt.Sprintf(`/file/%s/%s`, f.QuestID, f.Filename)) +} diff --git a/pkg/service/file.go b/pkg/service/file.go index b851f4d..1f1136b 100644 --- a/pkg/service/file.go +++ b/pkg/service/file.go @@ -57,3 +57,9 @@ func (u *File) GetFilesByQuest(ctx context.Context, quest uuid.UUID) ([]*models. return list, u.DB.WithContext(ctx).Find(&list, `quest_id = ?`, quest.String()).Error } + +func (u *File) GetFilesByID(ctx context.Context, fileID uuid.UUID) (*models.File, error) { + file := &models.File{} + + return file, u.DB.WithContext(ctx).First(file, fileID).Error +}