This commit is contained in:
pakintada@gmail.com 2023-09-25 16:17:21 +07:00
parent 9e8efa4cfc
commit d737eed480
5 changed files with 143 additions and 81 deletions

View file

@ -1,9 +1,10 @@
import { Component } from '@angular/core'; import { Component, OnInit } from '@angular/core';
import { MergeComponent } from '../merge/merge.component'; import { MergeComponent } from '../merge/merge.component';
import { HttpClient } from '@angular/common/http'; import { HttpClient } from '@angular/common/http';
import { environment } from 'src/environments/environment.development'; import { environment } from 'src/environments/environment.development';
import { CommonModule } from '@angular/common'; import { CommonModule } from '@angular/common';
import { FetchLogService } from 'src/app/shared/services/fetch-log.service'; import { FetchLogService } from 'src/app/shared/services/fetch-log.service';
import { WebsocketService } from 'src/app/shared/services/websocket.service';
@Component({ @Component({
selector: 'app-changelog', selector: 'app-changelog',
@ -21,52 +22,64 @@ export class ChangelogComponent {
constructor( constructor(
private httpClient: HttpClient, private httpClient: HttpClient,
private wss: WebsocketService
){ ){
this.fetchLoglist(); this.fetchLoglist();
this.translateLogDirToString(); // this.translateLogDirToString();
} }
public fetchLoglist(): string[]{ public fetchLoglist(): string[]{
// TODO: Fetch changelog.html // TODO: Fetch changelog.html
// fetch("/changelog") // fetch("/changelog")
let dirlist: any[] = [] let dirlist: any[] = []
this.httpClient.get(environment.api+"/listFileInDir/changelog", {
withCredentials: true
}).subscribe({
next: (value) => {
console.log(value)
if(typeof value === "object" && value !== null){
if("dirs" in value){
console.log("dirs", value)
// dirlist.push(...(value as {dirs: unknown[]})["dirs"]);
this.displayableLogs = value.dirs as any[];
}
}
},
error: (err) => {
console.error('Error fetch json: ',err);
}
})
// console.log(dirlist);
this.httpClient.get(environment.api+"/listFileInDir/merge", {
withCredentials: true this.wss.connect(environment.wsapi+"/listFileInDir");
}).subscribe({ this.wss.send({
next: (value) => { "topic": "changelog",
console.log(value)
if(typeof value === "object" && value !== null){
if("dirs" in value){
console.log("dirs", value)
// dirlist.push(...(value as {dirs: unknown[]})["dirs"]);
this.displayableMergeJson = value.dirs as any[];
}
}
},
error: (err) => {
console.error('Error fetch json: ',err);
}
}) })
let response = this.wss.data;
console.log(response);
// this.httpClient.get(environment.api+"/listFileInDir/changelog", {
// withCredentials: true
// }).subscribe({
// next: (value) => {
// console.log(value)
// if(typeof value === "object" && value !== null){
// if("dirs" in value){
// console.log("dirs", value)
// // dirlist.push(...(value as {dirs: unknown[]})["dirs"]);
// this.displayableLogs = value.dirs as any[];
// }
// }
// },
// error: (err) => {
// console.error('Error fetch json: ',err);
// }
// })
// // console.log(dirlist);
// this.httpClient.get(environment.api+"/listFileInDir/merge", {
// withCredentials: true
// }).subscribe({
// next: (value) => {
// console.log(value)
// if(typeof value === "object" && value !== null){
// if("dirs" in value){
// console.log("dirs", value)
// // dirlist.push(...(value as {dirs: unknown[]})["dirs"]);
// this.displayableMergeJson = value.dirs as any[];
// }
// }
// },
// error: (err) => {
// console.error('Error fetch json: ',err);
// }
// })
return dirlist; return dirlist;
} }

View file

@ -1,4 +1,5 @@
export const environment = { export const environment = {
production: false, production: false,
api: 'http://localhost:8080', api: 'http://localhost:8080',
wsapi: 'ws://localhost:8080'
}; };

View file

@ -8,6 +8,8 @@ require (
golang.org/x/oauth2 v0.12.0 golang.org/x/oauth2 v0.12.0
) )
require github.com/gorilla/websocket v1.5.0 // indirect
require ( require (
github.com/fsnotify/fsnotify v1.6.0 // indirect github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect github.com/golang/protobuf v1.5.3 // indirect

View file

@ -123,6 +123,8 @@ github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg= github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk= github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/googleapis/google-cloud-go-testing v0.0.0-20200911160855-bcd43fbb19e8/go.mod h1:dvDLG8qkwmyD9a/MJJN3XJcT3xFxOKAvTZGvuZmac9g= github.com/googleapis/google-cloud-go-testing v0.0.0-20200911160855-bcd43fbb19e8/go.mod h1:dvDLG8qkwmyD9a/MJJN3XJcT3xFxOKAvTZGvuZmac9g=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=

View file

@ -23,15 +23,21 @@ import (
"github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5"
"github.com/go-chi/cors" "github.com/go-chi/cors"
"github.com/gorilla/websocket"
"github.com/spf13/viper" "github.com/spf13/viper"
"go.uber.org/zap" "go.uber.org/zap"
"golang.org/x/oauth2" "golang.org/x/oauth2"
) )
var ( var (
Log = logger.GetInstance() Log = logger.GetInstance()
python_api_lock sync.Mutex python_api_lock sync.Mutex
upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool { return true },
}
) )
func loadConfig(path string) (*config.ServerConfig, error) { func loadConfig(path string) (*config.ServerConfig, error) {
@ -336,60 +342,98 @@ func (s *Server) createHandler() {
// json.NewEncoder(w).Encode(map[string][]string{"dirs": displayable}) // json.NewEncoder(w).Encode(map[string][]string{"dirs": displayable})
// }) // })
r.Get("/listFileInDir/*", func(w http.ResponseWriter, r *http.Request) { r.Get("/listFileInDir", func(w http.ResponseWriter, r *http.Request) {
// target_dir, err := os.ReadDir("cofffeemachineConfig") // socket
socket, err := upgrader.Upgrade(w, r, nil)
if err != nil {
Log.Error("Error while trying to upgrade socket: ", zap.Error(err))
return
}
// TODO: Change to websocket for better performance
for {
msg_type, msg, err := socket.ReadMessage()
if err != nil {
Log.Warn("Idle ", zap.Any("status", err))
return
}
var msgPayload map[string]interface{}
err = json.Unmarshal(msg, &msgPayload)
if err != nil {
Log.Error("Error while trying to unmarshal message: ", zap.Error(err))
return
}
// topic := msgPayload["topic"].(string)
Log.Info("Receive message: ", zap.Any("msg_type", msg_type), zap.Any("msg", msgPayload))
response, err := json.Marshal(map[string]interface{}{
"message": "ok",
})
if err != nil {
Log.Error("Error at marshalling response: ", zap.Error(err))
return
}
// respons to client
socket.WriteMessage(msg_type, response)
}
// spl // spl
spl_path := strings.Split(r.RequestURI, "/") // spl_path := strings.Split(r.RequestURI, "/")
if len(spl_path) > 3 { // if len(spl_path) > 3 {
Log.Warn("Unexpected depth: ", // Log.Warn("Unexpected depth: ",
zap.String("path", r.RequestURI), // zap.String("path", r.RequestURI),
zap.String("depth", fmt.Sprintf("%d", len(spl_path)))) // zap.String("depth", fmt.Sprintf("%d", len(spl_path))))
} // }
if spl_path[2] == "" { // if spl_path[2] == "" {
Log.Error("Empty target dir", zap.String("path", r.RequestURI)) // Log.Error("Empty target dir", zap.String("path", r.RequestURI))
} // }
Log.Debug("Split path = ", zap.Any("paths", spl_path)) // Log.Debug("Split path = ", zap.Any("paths", spl_path))
// Log.Info("Target dir: ", zap.String("dir", "cofffeemachineConfig")) // // Log.Info("Target dir: ", zap.String("dir", "cofffeemachineConfig"))
main_folder := "cofffeemachineConfig" // main_folder := "cofffeemachineConfig"
target_path := main_folder + "/" + spl_path[2] // target_path := main_folder + "/" + spl_path[2]
dir, err := os.ReadDir(target_path) // dir, err := os.ReadDir(target_path)
if err != nil { // if err != nil {
Log.Error("Error while trying to read dir: ", zap.String("dir", target_path), zap.Error(err)) // Log.Error("Error while trying to read dir: ", zap.String("dir", target_path), zap.Error(err))
http.Error(w, err.Error(), http.StatusInternalServerError) // http.Error(w, err.Error(), http.StatusInternalServerError)
} // }
// File ext // // File ext
file_ext := ".json" // file_ext := ".json"
switch spl_path[2] { // switch spl_path[2] {
case "changelog": // case "changelog":
file_ext = ".html" // file_ext = ".html"
break // break
case "merge": // case "merge":
file_ext = ".json" // file_ext = ".json"
break // break
} // }
Log.Debug("Set file ext = ", zap.String("file_ext", file_ext)) // Log.Debug("Set file ext = ", zap.String("file_ext", file_ext))
displayable := make([]string, 0) // displayable := make([]string, 0)
for _, file := range dir { // for _, file := range dir {
if strings.Contains(file.Name(), file_ext) { // if strings.Contains(file.Name(), file_ext) {
Log.Debug("Found file: ", zap.String("file", file.Name())) // Log.Debug("Found file: ", zap.String("file", file.Name()))
displayable = append(displayable, file.Name()[:len(file.Name())-len(filepath.Ext(file.Name()))]) // displayable = append(displayable, file.Name()[:len(file.Name())-len(filepath.Ext(file.Name()))])
} // }
} // }
// send back // // send back
w.Header().Set("Content-Type", "application/json") // w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK) // w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(map[string][]string{"dirs": displayable}) // json.NewEncoder(w).Encode(map[string][]string{"dirs": displayable})
Log.Debug("Scan dir completed < ", zap.String("path", r.RequestURI)) // Log.Debug("Scan dir completed < ", zap.String("path", r.RequestURI))
}) })
// Recipe Router // Recipe Router