Skip to content

Commit

Permalink
fix #18 to support debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
Ingo Jaeckel committed Mar 29, 2021
1 parent bfee793 commit 7cb99ec
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 0 deletions.
8 changes: 8 additions & 0 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Grid from '@material-ui/core/Grid';
import HomeIcon from '@material-ui/icons/Home';
import SettingsIcon from '@material-ui/icons/Settings';
import TimelineIcon from '@material-ui/icons/Timeline';
import DescriptionIcon from '@material-ui/icons/Description';
import { Container, List, Avatar, ListItemAvatar, ListItem, CssBaseline } from '@material-ui/core';
import Switcher from './Switch';
import axios from 'axios';
Expand Down Expand Up @@ -55,6 +56,13 @@ function App() {
</Avatar>
</ListItemAvatar>
</ListItem>
<ListItem to={`${process.env.PUBLIC_URL}/logs`} component={NavLink}>
<ListItemAvatar>
<Avatar>
<DescriptionIcon />
</Avatar>
</ListItemAvatar>
</ListItem>
</List>
</Grid>
<Grid item xs={10}>
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/Switch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import PhotoComponent from './components/PhotoComponent';
import MonitoringComponent from './components/MonitoringComponent';
import SetupComponent from './components/SetupComponent';
import PreviewComponent from './components/PreviewComponent';
import LogComponent from './components/LogComponent';

export default function Switcher() {
return (
Expand All @@ -20,6 +21,9 @@ export default function Switcher() {
<Route exact path={`${process.env.PUBLIC_URL}/settings`}>
<SetupComponent />
</Route>
<Route exact path={`${process.env.PUBLIC_URL}/logs`}>
<LogComponent />
</Route>
</Switch>
);
}
30 changes: 30 additions & 0 deletions frontend/src/components/LogComponent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React, { useState, useEffect } from 'react';
import axios from 'axios';
import { LogResponse } from '../models/response'
import { Typography, Button } from '@material-ui/core';
import { BaseUrl } from '../conf/config'

export default function LogComponent() {
const [logs, setLogs] = useState<string>("");

const getLogs = () => {
console.log("Getting logs...");
axios
.get<LogResponse>(BaseUrl + "/logs")
.then(resp => {
if (resp.data) {
setLogs(resp.data.Logs);
}
});
}

useEffect(() => getLogs(), []);

return (
<React.Fragment>
<Typography variant="h4" component="h4">Logs</Typography>
<Button onClick={() => getLogs()}>Refresh</Button>
<div><pre>{logs}</pre></div>
</React.Fragment>
);
}
4 changes: 4 additions & 0 deletions frontend/src/models/response.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ export interface SettingsResponse {
DebugEnabled: boolean
}

export interface LogResponse {
Logs: string
}

export interface PhotosResponse {
Photos: Photo[]
}
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func main() {

configUpdatedChan := make(chan conf.Settings)

mux.HandleFunc(pat.Get("/logs"), rest.GetLogs)
mux.HandleFunc(pat.Get("/photos"), rest.GetPhotos)
mux.HandleFunc(pat.Get("/monitoring"), rest.GetMonitoring)
mux.HandleFunc(pat.Get("/file"), rest.GetFiles)
Expand Down
4 changes: 4 additions & 0 deletions rest/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ type GetPhotosResponse struct {
Photos []Photo
}

type LogResponse struct {
Logs string
}

type Photo struct {
Name string
ModTime string
Expand Down
13 changes: 13 additions & 0 deletions rest/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package rest
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"

"github.com/ingojaeckel/go-raspberry-pi-timelapse/admin"
Expand All @@ -25,6 +26,18 @@ func GetMonitoring(w http.ResponseWriter, _ *http.Request) {
})
}

func GetLogs(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("content-type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*") // TODO limit to dev mode

logFileContent, err := ioutil.ReadFile(conf.LogFile)
if err == nil {
json.NewEncoder(w).Encode(LogResponse{Logs: string(logFileContent)})
} else {
writeJSON(w, http.StatusInternalServerError, err.Error())
}
}

func GetPhotos(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("content-type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*") // TODO limit to dev mode
Expand Down

0 comments on commit 7cb99ec

Please sign in to comment.