Skip to content

Commit

Permalink
Merge pull request #82 from JudahNour/embedHTML
Browse files Browse the repository at this point in the history
embed html files into go code
  • Loading branch information
medyagh authored Aug 18, 2023
2 parents c0abf47 + 2b5a367 commit eba700b
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 38 deletions.
35 changes: 0 additions & 35 deletions cmd/gopogh-server/flake_chart.html

This file was deleted.

4 changes: 4 additions & 0 deletions cmd/gopogh-server/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
_ "embed"
"flag"
"log"
"net/http"
Expand Down Expand Up @@ -31,6 +32,7 @@ func main() {
Database: datab,
}
// Create an HTTP server and register the handlers

http.HandleFunc("/db", db.ServeEnvironmentTestsAndTestCases)

http.HandleFunc("/env", db.ServeEnvCharts)
Expand All @@ -41,6 +43,8 @@ func main() {

http.HandleFunc("/version", handler.ServeGopoghVersion)

http.HandleFunc("/", handler.ServeHTML)

// Start the HTTP server
err = http.ListenAndServe(":8080", nil)
if err != nil {
Expand Down
37 changes: 34 additions & 3 deletions cmd/gopogh-server/flake_chart.js → pkg/handler/flake_chart.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
// Displays an error message to the UI. Any previous message will be erased.
<html>
<head>
<meta http-equiv="CacheControl" content="no-cache, no-store, must-revalidate"/>
<meta http-equiv="Pragma" content="no-cache"/>
<meta http-equiv="Expires" content="0"/>

<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/tablesort/5.2.1/tablesort.min.js'></script>
<!-- Include sort types you need -->
<script src='https://cdnjs.cloudflare.com/ajax/libs/tablesort/5.2.1/sorts/tablesort.number.min.js'></script>
<script type="text/javascript">
// Displays an error message to the UI. Any previous message will be erased.
function displayError(message) {
// Clear the body of all children.
while (document.body.firstChild) {
Expand Down Expand Up @@ -862,7 +873,7 @@
await new Promise(resolve => google.charts.setOnLoadCallback(resolve));

let url;
const basePath = 'http://localhost:8080' // Base Server Path. Modify to actual server path if deploying
const basePath = ':8080' // Base Server Path. Modify to actual server path if deploying
if (desiredEnvironment === undefined) {
// URL for displaySummaryChart
url = basePath + '/summary'
Expand Down Expand Up @@ -905,4 +916,24 @@
}
}

init();
init();
</script>
<style>
table {
border: 1px solid gray;
margin-left: auto;
margin-right: auto;
border-collapse: collapse;
}
td, th {
border-bottom: 1px solid gray;
padding: 8px;
}
</style>
</head>
<body>
<div id="dropdown_container"></div>
<div id="chart_div"></div>
<div id="version_div"></div>
</body>
</html>
10 changes: 10 additions & 0 deletions pkg/handler/handler.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package handler

import (
_ "embed"
"encoding/json"
"fmt"
"net/http"
Expand All @@ -14,6 +15,9 @@ type DB struct {
Database db.Datab
}

//go:embed flake_chart.html
var flakeChartHTML string

func (m *DB) ServeEnvironmentTestsAndTestCases(w http.ResponseWriter, _ *http.Request) {
data, err := m.Database.GetEnvironmentTestsAndTestCases()
if err != nil {
Expand Down Expand Up @@ -158,3 +162,9 @@ func ServeGopoghVersion(w http.ResponseWriter, _ *http.Request) {
return
}
}

func ServeHTML(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Header().Set("Access-Control-Allow-Origin", "*")
fmt.Fprint(w, flakeChartHTML)
}

0 comments on commit eba700b

Please sign in to comment.