-
Notifications
You must be signed in to change notification settings - Fork 78
/
ipc.go
59 lines (56 loc) · 1.45 KB
/
ipc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
import (
"encoding/json"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/cors"
"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.Use(cors.Handler(cors.Options{
AllowOriginFunc: func(r *http.Request, origin string) bool { return true },
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
}))
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(util.WithPath("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)
}
}