Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Self host redoc #284

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions backend/internal/redoc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package internal

//go:generate go run github.com/opentofu/registry-ui/internal/tools/pull-redoc --version v2.3.0 --checksum 87e6d82ac43f8f797369dab7616381d06a1f9a55455a3dc47e8366afd73fb9d0
31 changes: 15 additions & 16 deletions backend/internal/server/index.html
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
<!DOCTYPE html>
<html>
<head>
<title>OpenTofu Registry UI API</title>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<redoc spec-url='/openapi.yml'></redoc>
<!-- TODO self-host this -->
<script src="https://cdn.redoc.ly/redoc/latest/bundles/redoc.standalone.js"> </script>
</body>
<head>
<title>OpenTofu Registry UI API</title>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<redoc spec-url='./openapi.yml'></redoc>
<script src="redoc.standalone.js"> </script>
</body>
</html>
14 changes: 13 additions & 1 deletion backend/internal/server/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ package server
import (
"context"
_ "embed"

"github.com/opentofu/registry-ui/internal/indexstorage"
)

Expand All @@ -23,6 +22,12 @@ var openapiYaml []byte
//go:embed index.html
var indexHTML []byte

//go:embed redoc.standalone.js
var reDocScriptFile []byte

//go:embed redoc.standalone.js.LICENSE.txt
var reDocLicenseFile []byte

type OpenAPIWriter interface {
Write(ctx context.Context) error
}
Expand All @@ -44,5 +49,12 @@ func (w writer) Write(ctx context.Context) error {
if err := w.storage.WriteFile(ctx, "index.html", indexHTML); err != nil {
return err
}
if err := w.storage.WriteFile(ctx, "redoc.standalone.js", reDocScriptFile); err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also write record.standalone.js.LICENSE.txt here.

return err
}
if err := w.storage.WriteFile(ctx, "redoc.standalone.js.LICENSE.txt", reDocLicenseFile); err != nil {
return err
}

return nil
}
1,828 changes: 1,828 additions & 0 deletions backend/internal/server/redoc.standalone.js

Large diffs are not rendered by default.

102 changes: 102 additions & 0 deletions backend/internal/server/redoc.standalone.js.LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*!
Copyright (c) 2018 Jed Watson.
Licensed under the MIT License (MIT), see
http://jedwatson.github.io/classnames
*/

/*!
* ReDoc - OpenAPI/Swagger-generated API Reference Documentation
* -------------------------------------------------------------
* Version: "2.3.0"
* Repo: https://github.com/Redocly/redoc
*/

/*!
* Stickyfill -- `position: sticky` polyfill
* v. 1.1.1 | https://github.com/wilddeer/stickyfill
* Copyright Oleg Korsunsky | http://wd.dizaina.net/
*
* MIT License
*/

/*!
* perfect-scrollbar v1.5.3
* Copyright 2021 Hyunje Jun, MDBootstrap and Contributors
* Licensed under MIT
*/

/*! @license DOMPurify 3.1.3 | (c) Cure53 and other contributors | Released under the Apache license 2.0 and Mozilla Public License 2.0 | github.com/cure53/DOMPurify/blob/3.1.3/LICENSE */

/*!***************************************************
* mark.js v8.11.1
* https://markjs.io/
* Copyright (c) 2014–2018, Julian Kühnel
* Released under the MIT license https://git.io/vwTVl
*****************************************************/

/**
* @license React
* react-dom.production.min.js
*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

/**
* @license React
* react.production.min.js
*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

/**
* @license React
* scheduler.production.min.js
*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

/**
* @license React
* use-sync-external-store-shim.production.min.js
*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

/**
* Prism: Lightweight, robust, elegant syntax highlighting
*
* @license MIT <https://opensource.org/licenses/MIT>
* @author Lea Verou <https://lea.verou.me>
* @namespace
* @public
*/

/** @license React v16.13.1
* react-is.production.min.js
*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

/** @license React v17.0.2
* react-is.production.min.js
*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
95 changes: 95 additions & 0 deletions backend/internal/tools/pull-redoc/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package main

import (
"crypto/sha256"
"encoding/hex"
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
)

func main() {
version := ""
checksum := ""
flag.StringVar(&version, "version", "latest", "The version of ReDoc to download.")
flag.StringVar(&checksum, "checksum", "", "The checksum of the ReDoc script file corresponding to the version to be downloaded.")
flag.Parse()

fmt.Printf("checksum: %+v\n", checksum)

// Pull Redoc script from Redocly CDN
redocCDNURL := fmt.Sprintf("https://cdn.redoc.ly/redoc/%s/bundles/redoc.standalone.js", version)

redocFileName := "redoc.standalone.js"
redocFile, err := os.Create("server/redoc.tmp")
if err != nil {
log.Fatalf("err creating ReDoc script tmp file: %v", err)
}
defer redocFile.Close()

resp, err := http.Get(redocCDNURL)
if err != nil {
log.Fatalf("err downloading ReDoc script file: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode > 299 {
err = fmt.Errorf("err downloading ReDoc script file: %+v\n", resp)
log.Fatal(err)
}

// Copy downloaded contents to a local file
if _, err = io.Copy(redocFile, resp.Body); err != nil {
log.Fatal(err)
}

// Reset file pointer to the beginning before reading it into the hash
if _, err = redocFile.Seek(0, 0); err != nil {
log.Fatal(err)
}
hash := sha256.New()
_, err = io.Copy(hash, redocFile)
if err != nil {
log.Fatalf("err hashing script file: %+v\n", err)
}

// compare hashed value with supplied checksum
if checksum != hex.EncodeToString(hash.Sum(nil)) {
log.Fatalf("could not verify checksum for the file : %+v\n", redocFileName)
}

err = os.Rename("server/redoc.tmp", fmt.Sprintf("server/%s", redocFileName))
if err != nil {
log.Fatalf("Error renaming ReDoc script file: %v", err)
}

// Pull ReDoc LICENSE file
licenseFileURL := fmt.Sprintf("https://cdn.redoc.ly/redoc/%s/bundles/redoc.standalone.js.LICENSE.txt", version)

licenseFile, err := os.Create("server/license.tmp")
if err != nil {
log.Fatalf("err creating ReDoc license tmp file: %v", err)
}
resp1, err := http.Get(licenseFileURL)
if err != nil {
log.Fatalf("err downloading ReDoc license file: %v", err)
}

defer resp1.Body.Close()

if resp1.StatusCode < 200 || resp1.StatusCode > 299 {
err = fmt.Errorf("err downloading ReDoc license file: %+v\n", resp1)
log.Fatal(err)
}

if _, err = io.Copy(licenseFile, resp1.Body); err != nil {
log.Fatal(err)
}

err = os.Rename("server/license.tmp", "server/redoc.standalone.js.LICENSE.txt")
if err != nil {
log.Fatalf("Error renaming ReDoc license file: %v", err)
}
}