From d6adc1c7754d09dc58e4dcd9a44f6bd4cdfbb5a3 Mon Sep 17 00:00:00 2001 From: arzumify Date: Tue, 29 Oct 2024 11:20:01 +0000 Subject: [PATCH] Added compression support, made all fetches use jsFetch for improved binary sizes Signed-off-by: arzumify --- go.mod | 21 +-- go.sum | 68 ++------ main.go | 155 ++++++++++++++++-- services-src/auth/main.go | 12 +- services-src/auth/resources/go.mod | 6 +- services-src/auth/resources/go.sum | 10 +- .../auth/resources/wasm/authorize/main.go | 23 +-- .../resources/wasm/clientKeyShare/main.go | 5 +- .../auth/resources/wasm/dashboard/main.go | 51 +++--- .../auth/resources/wasm/login/main.go | 25 +-- .../auth/resources/wasm/signup/main.go | 19 +-- .../auth/resources/wasm/testApp/main.go | 33 ++-- 12 files changed, 261 insertions(+), 167 deletions(-) diff --git a/go.mod b/go.mod index 2d07a5f..14947b3 100644 --- a/go.mod +++ b/go.mod @@ -3,40 +3,29 @@ module git.ailur.dev/ailur/fulgens go 1.23.1 require ( - git.ailur.dev/ailur/fg-library/v2 v2.1.0 - git.ailur.dev/ailur/fg-nucleus-library v1.0.2 - git.ailur.dev/ailur/pow v1.0.0 + git.ailur.dev/ailur/fg-library/v2 v2.1.1 + git.ailur.dev/ailur/fg-nucleus-library v1.0.3 + git.ailur.dev/ailur/pow v1.0.2 + github.com/andybalholm/brotli v1.1.1 github.com/cespare/xxhash/v2 v2.3.0 github.com/go-chi/chi/v5 v5.1.0 github.com/go-chi/hostrouter v0.2.0 github.com/go-playground/validator/v10 v10.22.1 github.com/golang-jwt/jwt/v5 v5.2.1 github.com/google/uuid v1.6.0 + github.com/klauspost/compress v1.17.11 github.com/lib/pq v1.10.9 github.com/mattn/go-sqlite3 v1.14.24 golang.org/x/crypto v0.28.0 - modernc.org/sqlite v1.33.1 ) require ( - github.com/dustin/go-humanize v1.0.1 // indirect github.com/gabriel-vasile/mimetype v1.4.6 // indirect github.com/go-playground/locales v0.14.1 // indirect github.com/go-playground/universal-translator v0.18.1 // indirect - github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect github.com/leodido/go-urn v1.4.0 // indirect - github.com/mattn/go-isatty v0.0.20 // indirect - github.com/ncruces/go-strftime v0.1.9 // indirect - github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect github.com/stretchr/testify v1.9.0 // indirect - golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c // indirect golang.org/x/net v0.30.0 // indirect golang.org/x/sys v0.26.0 // indirect golang.org/x/text v0.19.0 // indirect - modernc.org/gc/v3 v3.0.0-20241004144649-1aea3fae8852 // indirect - modernc.org/libc v1.61.0 // indirect - modernc.org/mathutil v1.6.0 // indirect - modernc.org/memory v1.8.0 // indirect - modernc.org/strutil v1.2.0 // indirect - modernc.org/token v1.1.0 // indirect ) diff --git a/go.sum b/go.sum index 431ce33..368b098 100644 --- a/go.sum +++ b/go.sum @@ -1,15 +1,17 @@ -git.ailur.dev/ailur/fg-library/v2 v2.1.0 h1:SsLZ56poM6GZPfV/ywU/8WDTelu2dtlPp6jzbEZ4hrA= -git.ailur.dev/ailur/fg-library/v2 v2.1.0/go.mod h1:1jYbWhabGcIwp7CkhHqvRwC8eP+nHv5BrXPe9NX2HE8= -git.ailur.dev/ailur/fg-nucleus-library v1.0.2 h1:EWfeab+wJKaxx/Qg5TKpvZHicA0V/NilUv2g6W97rtg= -git.ailur.dev/ailur/fg-nucleus-library v1.0.2/go.mod h1:T2mdUiXlZqb917CkNB2vwujkD/QhJDpCHLRvKuskBpY= -git.ailur.dev/ailur/pow v1.0.0 h1:eCJiZSbskcmzmwR4Nv4YrYpsZci5kfoGM9ihkXAHHoU= -git.ailur.dev/ailur/pow v1.0.0/go.mod h1:BHl7H6B6uN+q2cCCUlno6JMhqLa2A52wkbAdJbq2izA= +git.ailur.dev/ailur/fg-library/v2 v2.1.1 h1:49NgKud/gJjlTPZQfrZkmx4b/IEa3ZkO/NNM8QGA3qk= +git.ailur.dev/ailur/fg-library/v2 v2.1.1/go.mod h1:Il3+GZ7tClNx4/QSt3eY4tR7NdvO+Qf00J0PKkxPrl4= +git.ailur.dev/ailur/fg-nucleus-library v1.0.3 h1:C0xgfZg7bkULhh9Ci7ZoAcx4QIqxLh+QW9/ng1kKTFU= +git.ailur.dev/ailur/fg-nucleus-library v1.0.3/go.mod h1:RbBVFRwtQgYvCWoru1mC3vUJ1dMftkNbvd7hVFtREFw= +git.ailur.dev/ailur/pow v1.0.2 h1:8tb6mXZdyQYjrKRW+AUmWMi5wJoHh9Ch3oRqiJr/ivs= +git.ailur.dev/ailur/pow v1.0.2/go.mod h1:fjFb1z5KtF6V14HRhGWiDmmJKggO8KyAP20Lr5OJI/g= +github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs= +github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= +github.com/andybalholm/brotli v1.1.1 h1:PR2pgnyFznKEugtsUo0xLdDop5SKXd5Qf5ysW+7XdTA= +github.com/andybalholm/brotli v1.1.1/go.mod h1:05ib4cKhjx3OQYUY22hTVd34Bc8upXjOLL2rKwwZBoA= github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= -github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= github.com/gabriel-vasile/mimetype v1.4.6 h1:3+PzJTKLkvgjeTbts6msPJt4DixhT4YtFNf1gtGe3zc= github.com/gabriel-vasile/mimetype v1.4.6/go.mod h1:JX1qVKqZd40hUPpAfiNTe0Sne7hdfKSbOqqmkq8GCXc= github.com/go-chi/chi/v5 v5.0.0/go.mod h1:BBug9lr0cqtdAhsu6R4AAdvufI0/XBzAQSsUqJpoZOs= @@ -27,70 +29,30 @@ github.com/go-playground/validator/v10 v10.22.1 h1:40JcKH+bBNGFczGuoBYgX4I6m/i27 github.com/go-playground/validator/v10 v10.22.1/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM= github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk= github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk= -github.com/google/pprof v0.0.0-20240409012703-83162a5b38cd h1:gbpYu9NMq8jhDVbvlGkMFWCjLFlqqEZjEmObmhUy6Vo= -github.com/google/pprof v0.0.0-20240409012703-83162a5b38cd/go.mod h1:kf6iHlnVGwgKolg33glAes7Yg/8iWP8ukqeldJSO7jw= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k= -github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM= +github.com/klauspost/compress v1.16.7 h1:2mk3MPGNzKyxErAw8YaohYh69+pa4sIQSC0fPGCFR9I= +github.com/klauspost/compress v1.16.7/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= +github.com/klauspost/compress v1.17.11 h1:In6xLpyWOi1+C7tXUUWv2ot1QvBjxevKAaI6IXrJmUc= +github.com/klauspost/compress v1.17.11/go.mod h1:pMDklpSncoRMuLFrf1W9Ss9KT+0rH90U12bZKk7uwG0= github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= -github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= -github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-sqlite3 v1.14.24 h1:tpSp2G2KyMnnQu99ngJ47EIkWVmliIizyZBfPrBWDRM= github.com/mattn/go-sqlite3 v1.14.24/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= -github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdhx/f4= -github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE= -github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/xyproto/randomstring v1.0.5/go.mod h1:rgmS5DeNXLivK7YprL0pY+lTuhNQW3iGxZ18UQApw/E= golang.org/x/crypto v0.28.0 h1:GBDwsMXVQi34v5CCYUm2jkJvu4cbtru2U4TN2PSyQnw= golang.org/x/crypto v0.28.0/go.mod h1:rmgy+3RHxRZMyY0jjAJShp2zgEdOqj2AO7U0pYmeQ7U= -golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c h1:7dEasQXItcW1xKJ2+gg5VOiBnqWrJc+rq0DPKyvvdbY= -golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c/go.mod h1:NQtJDoLvd6faHhE7m4T/1IY708gDefGGjR/iUW8yQQ8= -golang.org/x/mod v0.21.0 h1:vvrHzRwRfVKSiLrG+d4FMl/Qi4ukBCE6kZlTUkDYRT0= -golang.org/x/mod v0.21.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY= golang.org/x/net v0.30.0 h1:AcW1SDZMkb8IpzCdQUaIq2sP4sZ4zw+55h6ynffypl4= golang.org/x/net v0.30.0/go.mod h1:2wGyMJ5iFasEhkwi13ChkO/t1ECNC4X4eBKkVFyYFlU= -golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ= -golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= -golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo= golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM= golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= -golang.org/x/tools v0.26.0 h1:v/60pFQmzmT9ExmjDv2gGIfi3OqfKoEP6I5+umXlbnQ= -golang.org/x/tools v0.26.0/go.mod h1:TPVVj70c7JJ3WCazhD8OdXcZg/og+b9+tH/KxylGwH0= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -modernc.org/cc/v4 v4.21.4 h1:3Be/Rdo1fpr8GrQ7IVw9OHtplU4gWbb+wNgeoBMmGLQ= -modernc.org/cc/v4 v4.21.4/go.mod h1:HM7VJTZbUCR3rV8EYBi9wxnJ0ZBRiGE5OeGXNA0IsLQ= -modernc.org/ccgo/v4 v4.21.0 h1:kKPI3dF7RIag8YcToh5ZwDcVMIv6VGa0ED5cvh0LMW4= -modernc.org/ccgo/v4 v4.21.0/go.mod h1:h6kt6H/A2+ew/3MW/p6KEoQmrq/i3pr0J/SiwiaF/g0= -modernc.org/fileutil v1.3.0 h1:gQ5SIzK3H9kdfai/5x41oQiKValumqNTDXMvKo62HvE= -modernc.org/fileutil v1.3.0/go.mod h1:XatxS8fZi3pS8/hKG2GH/ArUogfxjpEKs3Ku3aK4JyQ= -modernc.org/gc/v2 v2.5.0 h1:bJ9ChznK1L1mUtAQtxi0wi5AtAs5jQuw4PrPHO5pb6M= -modernc.org/gc/v2 v2.5.0/go.mod h1:wzN5dK1AzVGoH6XOzc3YZ+ey/jPgYHLuVckd62P0GYU= -modernc.org/gc/v3 v3.0.0-20241004144649-1aea3fae8852 h1:IYXPPTTjjoSHvUClZIYexDiO7g+4x+XveKT4gCIAwiY= -modernc.org/gc/v3 v3.0.0-20241004144649-1aea3fae8852/go.mod h1:Qz0X07sNOR1jWYCrJMEnbW/X55x206Q7Vt4mz6/wHp4= -modernc.org/libc v1.61.0 h1:eGFcvWpqlnoGwzZeZe3PWJkkKbM/3SUGyk1DVZQ0TpE= -modernc.org/libc v1.61.0/go.mod h1:DvxVX89wtGTu+r72MLGhygpfi3aUGgZRdAYGCAVVud0= -modernc.org/mathutil v1.6.0 h1:fRe9+AmYlaej+64JsEEhoWuAYBkOtQiMEU7n/XgfYi4= -modernc.org/mathutil v1.6.0/go.mod h1:Ui5Q9q1TR2gFm0AQRqQUaBWFLAhQpCwNcuhBOSedWPo= -modernc.org/memory v1.8.0 h1:IqGTL6eFMaDZZhEWwcREgeMXYwmW83LYW8cROZYkg+E= -modernc.org/memory v1.8.0/go.mod h1:XPZ936zp5OMKGWPqbD3JShgd/ZoQ7899TUuQqxY+peU= -modernc.org/opt v0.1.3 h1:3XOZf2yznlhC+ibLltsDGzABUGVx8J6pnFMS3E4dcq4= -modernc.org/opt v0.1.3/go.mod h1:WdSiB5evDcignE70guQKxYUl14mgWtbClRi5wmkkTX0= -modernc.org/sortutil v1.2.0 h1:jQiD3PfS2REGJNzNCMMaLSp/wdMNieTbKX920Cqdgqc= -modernc.org/sortutil v1.2.0/go.mod h1:TKU2s7kJMf1AE84OoiGppNHJwvB753OYfNl2WRb++Ss= -modernc.org/sqlite v1.33.1 h1:trb6Z3YYoeM9eDL1O8do81kP+0ejv+YzgyFo+Gwy0nM= -modernc.org/sqlite v1.33.1/go.mod h1:pXV2xHxhzXZsgT/RtTFAPY6JJDEvOTcTdwADQCCWD4k= -modernc.org/strutil v1.2.0 h1:agBi9dp1I+eOnxXeiZawM8F4LawKv4NzGWSaLfyeNZA= -modernc.org/strutil v1.2.0/go.mod h1:/mdcBmfOibveCTBxUl5B5l6W+TTH1FXPLHZE6bTosX0= -modernc.org/token v1.1.0 h1:Xl7Ap9dKaEs5kLoOQeQmPWevfnk/DM5qcLcYlA8ys6Y= -modernc.org/token v1.1.0/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM= diff --git a/main.go b/main.go index 1f2e514..c96b011 100644 --- a/main.go +++ b/main.go @@ -1,8 +1,9 @@ package main import ( - "errors" library "git.ailur.dev/ailur/fg-library/v2" + + "errors" "io" "io/fs" "log" @@ -13,16 +14,19 @@ import ( "sync" "time" + "compress/gzip" "database/sql" "encoding/json" "log/slog" "net/http" "path/filepath" + "github.com/andybalholm/brotli" "github.com/go-chi/chi/v5" "github.com/go-chi/hostrouter" "github.com/go-playground/validator/v10" "github.com/google/uuid" + "github.com/klauspost/compress/zstd" _ "github.com/lib/pq" _ "github.com/mattn/go-sqlite3" @@ -30,10 +34,13 @@ import ( type Config struct { Global struct { - IP string `json:"ip" validate:"required,ip_addr"` - Port string `json:"port" validate:"required"` - ServiceDirectory string `json:"serviceDirectory" validate:"required"` - ResourceDirectory string `json:"resourceDirectory" validate:"required"` + IP string `json:"ip" validate:"required,ip_addr"` + Port string `json:"port" validate:"required"` + ServiceDirectory string `json:"serviceDirectory" validate:"required"` + ResourceDirectory string `json:"resourceDirectory" validate:"required"` + Compression string `json:"compression" validate:"omitempty,oneof=gzip brotli zstd"` + CompressionLevelJN json.Number `json:"compressionLevel" validate:"required_with=Compression"` + CompressionLevel int } `json:"global" validate:"required"` Logging struct { Enabled bool `json:"enabled"` @@ -58,6 +65,19 @@ type Service struct { Inbox chan library.InterServiceMessage } +type ResponseWriterWrapper struct { + http.ResponseWriter + io.Writer +} + +func (w *ResponseWriterWrapper) WriteHeader(statusCode int) { + w.ResponseWriter.WriteHeader(statusCode) +} + +func (w *ResponseWriterWrapper) Write(p []byte) (int, error) { + return w.Writer.Write(p) +} + var ( logger = func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { @@ -71,13 +91,104 @@ var ( next.ServeHTTP(w, r) }) } + gzipHandler = func(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") { + gzipWriter, err := gzip.NewWriterLevel(w, config.Global.CompressionLevel) + if err != nil { + slog.Error("Error creating gzip writer: ", err) + next.ServeHTTP(w, r) + return + } + if w.Header().Get("Content-Encoding") != "" { + w.Header().Set("Content-Encoding", w.Header().Get("Content-Encoding")+", gzip") + } else { + w.Header().Set("Content-Encoding", "gzip") + } + defer func() { + w.Header().Del("Content-Length") + err := gzipWriter.Close() + if errors.Is(err, http.ErrBodyNotAllowed) { + // This is fine, all it means is that they have it cached, and we don't need to send it + return + } else if err != nil { + slog.Error("Error closing gzip writer: ", err) + } + }() + gzipResponseWriter := &ResponseWriterWrapper{ResponseWriter: w, Writer: gzipWriter} + next.ServeHTTP(gzipResponseWriter, r) + } else { + next.ServeHTTP(w, r) + } + }) + } + brotliHandler = func(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if strings.Contains(r.Header.Get("Accept-Encoding"), "br") { + brotliWriter := brotli.NewWriterV2(w, config.Global.CompressionLevel) + if w.Header().Get("Content-Encoding") != "" { + w.Header().Set("Content-Encoding", w.Header().Get("Content-Encoding")+", br") + } else { + w.Header().Set("Content-Encoding", "br") + } + defer func() { + w.Header().Del("Content-Length") + err := brotliWriter.Close() + if errors.Is(err, http.ErrBodyNotAllowed) { + // This is fine, all it means is that they have it cached, and we don't need to send it + return + } else if err != nil { + slog.Error("Error closing Brotli writer: ", err) + } + }() + brotliResponseWriter := &ResponseWriterWrapper{ResponseWriter: w, Writer: brotliWriter} + next.ServeHTTP(brotliResponseWriter, r) + } else { + next.ServeHTTP(w, r) + } + }) + } + zStandardHandler = func(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if strings.Contains(r.Header.Get("Accept-Encoding"), "zstd") { + zStandardWriter, err := zstd.NewWriter(w, zstd.WithEncoderLevel(zstd.EncoderLevelFromZstd(config.Global.CompressionLevel))) + if err != nil { + slog.Error("Error creating ZStandard writer: ", err) + next.ServeHTTP(w, r) + return + } + if w.Header().Get("Content-Encoding") != "" { + w.Header().Set("Content-Encoding", w.Header().Get("Content-Encoding")+", zstd") + } else { + w.Header().Set("Content-Encoding", "zstd") + } + defer func() { + w.Header().Del("Content-Length") + err := zStandardWriter.Close() + if err != nil { + if errors.Is(err, http.ErrBodyNotAllowed) { + // This is fine, all it means is that they have it cached, and we don't need to send it + return + } else { + slog.Error("Error closing ZStandard writer: ", err) + } + } + }() + gzipResponseWriter := &ResponseWriterWrapper{ResponseWriter: w, Writer: zStandardWriter} + next.ServeHTTP(gzipResponseWriter, r) + } else { + next.ServeHTTP(w, r) + } + }) + } validate *validator.Validate services = make(map[uuid.UUID]Service) lock sync.RWMutex hostRouter = hostrouter.New() + config Config ) -func processInterServiceMessage(channel chan library.InterServiceMessage, config Config) { +func processInterServiceMessage(channel chan library.InterServiceMessage) { for { message := <-channel if message.ForServiceID == uuid.MustParse("00000000-0000-0000-0000-000000000000") { @@ -411,7 +522,7 @@ func parseConfig(path string) Config { } // Parse the configuration file - configFile, err := os.ReadFile(path) + configFile, err := os.Open(path) if err != nil { slog.Error("Error reading configuration file: ", err) os.Exit(1) @@ -419,12 +530,23 @@ func parseConfig(path string) Config { // Parse the configuration file var config Config - err = json.Unmarshal(configFile, &config) + decoder := json.NewDecoder(configFile) + decoder.UseNumber() + err = decoder.Decode(&config) if err != nil { slog.Error("Error parsing configuration file: ", err) os.Exit(1) } + // Set the compression level + compressionLevelI64, err := config.Global.CompressionLevelJN.Int64() + if err != nil { + slog.Error("Error parsing compression level: ", err) + os.Exit(1) + } + + config.Global.CompressionLevel = int(compressionLevelI64) + // Validate the configuration err = validate.Struct(config) if err != nil { @@ -452,7 +574,6 @@ func parseConfig(path string) Config { func main() { // Parse the configuration file - var config Config if len(os.Args) < 2 { info, err := os.Stat("config.json") if err != nil { @@ -520,7 +641,7 @@ func main() { // Initialize the service discovery, health-check, and logging services // Since these are core services, always allocate them the service IDs 0, 1, and 2 // These are not dynamically loaded, as they are integral to the system functioning - go processInterServiceMessage(globalOutbox, config) + go processInterServiceMessage(globalOutbox) // Initialize all the services plugins := make(map[time.Time]string) @@ -656,7 +777,19 @@ func main() { // Start the server slog.Info("Starting server on " + config.Global.IP + ":" + config.Global.Port) - err = http.ListenAndServe(config.Global.IP+":"+config.Global.Port, router) + switch config.Global.Compression { + case "": + err = http.ListenAndServe(config.Global.IP+":"+config.Global.Port, router) + case "gzip": + slog.Info("GZip compression enabled") + err = http.ListenAndServe(config.Global.IP+":"+config.Global.Port, gzipHandler(router)) + case "brotli": + slog.Info("Brotli compression enabled") + err = http.ListenAndServe(config.Global.IP+":"+config.Global.Port, brotliHandler(router)) + case "zstd": + slog.Info("ZStandard compression enabled") + err = http.ListenAndServe(config.Global.IP+":"+config.Global.Port, zStandardHandler(router)) + } if err != nil { slog.Error("Error starting server: ", err) os.Exit(1) diff --git a/services-src/auth/main.go b/services-src/auth/main.go index c3df80c..efec68e 100644 --- a/services-src/auth/main.go +++ b/services-src/auth/main.go @@ -32,7 +32,7 @@ import ( "github.com/cespare/xxhash/v2" "github.com/golang-jwt/jwt/v5" "github.com/google/uuid" - _ "modernc.org/sqlite" + _ "github.com/mattn/go-sqlite3" ) var ServiceInformation = library.Service{ @@ -100,9 +100,8 @@ func renderTemplate(statusCode int, w http.ResponseWriter, data map[string]inter requestedTemplate, err = template.ParseFS(information.ResourceDir, "templates/"+templatePath) if err != nil { logFunc(err.Error(), 2, information) - renderString(500, w, "Sorry, something went wrong on our end. Error code: 01. Please report to the administrator.", information) + http.Error(w, err.Error(), 500) } else { - w.WriteHeader(statusCode) if strings.HasSuffix(templatePath, ".html") { w.Header().Set("Content-Type", "text/html") } else if strings.HasSuffix(templatePath, ".json") { @@ -110,10 +109,11 @@ func renderTemplate(statusCode int, w http.ResponseWriter, data map[string]inter } else { w.Header().Set("Content-Type", "text/plain") } + w.WriteHeader(statusCode) err = requestedTemplate.Execute(w, data) if err != nil { logFunc(err.Error(), 2, information) - renderString(500, w, "Sorry, something went wrong on our end. Error code: 02. Please report to the administrator.", information) + http.Error(w, err.Error(), 500) } } } @@ -247,7 +247,7 @@ func Main(information library.ServiceInitializationInformation) { } } // Set up the in-memory cache - mem, err = sql.Open("sqlite", "file:"+ServiceInformation.ServiceID.String()+"?mode=memory&cache=shared") + mem, err = sql.Open("sqlite3", "file:"+ServiceInformation.ServiceID.String()+"?mode=memory&cache=shared") if err != nil { logFunc(err.Error(), 3, information) } @@ -416,7 +416,7 @@ func Main(information library.ServiceInitializationInformation) { renderString(404, w, "App not found", information) } else { logFunc(err.Error(), 2, information) - renderString(500, w, "Sorry, something went wrong on our end. Error code: 03. Please report to the administrator.", information) + renderString(500, w, "Sorry, something went wrong on our end. Error code: 02. Please report to the administrator.", information) } return } diff --git a/services-src/auth/resources/go.mod b/services-src/auth/resources/go.mod index eed9952..54c8a3c 100644 --- a/services-src/auth/resources/go.mod +++ b/services-src/auth/resources/go.mod @@ -3,8 +3,12 @@ module git.ailur.dev/fulgens/services-src/auth/resources go 1.23.0 require ( + git.ailur.dev/ailur/jsFetch v1.1.1 github.com/cespare/xxhash/v2 v2.3.0 golang.org/x/crypto v0.28.0 ) -require golang.org/x/sys v0.26.0 // indirect +require ( + git.ailur.dev/ailur/jsStreams v1.2.1 // indirect + golang.org/x/sys v0.26.0 // indirect +) diff --git a/services-src/auth/resources/go.sum b/services-src/auth/resources/go.sum index e7108a9..738504e 100644 --- a/services-src/auth/resources/go.sum +++ b/services-src/auth/resources/go.sum @@ -1,10 +1,12 @@ +git.ailur.dev/ailur/jsFetch v1.1.1 h1:kdCkrNr2mRvTG6hlK3YwnqlwfvzIQaw4z4AXLXewQ38= +git.ailur.dev/ailur/jsFetch v1.1.1/go.mod h1:eaQVFOlHwcPHCqh3oyQkQrpltmILOaiA9DKq3oTHBbM= +git.ailur.dev/ailur/jsStreams v1.2.0 h1:BRtLEyjkUoPKPu0Y6odUbSMlKCYNyR792TYRtujKfPw= +git.ailur.dev/ailur/jsStreams v1.2.0/go.mod h1:/ZCvbUcWkZRuKIkO7jH6b5vIjzdxIOP8ET8X0src5Go= +git.ailur.dev/ailur/jsStreams v1.2.1 h1:nXZYZrxHJCVwR0Kx/X+TenMBmS6Gh8Uc2DMinbyiGoo= +git.ailur.dev/ailur/jsStreams v1.2.1/go.mod h1:/ZCvbUcWkZRuKIkO7jH6b5vIjzdxIOP8ET8X0src5Go= github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= -golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A= -golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70= golang.org/x/crypto v0.28.0 h1:GBDwsMXVQi34v5CCYUm2jkJvu4cbtru2U4TN2PSyQnw= golang.org/x/crypto v0.28.0/go.mod h1:rmgy+3RHxRZMyY0jjAJShp2zgEdOqj2AO7U0pYmeQ7U= -golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34= -golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo= golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= diff --git a/services-src/auth/resources/wasm/authorize/main.go b/services-src/auth/resources/wasm/authorize/main.go index c442601..1915e95 100644 --- a/services-src/auth/resources/wasm/authorize/main.go +++ b/services-src/auth/resources/wasm/authorize/main.go @@ -2,13 +2,14 @@ package main import ( "bytes" - "encoding/json" - "fmt" - "net/http" - "net/url" "strings" - "syscall/js" "time" + + "encoding/json" + "net/url" + "syscall/js" + + "git.ailur.dev/ailur/jsFetch" ) func authorize(deny bool, query url.Values, sleepTime time.Duration) { @@ -49,7 +50,7 @@ func authorize(deny bool, query url.Values, sleepTime time.Duration) { } // Send the request - response, err := http.Post(requestUri, "application/json", bytes.NewReader(body)) + response, err := jsFetch.Post(requestUri, "application/json", bytes.NewReader(body)) if err != nil { js.Global().Get("document").Call("getElementById", "statusBox").Set("innerText", "Error contacting server: "+err.Error()) return @@ -69,7 +70,7 @@ func authorize(deny bool, query url.Values, sleepTime time.Duration) { // Close the response body err = response.Body.Close() if err != nil { - fmt.Println("Could not close response body: " + err.Error() + ", memory leaks may occur") + println("Could not close response body: " + err.Error() + ", memory leaks may occur") } if response.StatusCode == 200 { @@ -154,7 +155,7 @@ func main() { return } - response, err := http.Post(requestUri, "application/json", bytes.NewReader(body)) + response, err := jsFetch.Post(requestUri, "application/json", bytes.NewReader(body)) if err != nil { statusBox.Set("innerText", "Error contacting server: "+err.Error()) return @@ -165,7 +166,7 @@ func main() { // Close the response body err = response.Body.Close() if err != nil { - fmt.Println("Could not close response body: " + err.Error() + ", memory leaks may occur") + println("Could not close response body: " + err.Error() + ", memory leaks may occur") } // Redirect to log-out if not signed in @@ -186,7 +187,7 @@ func main() { // Close the response body err = response.Body.Close() if err != nil { - fmt.Println("Could not close response body: " + err.Error() + ", memory leaks may occur") + println("Could not close response body: " + err.Error() + ", memory leaks may occur") } // Alert the user if the server is down @@ -197,7 +198,7 @@ func main() { // Close the response body err = response.Body.Close() if err != nil { - fmt.Println("Could not close response body: " + err.Error() + ", memory leaks may occur") + println("Could not close response body: " + err.Error() + ", memory leaks may occur") } if autoAccept.Get("innerText").String() == "0" { diff --git a/services-src/auth/resources/wasm/clientKeyShare/main.go b/services-src/auth/resources/wasm/clientKeyShare/main.go index 5a984aa..d81c8e9 100644 --- a/services-src/auth/resources/wasm/clientKeyShare/main.go +++ b/services-src/auth/resources/wasm/clientKeyShare/main.go @@ -1,15 +1,16 @@ package main import ( + "strings" + "time" + "crypto/aes" "crypto/cipher" "crypto/ecdh" "crypto/rand" "encoding/base64" "net/url" - "strings" "syscall/js" - "time" ) func main() { diff --git a/services-src/auth/resources/wasm/dashboard/main.go b/services-src/auth/resources/wasm/dashboard/main.go index a08433e..3a08b20 100644 --- a/services-src/auth/resources/wasm/dashboard/main.go +++ b/services-src/auth/resources/wasm/dashboard/main.go @@ -2,13 +2,14 @@ package main import ( "bytes" - "encoding/json" - "fmt" - "net/http" - "net/url" "strings" - "syscall/js" "time" + + "encoding/json" + "net/url" + "syscall/js" + + "git.ailur.dev/ailur/jsFetch" ) func fetchOauthClients(oauthList js.Value, localStorage js.Value, body []byte) { @@ -21,7 +22,7 @@ func fetchOauthClients(oauthList js.Value, localStorage js.Value, body []byte) { return } - response, err := http.Post(requestUri, "application/json", bytes.NewReader(body)) + response, err := jsFetch.Post(requestUri, "application/json", bytes.NewReader(body)) if err != nil { var statusText = js.Global().Get("document").Call("createElement", "p") statusText.Set("innerText", "Error contacting server: "+err.Error()) @@ -45,7 +46,7 @@ func fetchOauthClients(oauthList js.Value, localStorage js.Value, body []byte) { // Close the response body err = response.Body.Close() if err != nil { - fmt.Println("Could not close response body: " + err.Error() + ", memory leaks may occur") + println("Could not close response body: " + err.Error() + ", memory leaks may occur") } if response.StatusCode == 200 { @@ -95,7 +96,7 @@ func fetchOauthClients(oauthList js.Value, localStorage js.Value, body []byte) { return } - response, err := http.Post(requestUri, "application/json", bytes.NewReader(bodyBytes)) + response, err := jsFetch.Post(requestUri, "application/json", bytes.NewReader(bodyBytes)) if err != nil { js.Global().Call("alert", "Error contacting server: "+err.Error()) return @@ -115,7 +116,7 @@ func fetchOauthClients(oauthList js.Value, localStorage js.Value, body []byte) { // Close the response body err = response.Body.Close() if err != nil { - fmt.Println("Could not close response body: " + err.Error() + ", memory leaks may occur") + println("Could not close response body: " + err.Error() + ", memory leaks may occur") } if response.StatusCode == 200 { @@ -246,7 +247,7 @@ func main() { return } - response, err := http.Post(requestUri, "application/json", bytes.NewReader(body)) + response, err := jsFetch.Post(requestUri, "application/json", bytes.NewReader(body)) if err != nil { js.Global().Call("alert", "Error contacting server: "+err.Error()) return @@ -257,7 +258,7 @@ func main() { // Close the response body err = response.Body.Close() if err != nil { - fmt.Println("Could not close response body: " + err.Error() + ", memory leaks may occur") + println("Could not close response body: " + err.Error() + ", memory leaks may occur") } // Redirect to log-out if not signed in @@ -278,7 +279,7 @@ func main() { // Close the response body err = response.Body.Close() if err != nil { - fmt.Println("Could not close response body: " + err.Error() + ", memory leaks may occur") + println("Could not close response body: " + err.Error() + ", memory leaks may occur") } // Alert the user if the server is down @@ -289,7 +290,7 @@ func main() { // Close the response body err = response.Body.Close() if err != nil { - fmt.Println("Could not close response body: " + err.Error() + ", memory leaks may occur") + println("Could not close response body: " + err.Error() + ", memory leaks may occur") } // Fetch the OAuth clients @@ -304,7 +305,7 @@ func main() { return } - response, err = http.Post(requestUri, "application/json", bytes.NewReader(body)) + response, err = jsFetch.Post(requestUri, "application/json", bytes.NewReader(body)) if err != nil { var statusText = js.Global().Get("document").Call("createElement", "p") statusText.Set("innerText", "Error contacting server: "+err.Error()) @@ -328,7 +329,7 @@ func main() { // Close the response body err = response.Body.Close() if err != nil { - fmt.Println("Could not close response body: " + err.Error() + ", memory leaks may occur") + println("Could not close response body: " + err.Error() + ", memory leaks may occur") } if response.StatusCode == 200 { @@ -388,7 +389,7 @@ func main() { return } - response, err := http.Post(requestUri, "application/json", bytes.NewReader(bodyBytes)) + response, err := jsFetch.Post(requestUri, "application/json", bytes.NewReader(bodyBytes)) if err != nil { js.Global().Call("alert", "Error contacting server: "+err.Error()) return @@ -408,7 +409,7 @@ func main() { // Close the response body err = response.Body.Close() if err != nil { - fmt.Println("Could not close response body: " + err.Error() + ", memory leaks may occur") + println("Could not close response body: " + err.Error() + ", memory leaks may occur") } if response.StatusCode == 200 { @@ -453,7 +454,7 @@ func main() { } // Re-use the body variable for this request - response, err = http.Post(requestUri, "application/json", bytes.NewReader(body)) + response, err = jsFetch.Post(requestUri, "application/json", bytes.NewReader(body)) if err != nil { js.Global().Call("alert", "Error contacting server: "+err.Error()) return @@ -526,7 +527,7 @@ func main() { return } - response, err := http.Post(requestUri, "application/json", bytes.NewReader(bodyBytes)) + response, err := jsFetch.Post(requestUri, "application/json", bytes.NewReader(bodyBytes)) if err != nil { statusBox.Set("innerText", "Error contacting server: "+err.Error()) return @@ -546,7 +547,7 @@ func main() { // Close the response body err = response.Body.Close() if err != nil { - fmt.Println("Could not close response body: " + err.Error() + ", memory leaks may occur") + println("Could not close response body: " + err.Error() + ", memory leaks may occur") } if response.StatusCode == 200 { @@ -560,7 +561,7 @@ func main() { // Marshal the body body, err := json.Marshal(bodyMap) if err != nil { - fmt.Println("Error marshaling body: " + err.Error() + ", this is non-fatal.") + println("Error marshaling body: " + err.Error() + ", this is non-fatal.") return } @@ -597,7 +598,7 @@ func main() { return } - response, err := http.Post(requestUri, "application/json", bytes.NewReader(bodyBytes)) + response, err := jsFetch.Post(requestUri, "application/json", bytes.NewReader(bodyBytes)) if err != nil { statusBox.Set("innerText", "Error contacting server: "+err.Error()) return @@ -617,7 +618,7 @@ func main() { // Close the response body err = response.Body.Close() if err != nil { - fmt.Println("Could not close response body: " + err.Error() + ", memory leaks may occur") + println("Could not close response body: " + err.Error() + ", memory leaks may occur") } if response.StatusCode == 200 { @@ -668,7 +669,7 @@ func main() { } // Send the request - response, err := http.Post(requestUri, "application/json", bytes.NewReader(bodyBytes)) + response, err := jsFetch.Post(requestUri, "application/json", bytes.NewReader(bodyBytes)) if err != nil { js.Global().Call("alert", "Error contacting server: "+err.Error()) return @@ -688,7 +689,7 @@ func main() { // Close the response body err = response.Body.Close() if err != nil { - fmt.Println("Could not close response body: " + err.Error() + ", memory leaks may occur") + println("Could not close response body: " + err.Error() + ", memory leaks may occur") } // We don't care about the response, we're logging out anyway diff --git a/services-src/auth/resources/wasm/login/main.go b/services-src/auth/resources/wasm/login/main.go index dd32b76..af3a389 100644 --- a/services-src/auth/resources/wasm/login/main.go +++ b/services-src/auth/resources/wasm/login/main.go @@ -2,17 +2,18 @@ package main import ( "bytes" + "strings" + "time" + "crypto/ed25519" "encoding/base64" "encoding/json" - "fmt" - "net/http" "net/url" - "strings" "syscall/js" - "time" "golang.org/x/crypto/argon2" + + "git.ailur.dev/ailur/jsFetch" ) var currentInputType = 0 @@ -134,7 +135,7 @@ func main() { // Hash the password statusBox.Set("innerText", "Hashing password...") - fmt.Println("Hashing password...") + println("Hashing password...") // Fetch the challenge from the server body, err := json.Marshal(map[string]interface{}{ @@ -154,7 +155,7 @@ func main() { return } - response, err := http.Post(requestUri, "application/json", bytes.NewReader(body)) + response, err := jsFetch.Post(requestUri, "application/json", bytes.NewReader(body)) if err != nil { showInput(1, inputContainer, usernameBox, signupButton, passwordBox, backButton, inputNameBox, statusBox, nextButton) statusBox.Set("innerText", "Error contacting server: "+err.Error()) @@ -176,7 +177,7 @@ func main() { // Close the response body err = response.Body.Close() if err != nil { - fmt.Println("Could not close response body: " + err.Error() + ", memory leaks may occur") + println("Could not close response body: " + err.Error() + ", memory leaks may occur") } if response.StatusCode == 200 { @@ -210,8 +211,8 @@ func main() { } // Send the request - fmt.Println("Sending request to", requestUri) - response, err = http.Post(requestUri, "application/json", bytes.NewReader(body)) + println("Sending request to", requestUri) + response, err = jsFetch.Post(requestUri, "application/json", bytes.NewReader(body)) if err != nil { showInput(1, inputContainer, usernameBox, signupButton, passwordBox, backButton, inputNameBox, statusBox, nextButton) statusBox.Set("innerText", "Error contacting server: "+err.Error()) @@ -219,7 +220,7 @@ func main() { } // Read the response - fmt.Println("Reading response...") + println("Reading response...") decoder = json.NewDecoder(response.Body) err = decoder.Decode(&responseMap) if err != nil { @@ -231,12 +232,12 @@ func main() { // Close the response body err = response.Body.Close() if err != nil { - fmt.Println("Could not close response body: " + err.Error() + ", memory leaks may occur") + println("Could not close response body: " + err.Error() + ", memory leaks may occur") } if response.StatusCode == 200 { // Logged in - fmt.Println("Logged in!") + println("Logged in!") statusBox.Set("innerText", "Setting up encryption keys...") localStorage.Call("setItem", "DONOTSHARE-secretKey", responseMap["key"].(string)) localStorage.Call("setItem", "DONOTSHARE-clientKey", base64.StdEncoding.EncodeToString(hashPassword(password, []byte("fg-auth-client")))) diff --git a/services-src/auth/resources/wasm/signup/main.go b/services-src/auth/resources/wasm/signup/main.go index afc9092..05d33b9 100644 --- a/services-src/auth/resources/wasm/signup/main.go +++ b/services-src/auth/resources/wasm/signup/main.go @@ -2,23 +2,22 @@ package main import ( "bytes" + "strconv" + "strings" + "time" + "crypto/ed25519" "crypto/rand" "encoding/base64" "encoding/binary" "encoding/hex" - "fmt" - "strconv" - "strings" - "time" - "encoding/json" - "net/http" "net/url" + "syscall/js" "golang.org/x/crypto/argon2" - "syscall/js" + "git.ailur.dev/ailur/jsFetch" ) func showElements(show bool, elements ...js.Value) { @@ -131,7 +130,7 @@ func main() { } // Start the signup process - fmt.Println("Starting signup process for user: " + username) + println("Starting signup process for user: " + username) showElements(false, inputContainer, signupButton, loginButton) if captcha == "" { statusBox.Set("innerText", "You must have a valid captcha! Press the \"Start\" button to start calculating a captcha.") @@ -170,7 +169,7 @@ func main() { return } - response, err := http.Post(requestUri, "application/json", bytes.NewReader(body)) + response, err := jsFetch.Post(requestUri, "application/json", bytes.NewReader(body)) if err != nil { showElements(true, inputContainer, signupButton, loginButton) statusBox.Set("innerText", "Error contacting server: "+err.Error()) @@ -192,7 +191,7 @@ func main() { // Close the response body err = response.Body.Close() if err != nil { - fmt.Println("Could not close response body: " + err.Error() + ", memory leaks may occur") + println("Could not close response body: " + err.Error() + ", memory leaks may occur") } if response.StatusCode == 200 { diff --git a/services-src/auth/resources/wasm/testApp/main.go b/services-src/auth/resources/wasm/testApp/main.go index 4e18f99..154fb0d 100644 --- a/services-src/auth/resources/wasm/testApp/main.go +++ b/services-src/auth/resources/wasm/testApp/main.go @@ -2,6 +2,11 @@ package main import ( "bytes" + "errors" + "strconv" + "strings" + "time" + "crypto/aes" "crypto/cipher" "crypto/ecdh" @@ -9,15 +14,11 @@ import ( "crypto/sha256" "encoding/base64" "encoding/json" - "errors" - "fmt" - "github.com/cespare/xxhash/v2" - "net/http" "net/url" - "strconv" - "strings" "syscall/js" - "time" + + "git.ailur.dev/ailur/jsFetch" + "github.com/cespare/xxhash/v2" ) func sha256Base64(s string) string { @@ -87,7 +88,7 @@ func main() { return } - response, err := http.Post(requestUri, "application/json", bytes.NewReader(body)) + response, err := jsFetch.Post(requestUri, "application/json", bytes.NewReader(body)) if err != nil { statusBox.Set("innerText", "Error contacting server: "+err.Error()) return @@ -98,7 +99,7 @@ func main() { // Close the response body err = response.Body.Close() if err != nil { - fmt.Println("Could not close response body: " + err.Error() + ", memory leaks may occur") + println("Could not close response body: " + err.Error() + ", memory leaks may occur") } // Redirect to log-out if not signed in @@ -119,7 +120,7 @@ func main() { // Close the response body err = response.Body.Close() if err != nil { - fmt.Println("Could not close response body: " + err.Error() + ", memory leaks may occur") + println("Could not close response body: " + err.Error() + ", memory leaks may occur") } // Alert the user if the server is down @@ -130,7 +131,7 @@ func main() { // Close the response body err = response.Body.Close() if err != nil { - fmt.Println("Could not close response body: " + err.Error() + ", memory leaks may occur") + println("Could not close response body: " + err.Error() + ", memory leaks may occur") } // Check if the URL has a code @@ -159,7 +160,7 @@ func main() { return } - response, err := http.Post(requestUri, "application/x-www-form-urlencoded", strings.NewReader(formData.Encode())) + response, err := jsFetch.Post(requestUri, "application/x-www-form-urlencoded", strings.NewReader(formData.Encode())) if err != nil { statusBox.Set("innerText", "Error contacting server: "+err.Error()) return @@ -177,7 +178,7 @@ func main() { // Close the response body err = response.Body.Close() if err != nil { - fmt.Println("Could not close response body: " + err.Error() + ", memory leaks may occur") + println("Could not close response body: " + err.Error() + ", memory leaks may occur") } if response.StatusCode == 200 { @@ -189,7 +190,7 @@ func main() { } // Create the request - request, err := http.NewRequest("GET", requestUri, nil) + request, err := jsFetch.NewRequest("GET", requestUri, nil) if err != nil { statusBox.Set("innerText", "Error creating request: "+err.Error()) return @@ -199,7 +200,7 @@ func main() { request.Header.Set("Authorization", "Bearer "+responseMap["id_token"].(string)) // Send the request - response, err := http.DefaultClient.Do(request) + response, err := jsFetch.Fetch.Do(request) if err != nil { statusBox.Set("innerText", "Error contacting server: "+err.Error()) return @@ -216,7 +217,7 @@ func main() { // Close the response body err = response.Body.Close() if err != nil { - fmt.Println("Could not close response body: " + err.Error() + ", memory leaks may occur") + println("Could not close response body: " + err.Error() + ", memory leaks may occur") } // Set the username