From b0518288da806101ceded0ffb7be1ba23de7e9c5 Mon Sep 17 00:00:00 2001 From: Jacob Gunther Date: Tue, 8 Mar 2022 21:28:46 -0600 Subject: [PATCH] Add initial code --- .gitignore | 2 + README.md | 2 + config.example.yml | 5 + go.mod | 19 + go.sum | 119 ++++++ scripts/build | 2 + src/config.go | 26 ++ src/main.go | 56 +++ src/redis/redis.go | 103 +++++ src/routes/body.go | 538 +++++++++++++++++++++++++++ src/routes/face.go | 118 ++++++ src/routes/head.go | 118 ++++++ src/routes/init.go | 17 + src/routes/ping.go | 9 + src/routes/skin.go | 60 +++ src/routes/uuid.go | 26 ++ src/util/alex.png | Bin 0 -> 2105 bytes src/util/image.go | 17 + src/util/init.go | 5 + src/util/renders/backbody.go | 59 +++ src/util/renders/body.go | 90 +++++ src/util/renders/face.go | 13 + src/util/renders/frontbody.go | 59 +++ src/util/renders/head.go | 33 ++ src/util/renders/leftbody.go | 44 +++ src/util/renders/matrix/transform.go | 90 +++++ src/util/renders/options.go | 7 + src/util/renders/rightbody.go | 34 ++ src/util/renders/util.go | 164 ++++++++ src/util/skin.go | 47 +++ src/util/steve.png | Bin 0 -> 1469 bytes src/util/uuid.go | 50 +++ src/util/yggdrasil.go | 243 ++++++++++++ 33 files changed, 2175 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 config.example.yml create mode 100644 go.mod create mode 100644 go.sum create mode 100755 scripts/build create mode 100644 src/config.go create mode 100644 src/main.go create mode 100644 src/redis/redis.go create mode 100644 src/routes/body.go create mode 100644 src/routes/face.go create mode 100644 src/routes/head.go create mode 100644 src/routes/init.go create mode 100644 src/routes/ping.go create mode 100644 src/routes/skin.go create mode 100644 src/routes/uuid.go create mode 100644 src/util/alex.png create mode 100644 src/util/image.go create mode 100644 src/util/init.go create mode 100644 src/util/renders/backbody.go create mode 100644 src/util/renders/body.go create mode 100644 src/util/renders/face.go create mode 100644 src/util/renders/frontbody.go create mode 100644 src/util/renders/head.go create mode 100644 src/util/renders/leftbody.go create mode 100644 src/util/renders/matrix/transform.go create mode 100644 src/util/renders/options.go create mode 100644 src/util/renders/rightbody.go create mode 100644 src/util/renders/util.go create mode 100644 src/util/skin.go create mode 100644 src/util/steve.png create mode 100644 src/util/uuid.go create mode 100644 src/util/yggdrasil.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7561fce --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +bin/ +config.yml \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..32ac9eb --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# API Server +The REST server that powers the API for mineatar.io. \ No newline at end of file diff --git a/config.example.yml b/config.example.yml new file mode 100644 index 0000000..79f8e69 --- /dev/null +++ b/config.example.yml @@ -0,0 +1,5 @@ +host: 0.0.0.0 +port: 3001 +redis: + uri: 127.0.0.1:6379 + database: 0 \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..f721cfd --- /dev/null +++ b/go.mod @@ -0,0 +1,19 @@ +module main + +go 1.17 + +require ( + github.com/buaazp/fasthttprouter v0.1.1 + github.com/go-redis/redis/v8 v8.11.4 + github.com/valyala/fasthttp v1.34.0 + golang.org/x/image v0.0.0-20220302094943-723b81ca9867 + gopkg.in/yaml.v2 v2.4.0 +) + +require ( + github.com/andybalholm/brotli v1.0.4 // indirect + github.com/cespare/xxhash/v2 v2.1.2 // indirect + github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect + github.com/klauspost/compress v1.15.0 // indirect + github.com/valyala/bytebufferpool v1.0.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..34a0a0a --- /dev/null +++ b/go.sum @@ -0,0 +1,119 @@ +github.com/andybalholm/brotli v1.0.4 h1:V7DdXeJtZscaqfNuAdSRuRFzuiKlHSC/Zh3zl9qY3JY= +github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= +github.com/buaazp/fasthttprouter v0.1.1 h1:4oAnN0C3xZjylvZJdP35cxfclyn4TYkW6Y+DSvS+h8Q= +github.com/buaazp/fasthttprouter v0.1.1/go.mod h1:h/Ap5oRVLeItGKTVBb+heQPks+HdIUtGmI4H5WCYijM= +github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE= +github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= +github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= +github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= +github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= +github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= +github.com/go-redis/redis/v8 v8.11.4 h1:kHoYkfZP6+pe04aFTnhDH6GDROa5yJdHJVNxV3F46Tg= +github.com/go-redis/redis/v8 v8.11.4/go.mod h1:2Z2wHZXdQpCDXEGzqMockDpNyYvi2l4Pxt6RJr792+w= +github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= +github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= +github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= +github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= +github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= +github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= +github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= +github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= +github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= +github.com/klauspost/compress v1.15.0 h1:xqfchp4whNFxn5A4XFyyYtitiWI8Hy5EW59jEwcyL6U= +github.com/klauspost/compress v1.15.0/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk= +github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= +github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE= +github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU= +github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk= +github.com/onsi/ginkgo v1.16.4 h1:29JGrr5oVBm5ulCWet69zQkzWipVXIol6ygQUe/EzNc= +github.com/onsi/ginkgo v1.16.4/go.mod h1:dX+/inL/fNMqNlz0e9LfyB9TswhZpCVdJM/Z6Vvnwo0= +github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY= +github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= +github.com/onsi/gomega v1.16.0 h1:6gjqkI8iiRHMvdccRJM8rVKjCWk6ZIm6FTm3ddIe4/c= +github.com/onsi/gomega v1.16.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAlGdZY= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= +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.34.0 h1:d3AAQJ2DRcxJYHm7OXNXtXt2as1vMDfxeIcFvhmGGm4= +github.com/valyala/fasthttp v1.34.0/go.mod h1:epZA5N+7pY6ZaEKRmstzOuYJx9HI8DI1oaCGZpdH4h0= +github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc= +github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +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-20220214200702-86341886e292/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= +golang.org/x/image v0.0.0-20220302094943-723b81ca9867 h1:TcHcE0vrmgzNH1v3ppjcMGbhG5+9fMuvOmUYwNEF4q4= +golang.org/x/image v0.0.0-20220302094943-723b81ca9867/go.mod h1:023OzeP/+EPmXeapQh35lcL3II3LrY8Ic+EFFKVhULM= +golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +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-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= +golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= +golang.org/x/net v0.0.0-20210428140749-89ef3d95e781/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk= +golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/net v0.0.0-20220225172249-27dd8689420f h1:oA4XRj0qtSt8Yo1Zms0CUlsT3KG69V2UGQWPBxujDmc= +golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +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/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +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-20190904154756-749cb33beabd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/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-20210112080510-489259a85091/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-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9 h1:nhht2DYV/Sn3qOayu8lM+cU1ii9sTLUeBQwQQfUHtrs= +golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9/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/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 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +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-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= +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-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= +google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= +google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= +google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= +google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= +google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= +google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= diff --git a/scripts/build b/scripts/build new file mode 100755 index 0000000..6e917be --- /dev/null +++ b/scripts/build @@ -0,0 +1,2 @@ +#!/bin/bash +go build -o bin/main src/*.go \ No newline at end of file diff --git a/src/config.go b/src/config.go new file mode 100644 index 0000000..0b00676 --- /dev/null +++ b/src/config.go @@ -0,0 +1,26 @@ +package main + +import ( + "io/ioutil" + + "gopkg.in/yaml.v2" +) + +type Configuration struct { + Host string `yaml:"host"` + Port uint16 `yaml:"port"` + Redis struct { + URI string `yaml:"uri"` + Database int `yaml:"database"` + } `yaml:"redis"` +} + +func (c *Configuration) ReadFile(file string) error { + data, err := ioutil.ReadFile(file) + + if err != nil { + return err + } + + return yaml.Unmarshal(data, c) +} diff --git a/src/main.go b/src/main.go new file mode 100644 index 0000000..148959c --- /dev/null +++ b/src/main.go @@ -0,0 +1,56 @@ +package main + +import ( + "fmt" + "log" + "main/src/redis" + "main/src/routes" + "time" + + "github.com/buaazp/fasthttprouter" + "github.com/valyala/fasthttp" +) + +var ( + config *Configuration = &Configuration{} + r *redis.Redis = &redis.Redis{} +) + +func init() { + var err error + + if err = config.ReadFile("config.yml"); err != nil { + log.Fatal(err) + } + + start := time.Now() + + if err = r.Connect(config.Redis.URI, config.Redis.Database); err != nil { + log.Fatal(err) + } + + log.Printf("Successfully connected to Redis (%s)\n", time.Since(start)) + + routes.InitRoutes(r) +} + +func main() { + defer r.Close() + + router := fasthttprouter.New() + + router.GET("/ping", routes.PingHandler) + router.GET("/uuid/:user", routes.UUIDHandler) + router.GET("/skin/:user", routes.SkinHandler) + router.GET("/face/:user", routes.FaceHandler) + router.GET("/head/:user", routes.HeadHandler) + router.GET("/body/full/:user", routes.FullBodyHandler) + router.GET("/body/front/:user", routes.FrontBodyHandler) + router.GET("/body/back/:user", routes.BackBodyHandler) + router.GET("/body/left/:user", routes.LeftBodyHandler) + router.GET("/body/right/:user", routes.RightBodyHandler) + + log.Printf("Listening on %s:%d\n", config.Host, config.Port) + + log.Fatal(fasthttp.ListenAndServe(fmt.Sprintf("%s:%d", config.Host, config.Port), router.Handler)) +} diff --git a/src/redis/redis.go b/src/redis/redis.go new file mode 100644 index 0000000..d7f1413 --- /dev/null +++ b/src/redis/redis.go @@ -0,0 +1,103 @@ +package redis + +import ( + "context" + "time" + + "github.com/go-redis/redis/v8" +) + +type Redis struct { + conn *redis.Client +} + +func (r *Redis) Connect(uri string, database int) error { + c := redis.NewClient(&redis.Options{ + Addr: uri, + DB: database, + }) + + r.conn = c + + ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) + + defer cancel() + + return c.Ping(ctx).Err() +} + +func (r *Redis) GetString(key string) (string, bool, error) { + ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) + + defer cancel() + + existsResult := r.conn.Exists(ctx, key) + + if err := existsResult.Err(); err != nil { + return "", false, err + } + + if existsResult.Val() == 0 { + return "", false, nil + } + + result := r.conn.Get(ctx, key) + + return result.Val(), true, result.Err() +} + +func (r *Redis) GetBytes(key string) ([]byte, bool, error) { + ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) + + defer cancel() + + existsResult := r.conn.Exists(ctx, key) + + if err := existsResult.Err(); err != nil { + return nil, false, err + } + + if existsResult.Val() == 0 { + return nil, false, nil + } + + result := r.conn.Get(ctx, key) + + if err := result.Err(); err != nil { + return nil, true, err + } + + data, err := result.Bytes() + + return data, true, err +} + +func (r *Redis) Exists(key string) (bool, error) { + ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) + + defer cancel() + + result := r.conn.Exists(ctx, key) + + return result.Val() == 1, result.Err() +} + +func (r *Redis) Delete(key string) error { + ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) + + defer cancel() + + return r.conn.Del(ctx, key).Err() +} + +func (r *Redis) Set(key string, value interface{}, ttl time.Duration) error { + ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) + + defer cancel() + + return r.conn.Set(ctx, key, value, ttl).Err() +} + +func (r *Redis) Close() error { + return r.conn.Close() +} diff --git a/src/routes/body.go b/src/routes/body.go new file mode 100644 index 0000000..3b64202 --- /dev/null +++ b/src/routes/body.go @@ -0,0 +1,538 @@ +package routes + +import ( + "fmt" + "log" + "main/src/util" + "main/src/util/renders" + "math" + "net/http" + "time" + + "github.com/valyala/fasthttp" +) + +func FullBodyHandler(ctx *fasthttp.RequestCtx) { + user := ctx.UserValue("user").(string) + + download := ctx.QueryArgs().GetBool("download") + + scale, err := ctx.QueryArgs().GetUint("scale") + + if err != nil { + scale = 4 + } + + scale = int(math.Max(math.Min(float64(scale), MaxScaleFullBody), MinScale)) + + overlay := true + + if ctx.QueryArgs().Has("overlay") { + overlay = ctx.QueryArgs().GetBool("overlay") + } + + uuid, err := util.GetUUID(r, user) + + if err != nil { + log.Println(err) + + ctx.SetStatusCode(http.StatusInternalServerError) + ctx.SetBodyString(http.StatusText(http.StatusInternalServerError)) + + return + } + + cacheKey := fmt.Sprintf("result:fullbody-%d-%t-%s", scale, overlay, uuid) + + cache, ok, err := r.GetBytes(cacheKey) + + if err != nil { + log.Println(err) + + ctx.SetStatusCode(http.StatusInternalServerError) + ctx.SetBodyString(http.StatusText(http.StatusInternalServerError)) + + return + } + + if ok { + if download { + ctx.Response.Header.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, user)) + } + + ctx.Response.Header.Set("X-Cache-Hit", "TRUE") + ctx.SetContentType("image/png") + ctx.SetBody(cache) + + return + } + + skin, slim, err := util.GetPlayerSkin(r, uuid) + + if err != nil { + log.Println(err) + + ctx.SetStatusCode(http.StatusInternalServerError) + ctx.SetBodyString(http.StatusText(http.StatusInternalServerError)) + + return + } + + if skin == nil { + skin = util.GetDefaultSkin(slim) + } + + render := renders.RenderBody(skin, renders.RenderOptions{ + Overlay: overlay, + Slim: slim, + Scale: scale, + }) + + data, err := util.EncodePNG(render) + + if err != nil { + log.Println(err) + + ctx.SetStatusCode(http.StatusInternalServerError) + ctx.SetBodyString(http.StatusText(http.StatusInternalServerError)) + + return + } + + if err = r.Set(cacheKey, data, time.Hour*24); err != nil { + log.Println(err) + + ctx.SetStatusCode(http.StatusInternalServerError) + ctx.SetBodyString(http.StatusText(http.StatusInternalServerError)) + + return + } + + if download { + ctx.Response.Header.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, user)) + } + + ctx.Response.Header.Set("X-Cache-Hit", "FALSE") + ctx.SetContentType("image/png") + ctx.SetBody(data) +} + +func FrontBodyHandler(ctx *fasthttp.RequestCtx) { + user := ctx.UserValue("user").(string) + + download := ctx.QueryArgs().GetBool("download") + + scale, err := ctx.QueryArgs().GetUint("scale") + + if err != nil { + scale = 4 + } + + scale = int(math.Max(math.Min(float64(scale), MaxScale), MinScale)) + + overlay := true + + if ctx.QueryArgs().Has("overlay") { + overlay = ctx.QueryArgs().GetBool("overlay") + } + + uuid, err := util.GetUUID(r, user) + + if err != nil { + log.Println(err) + + ctx.SetStatusCode(http.StatusInternalServerError) + ctx.SetBodyString(http.StatusText(http.StatusInternalServerError)) + + return + } + + cacheKey := fmt.Sprintf("result:frontbody-%d-%t-%s", scale, overlay, uuid) + + cache, ok, err := r.GetBytes(cacheKey) + + if err != nil { + log.Println(err) + + ctx.SetStatusCode(http.StatusInternalServerError) + ctx.SetBodyString(http.StatusText(http.StatusInternalServerError)) + + return + } + + if ok { + if download { + ctx.Response.Header.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, user)) + } + + ctx.Response.Header.Set("X-Cache-Hit", "TRUE") + ctx.SetContentType("image/png") + ctx.SetBody(cache) + + return + } + + skin, slim, err := util.GetPlayerSkin(r, uuid) + + if err != nil { + log.Println(err) + + ctx.SetStatusCode(http.StatusInternalServerError) + ctx.SetBodyString(http.StatusText(http.StatusInternalServerError)) + + return + } + + if skin == nil { + skin = util.GetDefaultSkin(slim) + } + + render := renders.RenderFrontBody(skin, renders.RenderOptions{ + Overlay: overlay, + Slim: slim, + Scale: scale, + }) + + data, err := util.EncodePNG(render) + + if err != nil { + log.Println(err) + + ctx.SetStatusCode(http.StatusInternalServerError) + ctx.SetBodyString(http.StatusText(http.StatusInternalServerError)) + + return + } + + if err = r.Set(cacheKey, data, time.Hour*24); err != nil { + log.Println(err) + + ctx.SetStatusCode(http.StatusInternalServerError) + ctx.SetBodyString(http.StatusText(http.StatusInternalServerError)) + + return + } + + if download { + ctx.Response.Header.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, user)) + } + + ctx.Response.Header.Set("X-Cache-Hit", "FALSE") + ctx.SetContentType("image/png") + ctx.SetBody(data) +} + +func BackBodyHandler(ctx *fasthttp.RequestCtx) { + user := ctx.UserValue("user").(string) + + download := ctx.QueryArgs().GetBool("download") + + scale, err := ctx.QueryArgs().GetUint("scale") + + if err != nil { + scale = 4 + } + + scale = int(math.Max(math.Min(float64(scale), MaxScale), MinScale)) + + overlay := true + + if ctx.QueryArgs().Has("overlay") { + overlay = ctx.QueryArgs().GetBool("overlay") + } + + uuid, err := util.GetUUID(r, user) + + if err != nil { + log.Println(err) + + ctx.SetStatusCode(http.StatusInternalServerError) + ctx.SetBodyString(http.StatusText(http.StatusInternalServerError)) + + return + } + + cacheKey := fmt.Sprintf("result:backbody-%d-%t-%s", scale, overlay, uuid) + + cache, ok, err := r.GetBytes(cacheKey) + + if err != nil { + log.Println(err) + + ctx.SetStatusCode(http.StatusInternalServerError) + ctx.SetBodyString(http.StatusText(http.StatusInternalServerError)) + + return + } + + if ok { + if download { + ctx.Response.Header.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, user)) + } + + ctx.Response.Header.Set("X-Cache-Hit", "TRUE") + ctx.SetContentType("image/png") + ctx.SetBody(cache) + + return + } + + skin, slim, err := util.GetPlayerSkin(r, uuid) + + if err != nil { + log.Println(err) + + ctx.SetStatusCode(http.StatusInternalServerError) + ctx.SetBodyString(http.StatusText(http.StatusInternalServerError)) + + return + } + + if skin == nil { + skin = util.GetDefaultSkin(slim) + } + + render := renders.RenderBackBody(skin, renders.RenderOptions{ + Overlay: overlay, + Slim: slim, + Scale: scale, + }) + + data, err := util.EncodePNG(render) + + if err != nil { + log.Println(err) + + ctx.SetStatusCode(http.StatusInternalServerError) + ctx.SetBodyString(http.StatusText(http.StatusInternalServerError)) + + return + } + + if err = r.Set(cacheKey, data, time.Hour*24); err != nil { + log.Println(err) + + ctx.SetStatusCode(http.StatusInternalServerError) + ctx.SetBodyString(http.StatusText(http.StatusInternalServerError)) + + return + } + + if download { + ctx.Response.Header.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, user)) + } + + ctx.Response.Header.Set("X-Cache-Hit", "FALSE") + ctx.SetContentType("image/png") + ctx.SetBody(data) +} + +func LeftBodyHandler(ctx *fasthttp.RequestCtx) { + user := ctx.UserValue("user").(string) + + download := ctx.QueryArgs().GetBool("download") + + scale, err := ctx.QueryArgs().GetUint("scale") + + if err != nil { + scale = 4 + } + + scale = int(math.Max(math.Min(float64(scale), MaxScale), MinScale)) + + overlay := true + + if ctx.QueryArgs().Has("overlay") { + overlay = ctx.QueryArgs().GetBool("overlay") + } + + uuid, err := util.GetUUID(r, user) + + if err != nil { + log.Println(err) + + ctx.SetStatusCode(http.StatusInternalServerError) + ctx.SetBodyString(http.StatusText(http.StatusInternalServerError)) + + return + } + + cacheKey := fmt.Sprintf("result:leftbody-%d-%t-%s", scale, overlay, uuid) + + cache, ok, err := r.GetBytes(cacheKey) + + if err != nil { + log.Println(err) + + ctx.SetStatusCode(http.StatusInternalServerError) + ctx.SetBodyString(http.StatusText(http.StatusInternalServerError)) + + return + } + + if ok { + if download { + ctx.Response.Header.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, user)) + } + + ctx.Response.Header.Set("X-Cache-Hit", "TRUE") + ctx.SetContentType("image/png") + ctx.SetBody(cache) + + return + } + + skin, slim, err := util.GetPlayerSkin(r, uuid) + + if err != nil { + log.Println(err) + + ctx.SetStatusCode(http.StatusInternalServerError) + ctx.SetBodyString(http.StatusText(http.StatusInternalServerError)) + + return + } + + if skin == nil { + skin = util.GetDefaultSkin(slim) + } + + render := renders.RenderLeftBody(skin, renders.RenderOptions{ + Overlay: overlay, + Slim: slim, + Scale: scale, + }) + + data, err := util.EncodePNG(render) + + if err != nil { + log.Println(err) + + ctx.SetStatusCode(http.StatusInternalServerError) + ctx.SetBodyString(http.StatusText(http.StatusInternalServerError)) + + return + } + + if err = r.Set(cacheKey, data, time.Hour*24); err != nil { + log.Println(err) + + ctx.SetStatusCode(http.StatusInternalServerError) + ctx.SetBodyString(http.StatusText(http.StatusInternalServerError)) + + return + } + + if download { + ctx.Response.Header.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, user)) + } + + ctx.Response.Header.Set("X-Cache-Hit", "FALSE") + ctx.SetContentType("image/png") + ctx.SetBody(data) +} + +func RightBodyHandler(ctx *fasthttp.RequestCtx) { + user := ctx.UserValue("user").(string) + + download := ctx.QueryArgs().GetBool("download") + + scale, err := ctx.QueryArgs().GetUint("scale") + + if err != nil { + scale = 4 + } + + scale = int(math.Max(math.Min(float64(scale), MaxScale), MinScale)) + + overlay := true + + if ctx.QueryArgs().Has("overlay") { + overlay = ctx.QueryArgs().GetBool("overlay") + } + + uuid, err := util.GetUUID(r, user) + + if err != nil { + log.Println(err) + + ctx.SetStatusCode(http.StatusInternalServerError) + ctx.SetBodyString(http.StatusText(http.StatusInternalServerError)) + + return + } + + cacheKey := fmt.Sprintf("result:rightbody-%d-%t-%s", scale, overlay, uuid) + + cache, ok, err := r.GetBytes(cacheKey) + + if err != nil { + log.Println(err) + + ctx.SetStatusCode(http.StatusInternalServerError) + ctx.SetBodyString(http.StatusText(http.StatusInternalServerError)) + + return + } + + if ok { + if download { + ctx.Response.Header.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, user)) + } + + ctx.Response.Header.Set("X-Cache-Hit", "TRUE") + ctx.SetContentType("image/png") + ctx.SetBody(cache) + + return + } + + skin, slim, err := util.GetPlayerSkin(r, uuid) + + if err != nil { + log.Println(err) + + ctx.SetStatusCode(http.StatusInternalServerError) + ctx.SetBodyString(http.StatusText(http.StatusInternalServerError)) + + return + } + + if skin == nil { + skin = util.GetDefaultSkin(slim) + } + + render := renders.RenderRightBody(skin, renders.RenderOptions{ + Overlay: overlay, + Slim: slim, + Scale: scale, + }) + + data, err := util.EncodePNG(render) + + if err != nil { + log.Println(err) + + ctx.SetStatusCode(http.StatusInternalServerError) + ctx.SetBodyString(http.StatusText(http.StatusInternalServerError)) + + return + } + + if err = r.Set(cacheKey, data, time.Hour*24); err != nil { + log.Println(err) + + ctx.SetStatusCode(http.StatusInternalServerError) + ctx.SetBodyString(http.StatusText(http.StatusInternalServerError)) + + return + } + + if download { + ctx.Response.Header.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, user)) + } + + ctx.Response.Header.Set("X-Cache-Hit", "FALSE") + ctx.SetContentType("image/png") + ctx.SetBody(data) +} diff --git a/src/routes/face.go b/src/routes/face.go new file mode 100644 index 0000000..91e5c2c --- /dev/null +++ b/src/routes/face.go @@ -0,0 +1,118 @@ +package routes + +import ( + "fmt" + "log" + "main/src/util" + "main/src/util/renders" + "math" + "net/http" + "time" + + "github.com/valyala/fasthttp" +) + +func FaceHandler(ctx *fasthttp.RequestCtx) { + user := ctx.UserValue("user").(string) + + download := ctx.QueryArgs().GetBool("download") + + scale, err := ctx.QueryArgs().GetUint("scale") + + if err != nil { + scale = 4 + } + + scale = int(math.Max(math.Min(float64(scale), MaxScale), MinScale)) + + overlay := true + + if ctx.QueryArgs().Has("overlay") { + overlay = ctx.QueryArgs().GetBool("overlay") + } + + uuid, err := util.GetUUID(r, user) + + if err != nil { + log.Println(err) + + ctx.SetStatusCode(http.StatusInternalServerError) + ctx.SetBodyString(http.StatusText(http.StatusInternalServerError)) + + return + } + + cacheKey := fmt.Sprintf("result:face-%d-%t-%s", scale, overlay, uuid) + + cache, ok, err := r.GetBytes(cacheKey) + + if err != nil { + log.Println(err) + + ctx.SetStatusCode(http.StatusInternalServerError) + ctx.SetBodyString(http.StatusText(http.StatusInternalServerError)) + + return + } + + if ok { + if download { + ctx.Response.Header.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, user)) + } + + ctx.Response.Header.Set("X-Cache-Hit", "TRUE") + ctx.SetContentType("image/png") + ctx.SetBody(cache) + + return + } + + skin, slim, err := util.GetPlayerSkin(r, uuid) + + if err != nil { + log.Println(err) + + ctx.SetStatusCode(http.StatusInternalServerError) + ctx.SetBodyString(http.StatusText(http.StatusInternalServerError)) + + return + } + + if skin == nil { + skin = util.GetDefaultSkin(slim) + } + + render := renders.RenderFace(skin, renders.RenderOptions{ + Overlay: overlay, + Slim: slim, + Scale: scale, + }) + + data, err := util.EncodePNG(render) + + if err != nil { + log.Println(err) + + ctx.SetStatusCode(http.StatusInternalServerError) + ctx.SetBodyString(http.StatusText(http.StatusInternalServerError)) + + return + } + + if err = r.Set(cacheKey, data, time.Hour*24); err != nil { + log.Println(err) + + ctx.SetStatusCode(http.StatusInternalServerError) + ctx.SetBodyString(http.StatusText(http.StatusInternalServerError)) + + return + } + + if download { + ctx.Response.Header.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, user)) + } + + ctx.Response.Header.Set("X-Cache-Hit", "FALSE") + ctx.SetContentType("image/png") + ctx.SetBody(data) +} diff --git a/src/routes/head.go b/src/routes/head.go new file mode 100644 index 0000000..0e45c9b --- /dev/null +++ b/src/routes/head.go @@ -0,0 +1,118 @@ +package routes + +import ( + "fmt" + "log" + "main/src/util" + "main/src/util/renders" + "math" + "net/http" + "time" + + "github.com/valyala/fasthttp" +) + +func HeadHandler(ctx *fasthttp.RequestCtx) { + user := ctx.UserValue("user").(string) + + download := ctx.QueryArgs().GetBool("download") + + scale, err := ctx.QueryArgs().GetUint("scale") + + if err != nil { + scale = 4 + } + + scale = int(math.Max(math.Min(float64(scale), MaxScale), MinScale)) + + overlay := true + + if ctx.QueryArgs().Has("overlay") { + overlay = ctx.QueryArgs().GetBool("overlay") + } + + uuid, err := util.GetUUID(r, user) + + if err != nil { + log.Println(err) + + ctx.SetStatusCode(http.StatusInternalServerError) + ctx.SetBodyString(http.StatusText(http.StatusInternalServerError)) + + return + } + + cacheKey := fmt.Sprintf("result:head-%d-%t-%s", scale, overlay, uuid) + + cache, ok, err := r.GetBytes(cacheKey) + + if err != nil { + log.Println(err) + + ctx.SetStatusCode(http.StatusInternalServerError) + ctx.SetBodyString(http.StatusText(http.StatusInternalServerError)) + + return + } + + if ok { + if download { + ctx.Response.Header.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, user)) + } + + ctx.Response.Header.Set("X-Cache-Hit", "TRUE") + ctx.SetContentType("image/png") + ctx.SetBody(cache) + + return + } + + skin, slim, err := util.GetPlayerSkin(r, uuid) + + if err != nil { + log.Println(err) + + ctx.SetStatusCode(http.StatusInternalServerError) + ctx.SetBodyString(http.StatusText(http.StatusInternalServerError)) + + return + } + + if skin == nil { + skin = util.GetDefaultSkin(slim) + } + + render := renders.RenderHead(skin, renders.RenderOptions{ + Overlay: overlay, + Slim: slim, + Scale: scale, + }) + + data, err := util.EncodePNG(render) + + if err != nil { + log.Println(err) + + ctx.SetStatusCode(http.StatusInternalServerError) + ctx.SetBodyString(http.StatusText(http.StatusInternalServerError)) + + return + } + + if err = r.Set(cacheKey, data, time.Hour*24); err != nil { + log.Println(err) + + ctx.SetStatusCode(http.StatusInternalServerError) + ctx.SetBodyString(http.StatusText(http.StatusInternalServerError)) + + return + } + + if download { + ctx.Response.Header.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, user)) + } + + ctx.Response.Header.Set("X-Cache-Hit", "FALSE") + ctx.SetContentType("image/png") + ctx.SetBody(data) +} diff --git a/src/routes/init.go b/src/routes/init.go new file mode 100644 index 0000000..2fdc300 --- /dev/null +++ b/src/routes/init.go @@ -0,0 +1,17 @@ +package routes + +import "main/src/redis" + +var ( + r *redis.Redis +) + +const ( + MaxScale float64 = 64.0 + MaxScaleFullBody float64 = 32.0 + MinScale float64 = 1.0 +) + +func InitRoutes(red *redis.Redis) { + r = red +} diff --git a/src/routes/ping.go b/src/routes/ping.go new file mode 100644 index 0000000..631d9cc --- /dev/null +++ b/src/routes/ping.go @@ -0,0 +1,9 @@ +package routes + +import ( + "github.com/valyala/fasthttp" +) + +func PingHandler(ctx *fasthttp.RequestCtx) { + ctx.SetBodyString("Pong!") +} diff --git a/src/routes/skin.go b/src/routes/skin.go new file mode 100644 index 0000000..a66af04 --- /dev/null +++ b/src/routes/skin.go @@ -0,0 +1,60 @@ +package routes + +import ( + "fmt" + "log" + "main/src/util" + "net/http" + + "github.com/valyala/fasthttp" +) + +func SkinHandler(ctx *fasthttp.RequestCtx) { + user := ctx.UserValue("user").(string) + + download := ctx.QueryArgs().GetBool("download") + + uuid, err := util.GetUUID(r, user) + + if err != nil { + log.Println(err) + + ctx.SetStatusCode(http.StatusInternalServerError) + ctx.SetBodyString(http.StatusText(http.StatusInternalServerError)) + + return + } + + skin, slim, err := util.GetPlayerSkin(r, uuid) + + if err != nil { + log.Println(err) + + ctx.SetStatusCode(http.StatusInternalServerError) + ctx.SetBodyString(http.StatusText(http.StatusInternalServerError)) + + return + } + + if skin == nil { + skin = util.GetDefaultSkin(slim) + } + + data, err := util.EncodePNG(skin) + + if err != nil { + log.Println(err) + + ctx.SetStatusCode(http.StatusInternalServerError) + ctx.SetBodyString(http.StatusText(http.StatusInternalServerError)) + + return + } + + if download { + ctx.Response.Header.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, user)) + } + + ctx.SetContentType("image/png") + ctx.SetBody(data) +} diff --git a/src/routes/uuid.go b/src/routes/uuid.go new file mode 100644 index 0000000..a57c1a9 --- /dev/null +++ b/src/routes/uuid.go @@ -0,0 +1,26 @@ +package routes + +import ( + "log" + "main/src/util" + "net/http" + + "github.com/valyala/fasthttp" +) + +func UUIDHandler(ctx *fasthttp.RequestCtx) { + user := ctx.UserValue("user").(string) + + uuid, err := util.GetUUID(r, user) + + if err != nil { + log.Println(err) + + ctx.SetStatusCode(http.StatusInternalServerError) + ctx.SetBodyString(http.StatusText(http.StatusInternalServerError)) + + return + } + + ctx.SetBodyString(uuid) +} diff --git a/src/util/alex.png b/src/util/alex.png new file mode 100644 index 0000000000000000000000000000000000000000..ffd8e0719a1a4fac1f58895ed3a30130d3aa4b46 GIT binary patch literal 2105 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