Skip to content

Commit

Permalink
optimize api
Browse files Browse the repository at this point in the history
  • Loading branch information
vsimakhin committed Sep 1, 2024
1 parent a9e657e commit ae4ffc4
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 33 deletions.
16 changes: 0 additions & 16 deletions app/handlers_aux.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"fmt"
"io/fs"
"net/http"

"github.com/go-chi/chi/v5"
)

//go:embed static
Expand Down Expand Up @@ -43,20 +41,6 @@ func (app *application) HandlerNotAllowed(w http.ResponseWriter, r *http.Request
}
}

func (app *application) HandlerGetApi(w http.ResponseWriter, r *http.Request) {
api := chi.URLParam(r, "api")

// check if api is in mapAPI
item, ok := apiMap[api]
if !ok {
app.errorLog.Println("api not found")
http.Error(w, "api not found", http.StatusNotFound)
return
}

app.writeJSON(w, http.StatusOK, item)
}

func (app *application) HandlerPreferences(w http.ResponseWriter, r *http.Request) {
settings, err := app.db.GetSettings()
if err != nil {
Expand Down
2 changes: 0 additions & 2 deletions app/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

const (
APIRoot = "/"
APIGetApi = "/api/{api}"
APIPreferences = "/preferences"
APILogbook = "/logbook"
APILogbookUUID = "/logbook/{uuid}"
Expand Down Expand Up @@ -231,7 +230,6 @@ func (app *application) routes() *chi.Mux {
server.Post(APILicensingAttachmentDelete, app.HandlerLicensingDeleteAttachment)

// api & parameters
server.Get(APIGetApi, app.HandlerGetApi)
server.Get(APIPreferences, app.HandlerPreferences)
})

Expand Down
16 changes: 1 addition & 15 deletions app/static/js/wlb-common.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,19 @@
"use strict";

const commonUtils = function () {
let api = {};
let preferences = {};

/**
* Fetches data from the API and caches it for future use.
* @param {string} apiItem - The API item to fetch.
* @returns {Promise<any>} - A promise that resolves to the fetched API data.
*/
const getApi = async (apiItem) => {
if (!api.hasOwnProperty(apiItem)) {
const result = await fetchJSON(`/api/${apiItem}`);
if (!result) {
console.error(`Error fetching API ${apiItem}.`);
return "";
}
api[apiItem] = result;
}
return api[apiItem];
return apiMap[apiItem];
}

/**
* Retrieves the preferences/settings for the app.
* @param {string} item - The preference item to retrieve.
* @returns {Promise<any>} - A promise that resolves to the value of the preference item, or null if it fails.
*/
const getPreferences = async (item) => {
if (!preferences.hasOwnProperty(item)) {
Expand All @@ -41,7 +30,6 @@ const commonUtils = function () {
/**
* Fetches JSON data from the specified URL.
* @param {string} url - The URL to fetch JSON data from.
* @returns {Promise<Object>} - A promise that resolves to the JSON data.
*/
const fetchJSON = async (url) => {
const options = {
Expand Down Expand Up @@ -123,7 +111,6 @@ const commonUtils = function () {
/**
* Runs the export process for the specified format.
* @param {string} format - The format to export (e.g., "pdf", "csv").
* @returns {Promise<void>} - A promise that resolves when the export process is complete.
*/
const runExport = async (format) => {
const api = await getApi("Export");
Expand Down Expand Up @@ -246,7 +233,6 @@ const commonUtils = function () {
* Sends a POST request to the specified URL with the given payload.
* @param {string} url - The URL to send the request to.
* @param {object} payload - The payload to include in the request body.
* @returns {Promise<object>} - A promise that resolves to the JSON response from the server.
*/
const postRequest = async (url, payload) => {
const options = {
Expand Down
7 changes: 7 additions & 0 deletions app/templates/base.gohtml
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,13 @@
</div>

<script src="/static/js/app.js"></script>
<script>
const apiMap = {
{{- range $key, $value := .API }}
"{{ $key }}": "{{ $value }}",
{{- end }}
};
</script>
<script type="text/javascript" src="/static/js/jquery-3.7.1.min.js"></script>
<script type="text/javascript" src="/static/js/moment.min.js"></script>
<script type="text/javascript" src="/static/js/daterangepicker.js"></script>
Expand Down

0 comments on commit ae4ffc4

Please sign in to comment.