issue-2594: use embedded for static files (#2650)

embed static js and css files from CDN into vmalert, vmagent and vmsingle binaries.

Co-authored-by: f41gh7 <nik@victoriametrics.com>

https://github.com/VictoriaMetrics/VictoriaMetrics/issues/2594
This commit is contained in:
Dmytro Kozlov 2022-05-31 02:55:28 +03:00 committed by Aliaksandr Valialkin
parent 09fed19ba5
commit cd1fa2e4cd
No known key found for this signature in database
GPG key ID: A72BEC6CD3D0DED1
13 changed files with 56 additions and 16 deletions

View file

@ -1,6 +1,7 @@
package main package main
import ( import (
"embed"
"flag" "flag"
"fmt" "fmt"
"io" "io"
@ -62,6 +63,9 @@ var (
graphiteServer *graphiteserver.Server graphiteServer *graphiteserver.Server
opentsdbServer *opentsdbserver.Server opentsdbServer *opentsdbserver.Server
opentsdbhttpServer *opentsdbhttpserver.Server opentsdbhttpServer *opentsdbhttpserver.Server
//go:embed static
staticFiles embed.FS
staticServer = http.FileServer(http.FS(staticFiles))
) )
func main() { func main() {
@ -322,11 +326,16 @@ func requestHandler(w http.ResponseWriter, r *http.Request) bool {
w.Write([]byte("OK")) w.Write([]byte("OK"))
} }
return true return true
default:
if strings.HasPrefix(r.URL.Path, "/static") {
staticServer.ServeHTTP(w, r)
return true
}
if remotewrite.MultitenancyEnabled() {
return processMultitenantRequest(w, r, path)
}
return false
} }
if remotewrite.MultitenancyEnabled() {
return processMultitenantRequest(w, r, path)
}
return false
} }
func processMultitenantRequest(w http.ResponseWriter, r *http.Request, path string) bool { func processMultitenantRequest(w http.ResponseWriter, r *http.Request, path string) bool {

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -1,7 +1,7 @@
{% func Footer() %} {% func Footer() %}
</main> </main>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script> <script src="static/js/jquery-3.6.0.min.js" type="text/javascript"></script>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script> <script src="static/js/bootstrap.bundle.min.js" type="text/javascript"></script>
<script type="text/javascript"> <script type="text/javascript">
function expandAll() { function expandAll() {
$('.collapse').addClass('show'); $('.collapse').addClass('show');
@ -18,14 +18,14 @@
$(".group-heading").click(function(e) { $(".group-heading").click(function(e) {
let target = $(this).attr('data-bs-target'); let target = $(this).attr('data-bs-target');
let el = $('#'+target); let el = $("#"+target);
new bootstrap.Collapse(el, { new bootstrap.Collapse(el, {
toggle: true toggle: true
}); });
}); });
var hash = window.location.hash.substr(1); var hash = window.location.hash.substr(1);
let group = $('#'+hash); let group = $("#"+hash);
if (group.length > 0) { if (group.length > 0) {
group.click(); group.click();
} }

View file

@ -22,8 +22,8 @@ func StreamFooter(qw422016 *qt422016.Writer) {
//line app/vmalert/tpl/footer.qtpl:1 //line app/vmalert/tpl/footer.qtpl:1
qw422016.N().S(` qw422016.N().S(`
</main> </main>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script> <script src="static/js/jquery-3.6.0.min.js" type="text/javascript"></script>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script> <script src="static/js/bootstrap.bundle.min.js" type="text/javascript"></script>
<script type="text/javascript"> <script type="text/javascript">
function expandAll() { function expandAll() {
$('.collapse').addClass('show'); $('.collapse').addClass('show');
@ -40,14 +40,14 @@ func StreamFooter(qw422016 *qt422016.Writer) {
$(".group-heading").click(function(e) { $(".group-heading").click(function(e) {
let target = $(this).attr('data-bs-target'); let target = $(this).attr('data-bs-target');
let el = $('#'+target); let el = $("#"+target);
new bootstrap.Collapse(el, { new bootstrap.Collapse(el, {
toggle: true toggle: true
}); });
}); });
var hash = window.location.hash.substr(1); var hash = window.location.hash.substr(1);
let group = $('#'+hash); let group = $("#"+hash);
if (group.length > 0) { if (group.length > 0) {
group.click(); group.click();
} }

View file

@ -3,7 +3,7 @@
<html lang="en"> <html lang="en">
<head> <head>
<title>vmalert{% if title != "" %} - {%s title %}{% endif %}</title> <title>vmalert{% if title != "" %} - {%s title %}{% endif %}</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous"> <link href="static/css/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous">
<style> <style>
body{ body{
min-height: 75rem; min-height: 75rem;

View file

@ -35,7 +35,7 @@ func StreamHeader(qw422016 *qt422016.Writer, title string, pages []NavItem) {
} }
//line app/vmalert/tpl/header.qtpl:5 //line app/vmalert/tpl/header.qtpl:5
qw422016.N().S(`</title> qw422016.N().S(`</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous"> <link href="static/css/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous">
<style> <style>
body{ body{
min-height: 75rem; min-height: 75rem;

View file

@ -1,6 +1,7 @@
package main package main
import ( import (
"embed"
"encoding/json" "encoding/json"
"fmt" "fmt"
"net/http" "net/http"
@ -21,6 +22,9 @@ var (
once = sync.Once{} once = sync.Once{}
apiLinks [][2]string apiLinks [][2]string
navItems []tpl.NavItem navItems []tpl.NavItem
//go:embed static
staticFiles embed.FS
staticServer = http.FileServer(http.FS(staticFiles))
) )
func initLinks() { func initLinks() {
@ -99,6 +103,11 @@ func (rh *requestHandler) handler(w http.ResponseWriter, r *http.Request) bool {
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
return true return true
default: default:
if strings.HasPrefix(r.URL.Path, "/static") {
staticServer.ServeHTTP(w, r)
return true
}
if !strings.HasSuffix(r.URL.Path, "/status") { if !strings.HasSuffix(r.URL.Path, "/status") {
return false return false
} }

View file

@ -15,6 +15,8 @@ The following tip changes can be tested by building VictoriaMetrics components f
## tip ## tip
* FEATURE: [vmagent](https://docs.victoriametrics.com/vmagent.html): embed boostrap js from CDN into binary. Previously web API was broken without internet connection. See [shis issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/2594)
* FEATURE: [vmalert](https://docs.victoriametrics.com/vmalert.html): embed boostrap js from CDN into binary. Previously `/targets` API was broken without internet connection. See [shis issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/2594)
* FEATURE: [vmagent](https://docs.victoriametrics.com/vmagent.html): expose `/api/v1/status/config` endpoint in the same way as Prometheus does. See [these docs](https://prometheus.io/docs/prometheus/latest/querying/api/#config). * FEATURE: [vmagent](https://docs.victoriametrics.com/vmagent.html): expose `/api/v1/status/config` endpoint in the same way as Prometheus does. See [these docs](https://prometheus.io/docs/prometheus/latest/querying/api/#config).
* FEATURE: add ability to change the `indexdb` rotation timezone offset via `-retentionTimezoneOffset` command-line flag. Previously it was performed at 4am UTC time. This could lead to performance degradation in the middle of the day when VictoriaMetrics runs in time zones located too far from UTC. Thanks to @cnych for [the pull request](https://github.com/VictoriaMetrics/VictoriaMetrics/pull/2574). * FEATURE: add ability to change the `indexdb` rotation timezone offset via `-retentionTimezoneOffset` command-line flag. Previously it was performed at 4am UTC time. This could lead to performance degradation in the middle of the day when VictoriaMetrics runs in time zones located too far from UTC. Thanks to @cnych for [the pull request](https://github.com/VictoriaMetrics/VictoriaMetrics/pull/2574).
* FEATURE: [vmagent](https://docs.victoriametrics.com/vmagent.html): add `-promscrape.suppressScrapeErrorsDelay` command-line flag, which can be used for delaying and aggregating the logging of per-target scrape errors. This may reduce the amounts of logs when `vmagent` scrapes many unreliable targets. See [this feature request](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/2575). Thanks to @jelmd for [the initial implementation](https://github.com/VictoriaMetrics/VictoriaMetrics/pull/2576). * FEATURE: [vmagent](https://docs.victoriametrics.com/vmagent.html): add `-promscrape.suppressScrapeErrorsDelay` command-line flag, which can be used for delaying and aggregating the logging of per-target scrape errors. This may reduce the amounts of logs when `vmagent` scrapes many unreliable targets. See [this feature request](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/2575). Thanks to @jelmd for [the initial implementation](https://github.com/VictoriaMetrics/VictoriaMetrics/pull/2576).

View file

@ -48,7 +48,7 @@ job={%q= jobName %} (0/0 up)
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous"> <link href="static/css/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous">
<title>Scrape targets</title> <title>Scrape targets</title>
<script> <script>
function collapse_all() { function collapse_all() {

View file

@ -216,7 +216,7 @@ func TargetsResponsePlain(jts []jobTargetsStatuses, emptyJobs []string, showOrig
//line lib/promscrape/targetstatus.qtpl:45 //line lib/promscrape/targetstatus.qtpl:45
func StreamTargetsResponseHTML(qw422016 *qt422016.Writer, jts []jobTargetsStatuses, emptyJobs []string, showOnlyUnhealthy bool, endpointSearch, labelSearch string, err error) { func StreamTargetsResponseHTML(qw422016 *qt422016.Writer, jts []jobTargetsStatuses, emptyJobs []string, showOnlyUnhealthy bool, endpointSearch, labelSearch string, err error) {
//line lib/promscrape/targetstatus.qtpl:45 //line lib/promscrape/targetstatus.qtpl:45
qw422016.N().S(`<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous"><title>Scrape targets</title><script>function collapse_all() {for (var i = 0; i <`) qw422016.N().S(`<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><link href="static/css/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous"><title>Scrape targets</title><script>function collapse_all() {for (var i = 0; i <`)
//line lib/promscrape/targetstatus.qtpl:55 //line lib/promscrape/targetstatus.qtpl:55
qw422016.N().D(len(jts)) qw422016.N().D(len(jts))
//line lib/promscrape/targetstatus.qtpl:55 //line lib/promscrape/targetstatus.qtpl:55