commit 4ee829487e2b92c6c0afa70e5995e404bc562f98 Author: alex Date: Mon Feb 6 00:15:10 2023 +0100 first commit diff --git a/example.env b/example.env new file mode 100644 index 0000000..5d7f92f --- /dev/null +++ b/example.env @@ -0,0 +1,4 @@ +DEBUG=false +HOST=127.0.0.1 +PORT=8080 +MANAGEMENTSYSTEM_API_KEY=test \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..96b1ac1 --- /dev/null +++ b/go.mod @@ -0,0 +1,27 @@ +module clickandjoin.app/managementsystem + +go 1.18 + +require ( + github.com/gofiber/fiber/v2 v2.42.0 + github.com/joho/godotenv v1.5.0 + github.com/sirupsen/logrus v1.9.0 +) + +require ( + github.com/andybalholm/brotli v1.0.4 // indirect + github.com/google/uuid v1.3.0 // indirect + github.com/klauspost/compress v1.15.15 // indirect + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.17 // indirect + github.com/mattn/go-runewidth v0.0.14 // indirect + github.com/philhofer/fwd v1.1.2 // indirect + github.com/rivo/uniseg v0.4.3 // indirect + github.com/savsgio/dictpool v0.0.0-20221023140959-7bf2e61cea94 // indirect + github.com/savsgio/gotils v0.0.0-20230203094617-bcbc01813b4f // indirect + github.com/tinylib/msgp v1.1.8 // indirect + github.com/valyala/bytebufferpool v1.0.0 // indirect + github.com/valyala/fasthttp v1.44.0 // indirect + github.com/valyala/tcplisten v1.0.0 // indirect + golang.org/x/sys v0.4.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..82cd9f8 --- /dev/null +++ b/go.sum @@ -0,0 +1,109 @@ +github.com/andybalholm/brotli v1.0.4 h1:V7DdXeJtZscaqfNuAdSRuRFzuiKlHSC/Zh3zl9qY3JY= +github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +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/gofiber/fiber/v2 v2.42.0 h1:Fnp7ybWvS+sjNQsFvkhf4G8OhXswvB6Vee8hM/LyS+8= +github.com/gofiber/fiber/v2 v2.42.0/go.mod h1:3+SGNjqMh5VQH5Vz2Wdi43zTIV16ktlFd3x3R6O1Zlc= +github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= +github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/joho/godotenv v1.5.0 h1:C/Vohk/9L1RCoS/UW2gfyi2N0EElSW3yb9zwi3PjosE= +github.com/joho/godotenv v1.5.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= +github.com/klauspost/compress v1.15.9 h1:wKRjX6JRtDdrE9qwa4b/Cip7ACOshUI4smpCQanqjSY= +github.com/klauspost/compress v1.15.9/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU= +github.com/klauspost/compress v1.15.15 h1:EF27CXIuDsYJ6mmvtBRlEuB2UVOqHG1tAXgZ7yIO+lw= +github.com/klauspost/compress v1.15.15/go.mod h1:ZcK2JAFqKOpnBlxcLsJzYfrS9X1akm9fHZNnD9+Vo/4= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng= +github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU= +github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +github.com/philhofer/fwd v1.1.1 h1:GdGcTjf5RNAxwS4QLsiMzJYj5KEvPJD3Abr261yRQXQ= +github.com/philhofer/fwd v1.1.1/go.mod h1:gk3iGcWd9+svBvR0sR+KPcfE+RNWozjowpeBVG3ZVNU= +github.com/philhofer/fwd v1.1.2 h1:bnDivRJ1EWPjUIRXV5KfORO897HTbpFAQddBdE8t7Gw= +github.com/philhofer/fwd v1.1.2/go.mod h1:qkPdfjR2SIEbspLqpe1tO4n5yICnr2DY7mqEx2tUTP0= +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/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= +github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +github.com/rivo/uniseg v0.4.3 h1:utMvzDsuh3suAEnhH0RdHmoPbU648o6CvXxTx4SBMOw= +github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= +github.com/savsgio/dictpool v0.0.0-20221023140959-7bf2e61cea94 h1:rmMl4fXJhKMNWl+K+r/fq4FbbKI+Ia2m9hYBLm2h4G4= +github.com/savsgio/dictpool v0.0.0-20221023140959-7bf2e61cea94/go.mod h1:90zrgN3D/WJsDd1iXHT96alCoN2KJo6/4x1DZC3wZs8= +github.com/savsgio/gotils v0.0.0-20220530130905-52f3993e8d6d h1:Q+gqLBOPkFGHyCJxXMRqtUgUbTjI8/Ze8vu8GGyNFwo= +github.com/savsgio/gotils v0.0.0-20220530130905-52f3993e8d6d/go.mod h1:Gy+0tqhJvgGlqnTF8CVGP0AaGRjwBtXs/a5PA0Y3+A4= +github.com/savsgio/gotils v0.0.0-20230203094617-bcbc01813b4f h1:LKLq2MuL/uCGWS7BGo7yOeE/sj5wKxy2aQs69D8imsc= +github.com/savsgio/gotils v0.0.0-20230203094617-bcbc01813b4f/go.mod h1:qwtSXrKuJh/zsFQ12yEE89xfCrGKK63Rr7ctU/uCo4g= +github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0= +github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/tinylib/msgp v1.1.6 h1:i+SbKraHhnrf9M5MYmvQhFnbLhAXSDWF8WWsuyRdocw= +github.com/tinylib/msgp v1.1.6/go.mod h1:75BAfg2hauQhs3qedfdDZmWAPcFMAvJE5b9rGOMufyw= +github.com/tinylib/msgp v1.1.8 h1:FCXC1xanKO4I8plpHGH2P7koL/RzZs12l/+r7vakfm0= +github.com/tinylib/msgp v1.1.8/go.mod h1:qkpG+2ldGg4xRFmx+jfTvZPxfGFhi64BcnL9vkCm/Tw= +github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= +github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= +github.com/valyala/fasthttp v1.44.0 h1:R+gLUhldIsfg1HokMuQjdQ5bh9nuXHPIfvkYUu9eR5Q= +github.com/valyala/fasthttp v1.44.0/go.mod h1:f6VbjjoI3z1NDOZOv17o6RvtRSWxC77seBFc2uWtgiY= +github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8= +github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc= +github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/crypto v0.0.0-20220214200702-86341886e292/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= +golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/mod v0.7.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/net v0.0.0-20220906165146-f3363e06e74c/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk= +golang.org/x/net v0.3.0/go.mod h1:MBQ8lrhLObU/6UmLb4fmbmk5OcyYmqtbGd/9yIeKjEE= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab h1:2QkjZIsXupsJbJIdSjjUOgWK3aEtzyuh2mPt3l/CkeU= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18= +golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/term v0.3.0/go.mod h1:q750SLmJuPmVoN1blW3UFBPREJfb1KmY3vwxfr+nFDA= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20201022035929-9cf592e881e9/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/tools v0.4.0/go.mod h1:UE5sM2OK9E/d67R0ANs2xJizIymRP5gJU295PvKXxjQ= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go new file mode 100644 index 0000000..8214a90 --- /dev/null +++ b/main.go @@ -0,0 +1,82 @@ +// Package classification Click 'n' Join ManagementSystem Documentation +// +// Schemes: http +// Host: localhost +// BasePath: /v1 +// Version: 0.0.1 +// +// Consumes: +// - application/json +// +// Produces: +// - application/json +// +// swagger:meta +package main + +import ( + "clickandjoin.app/managementsystem/modules/config" + "clickandjoin.app/managementsystem/routers/router" + "github.com/gofiber/fiber/v2" + "github.com/gofiber/fiber/v2/middleware/logger" + "github.com/sirupsen/logrus" +) + +func init() { + config.LoadConfig() + + if config.Cfg.Debug { + logrus.SetLevel(logrus.DebugLevel) + } + + logrus.Println("Debug:", config.Cfg.Debug) +} + +func main() { + app := fiber.New() + + cfg := config.Cfg + + if cfg.Debug { + app.Use(logger.New(logger.Config{ + Format: "${pid} ${locals:requestid} ${status} - ${method} ${path}​\n", + })) + } + + /* + cache.WebSocketSessions["test"] = []string{"alpha", "test", "testa", "bob", "beta"} + + logrus.Println(len(cache.WebSocketSessions["test"])) + + user := cache.WebSocketSessions["test"] + + for index, item := range user { + if item == "alpha" { + user = remove(user, index) + } + } + + logrus.Println("s", cache.WebSocketSessions, len(user)) */ + /* + t1 := time.NewTicker(3 * time.Second) + + i := 0 + + go func() { + for range t1.C { + i++ + logrus.Println("timer", cache.WebSocketSessions) + } + }() */ + + router.SetupRoutes(app) + + app.Listen(cfg.Host + ":" + cfg.Port) +} + +/* +func remove(s []string, i int) []string { + s[i] = s[0] + return s[1:] +} +*/ diff --git a/modules/cache/cache.go b/modules/cache/cache.go new file mode 100644 index 0000000..a559ed9 --- /dev/null +++ b/modules/cache/cache.go @@ -0,0 +1,3 @@ +package cache + +var WebSocketSessions = make(map[string][]string) diff --git a/modules/config/config.go b/modules/config/config.go new file mode 100644 index 0000000..6742182 --- /dev/null +++ b/modules/config/config.go @@ -0,0 +1,44 @@ +package config + +import ( + "os" + "strconv" + + "github.com/joho/godotenv" + "github.com/sirupsen/logrus" +) + +var Cfg Config + +type Config struct { + Debug bool + Host string + Port string + ManagementSystemApiKey string +} + +func LoadConfig() { + // argument to start the server locally for development + if len(os.Args) > 1 { + if os.Args[1] == "--local" || os.Args[1] == "-l" { + if err := godotenv.Load("local.env"); err != nil { + logrus.Fatalln("Failed to load env, err:", err) + } + } + } + + debug, err := strconv.ParseBool(os.Getenv("DEBUG")) + + if err != nil { + logrus.Fatalln("Failed to parse boolean, err:", err) + } + + cfg := Config{ + Debug: debug, + Host: os.Getenv("HOST"), + Port: os.Getenv("PORT"), + ManagementSystemApiKey: os.Getenv("MANAGEMENTSYSTEM_API_KEY"), + } + + Cfg = cfg +} diff --git a/modules/structs/wssessions.go b/modules/structs/wssessions.go new file mode 100644 index 0000000..ac49d33 --- /dev/null +++ b/modules/structs/wssessions.go @@ -0,0 +1,13 @@ +package structs + +// swagger:model ExistsUserWebSocketSessionsResponse +type ExistsUserWebSocketSessionsResponse struct { + // Returns whether the user websocket session already exists or not + E bool +} + +// swagger:model GetUserWebSocketSessionsResponse +type GetUserWebSocketSessionsResponse struct { + // List of all websocket sessions of the user that are connected + WebSocketSessions []string +} diff --git a/routers/api/v1/wssessions/wssessions.go b/routers/api/v1/wssessions/wssessions.go new file mode 100644 index 0000000..68e240d --- /dev/null +++ b/routers/api/v1/wssessions/wssessions.go @@ -0,0 +1,132 @@ +package wssessions + +import ( + "clickandjoin.app/managementsystem/modules/cache" + "clickandjoin.app/managementsystem/modules/structs" + "github.com/gofiber/fiber/v2" + "github.com/gofiber/fiber/v2/utils" +) + +func AddUserWebSocketSession(c *fiber.Ctx) error { + // swagger:operation POST /wsconnections/:userId/:webSocketSession wsconnections wsconnectionsAddUserWebSocketSession + // --- + // summary: Add user websocket session + // consumes: + // - application/json + // produces: + // - application/json + // parameters: + // - name: X-Ms-Api + // in: header + // required: true + // responses: + // '200': + // description: Added user web socket session + + userId := utils.CopyString(c.Params("userId")) + webSocketSession := utils.CopyString(c.Params("webSocketSession")) + + if webSocketSessions, ok := cache.WebSocketSessions[userId]; ok { + if isInList(userId, webSocketSession) { + return c.SendStatus(fiber.StatusUnprocessableEntity) + } + + cache.WebSocketSessions[userId] = append(webSocketSessions, webSocketSession) + } else { + cache.WebSocketSessions[userId] = []string{webSocketSession} + } + + return c.SendStatus(fiber.StatusOK) +} + +func isInList(userId string, webSocketSession string) bool { + for _, item := range cache.WebSocketSessions[userId] { + if item == webSocketSession { + return true + } + + } + return false +} + +func ExistsUserWebSocketSession(c *fiber.Ctx) error { + // swagger:operation GET /wsconnections/:userId/:webSocketSession wsconnections wsconnectionsExistsUserWebSocketSession + // --- + // summary: Returns whether the user websocket session already exists + // consumes: + // - application/json + // produces: + // - application/json + // parameters: + // - name: X-Ms-Api + // in: header + // required: true + // responses: + // '200': + // description: Added user web socket session + + return c.JSON(structs.ExistsUserWebSocketSessionsResponse{E: isInList(c.Params("userId"), c.Params("webSocketSession"))}) +} + +func GetUserWebSocketSessions(c *fiber.Ctx) error { + // swagger:operation GET /wsconnections/:userId wsconnections wsconnectionsGetUserWebSocketSessions + // --- + // summary: List of all websocket sessions of the user that are connected + // consumes: + // - application/json + // produces: + // - application/json + // parameters: + // - name: X-Ms-Api + // in: header + // required: true + // responses: + // '200': + // description: List of all websocket sessions of the user that are connected + // schema: + // "$ref": "#/definitions/GetUserWebSocketSessionsResponse" + + var list []string + + if webSocketSessions, ok := cache.WebSocketSessions[c.Params("userId")]; ok { + list = webSocketSessions + } + + return c.JSON(structs.GetUserWebSocketSessionsResponse{WebSocketSessions: list}) +} + +func RemoveConnection(c *fiber.Ctx) error { + // swagger:operation DELETE /wsconnections/:userId/:webSocketSession wsconnections wsconnectionsRemoveConnection + // --- + // summary: Deletes a websocket session from the user + // consumes: + // - application/json + // produces: + // - application/json + // parameters: + // - name: X-Ms-Api + // in: header + // required: true + // responses: + // '200': + // description: Websocket session deleted + + userId := c.Params("userId") + webSocketSession := c.Params("webSocketSession") + + if webSocketSessions, ok := cache.WebSocketSessions[userId]; ok { + for index, session := range webSocketSessions { + if session == webSocketSession { + cache.WebSocketSessions[userId] = remove(webSocketSessions, index) + break + } + } + } + + return c.SendStatus(fiber.StatusOK) +} + +func remove(s []string, i int) []string { + s[i] = s[0] + return s[1:] +} diff --git a/routers/router/router.go b/routers/router/router.go new file mode 100644 index 0000000..c2bc68d --- /dev/null +++ b/routers/router/router.go @@ -0,0 +1,27 @@ +package router + +import ( + "clickandjoin.app/managementsystem/modules/config" + wssessions "clickandjoin.app/managementsystem/routers/api/v1/wssessions" + "github.com/gofiber/fiber/v2" +) + +func SetupRoutes(app *fiber.App) { + v1 := app.Group("/v1") + + wsc := v1.Group("/wsconnections") + wsc.Post("/:userId/:webSocketSession", ApiKeyValidation, wssessions.AddUserWebSocketSession) + wsc.Get("/:userId/:webSocketSession", ApiKeyValidation, wssessions.ExistsUserWebSocketSession) + wsc.Get("/:userId", ApiKeyValidation, wssessions.GetUserWebSocketSessions) + wsc.Delete("/:userId/:webSocketSession", ApiKeyValidation, wssessions.RemoveConnection) +} + +func ApiKeyValidation(c *fiber.Ctx) error { + apiKey := c.GetReqHeaders()["X-Ms-Api"] + + if apiKey != config.Cfg.ManagementSystemApiKey { + return c.SendStatus(fiber.StatusUnauthorized) + } + + return c.Next() +}