-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add ipc server for browser extension
- Loading branch information
Showing
2 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters