Skip to content

Commit

Permalink
add ipc server for browser extension
Browse files Browse the repository at this point in the history
  • Loading branch information
juzeon committed Jan 27, 2024
1 parent 3d19ddc commit 815c362
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
53 changes: 53 additions & 0 deletions ipc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package main

import (
"encoding/json"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"log/slog"
"net/http"
"os"
"sydneyqt/util"
)

type IPCServer struct {
mux *chi.Mux
settings *Settings
}

func NewIPCServer(settings *Settings) *IPCServer {
mux := chi.NewRouter()
server := &IPCServer{
mux: mux,
settings: settings,
}
server.registerRouters(mux)
return server
}
func (o *IPCServer) registerRouters(mux *chi.Mux) {
mux.Use(middleware.Logger)
mux.Use(middleware.Recoverer)
mux.Post("/cookies", func(writer http.ResponseWriter, request *http.Request) {
var cookies []util.FileCookie
err := json.NewDecoder(request.Body).Decode(&cookies)
if err != nil {
writer.WriteHeader(400)
slog.Error("Could not decode request", "err", err)
return
}
v, _ := json.MarshalIndent(&cookies, "", " ")
err = os.WriteFile("cookies.json", v, 0644)
if err != nil {
writer.WriteHeader(500)
slog.Error("Could write cookies.json", "err", err)
return
}
writer.WriteHeader(200)
})
}
func (o *IPCServer) Serve() {
err := http.ListenAndServe(":61989", o.mux)
if err != nil {
util.GracefulPanic(err)
}
}
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ var assets embed.FS
func main() {
// Create an instance of the app structure
settings := NewSettings()
ipcServer := NewIPCServer(settings)
go ipcServer.Serve()
app := NewApp(settings)

// Create application with options
Expand Down

0 comments on commit 815c362

Please # to comment.