diff options
author | Doog <157747121+doogongithub@users.noreply.github.com> | 2024-02-21 22:07:27 -0500 |
---|---|---|
committer | Doog <157747121+doogongithub@users.noreply.github.com> | 2024-02-21 22:07:27 -0500 |
commit | 3eafb413a48cde60dea8a7355ee621c6acca952f (patch) | |
tree | 044bb4ef57033a33024a3a3ffe6ff64f9a00dc9c /api | |
parent | fac21fa0a72d4a7f1a01ccd44e3acf9c90fd95bd (diff) |
first commit
Diffstat (limited to 'api')
-rw-r--r-- | api/go.mod | 32 | ||||
-rw-r--r-- | api/go.sum | 80 | ||||
-rw-r--r-- | api/main.go | 99 |
3 files changed, 211 insertions, 0 deletions
diff --git a/api/go.mod b/api/go.mod new file mode 100644 index 0000000..02f7c09 --- /dev/null +++ b/api/go.mod | |||
@@ -0,0 +1,32 @@ | |||
1 | module water/api | ||
2 | |||
3 | go 1.18 | ||
4 | |||
5 | require ( | ||
6 | github.com/bytedance/sonic v1.11.0 // indirect | ||
7 | github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect | ||
8 | github.com/chenzhuoyu/iasm v0.9.1 // indirect | ||
9 | github.com/gabriel-vasile/mimetype v1.4.3 // indirect | ||
10 | github.com/gin-contrib/sse v0.1.0 // indirect | ||
11 | github.com/gin-gonic/gin v1.9.1 // indirect | ||
12 | github.com/go-playground/locales v0.14.1 // indirect | ||
13 | github.com/go-playground/universal-translator v0.18.1 // indirect | ||
14 | github.com/go-playground/validator/v10 v10.18.0 // indirect | ||
15 | github.com/goccy/go-json v0.10.2 // indirect | ||
16 | github.com/json-iterator/go v1.1.12 // indirect | ||
17 | github.com/klauspost/cpuid/v2 v2.2.6 // indirect | ||
18 | github.com/leodido/go-urn v1.4.0 // indirect | ||
19 | github.com/mattn/go-isatty v0.0.20 // indirect | ||
20 | github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect | ||
21 | github.com/modern-go/reflect2 v1.0.2 // indirect | ||
22 | github.com/pelletier/go-toml/v2 v2.1.1 // indirect | ||
23 | github.com/twitchyliquid64/golang-asm v0.15.1 // indirect | ||
24 | github.com/ugorji/go/codec v1.2.12 // indirect | ||
25 | golang.org/x/arch v0.7.0 // indirect | ||
26 | golang.org/x/crypto v0.19.0 // indirect | ||
27 | golang.org/x/net v0.21.0 // indirect | ||
28 | golang.org/x/sys v0.17.0 // indirect | ||
29 | golang.org/x/text v0.14.0 // indirect | ||
30 | google.golang.org/protobuf v1.32.0 // indirect | ||
31 | gopkg.in/yaml.v3 v3.0.1 // indirect | ||
32 | ) | ||
diff --git a/api/go.sum b/api/go.sum new file mode 100644 index 0000000..eff6af1 --- /dev/null +++ b/api/go.sum | |||
@@ -0,0 +1,80 @@ | |||
1 | github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM= | ||
2 | github.com/bytedance/sonic v1.10.0-rc/go.mod h1:ElCzW+ufi8qKqNW0FY314xriJhyJhuoJ3gFZdAHF7NM= | ||
3 | github.com/bytedance/sonic v1.11.0 h1:FwNNv6Vu4z2Onf1++LNzxB/QhitD8wuTdpZzMTGITWo= | ||
4 | github.com/bytedance/sonic v1.11.0/go.mod h1:iZcSUejdk5aukTND/Eu/ivjQuEL0Cu9/rf50Hi0u/g4= | ||
5 | github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY= | ||
6 | github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk= | ||
7 | github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d h1:77cEq6EriyTZ0g/qfRdp61a3Uu/AWrgIq2s0ClJV1g0= | ||
8 | github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d/go.mod h1:8EPpVsBuRksnlj1mLy4AWzRNQYxauNi62uWcE3to6eA= | ||
9 | github.com/chenzhuoyu/iasm v0.9.0/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog= | ||
10 | github.com/chenzhuoyu/iasm v0.9.1 h1:tUHQJXo3NhBqw6s33wkGn9SP3bvrWLdlVIJ3hQBL7P0= | ||
11 | github.com/chenzhuoyu/iasm v0.9.1/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog= | ||
12 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
13 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
14 | github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0= | ||
15 | github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk= | ||
16 | github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= | ||
17 | github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= | ||
18 | github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg= | ||
19 | github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU= | ||
20 | github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= | ||
21 | github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= | ||
22 | github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= | ||
23 | github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= | ||
24 | github.com/go-playground/validator/v10 v10.18.0 h1:BvolUXjp4zuvkZ5YN5t7ebzbhlUtPsPm2S9NAZ5nl9U= | ||
25 | github.com/go-playground/validator/v10 v10.18.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM= | ||
26 | github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= | ||
27 | github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= | ||
28 | github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= | ||
29 | github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= | ||
30 | github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= | ||
31 | github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= | ||
32 | github.com/klauspost/cpuid/v2 v2.2.6 h1:ndNyv040zDGIDh8thGkXYjnFtiN02M1PVVF+JE/48xc= | ||
33 | github.com/klauspost/cpuid/v2 v2.2.6/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= | ||
34 | github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M= | ||
35 | github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= | ||
36 | github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= | ||
37 | github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= | ||
38 | github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= | ||
39 | github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= | ||
40 | github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= | ||
41 | github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= | ||
42 | github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= | ||
43 | github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= | ||
44 | github.com/pelletier/go-toml/v2 v2.1.1 h1:LWAJwfNvjQZCFIDKWYQaM62NcYeYViCmWIwmOStowAI= | ||
45 | github.com/pelletier/go-toml/v2 v2.1.1/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc= | ||
46 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
47 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
48 | github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= | ||
49 | github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= | ||
50 | github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= | ||
51 | github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | ||
52 | github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | ||
53 | github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= | ||
54 | github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= | ||
55 | github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= | ||
56 | github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= | ||
57 | github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= | ||
58 | github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE= | ||
59 | github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= | ||
60 | golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= | ||
61 | golang.org/x/arch v0.7.0 h1:pskyeJh/3AmoQ8CPE95vxHLqp1G1GfGNXTmcl9NEKTc= | ||
62 | golang.org/x/arch v0.7.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= | ||
63 | golang.org/x/crypto v0.19.0 h1:ENy+Az/9Y1vSrlrvBSyna3PITt4tiZLf7sgCjZBX7Wo= | ||
64 | golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU= | ||
65 | golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4= | ||
66 | golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44= | ||
67 | golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
68 | golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
69 | golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y= | ||
70 | golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= | ||
71 | golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= | ||
72 | golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= | ||
73 | google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I= | ||
74 | google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= | ||
75 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
76 | gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= | ||
77 | gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
78 | gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= | ||
79 | nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50= | ||
80 | rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4= | ||
diff --git a/api/main.go b/api/main.go new file mode 100644 index 0000000..ebae5d1 --- /dev/null +++ b/api/main.go | |||
@@ -0,0 +1,99 @@ | |||
1 | package main | ||
2 | |||
3 | import ( | ||
4 | "net/http" | ||
5 | "crypto/rand" | ||
6 | "encoding/base64" | ||
7 | |||
8 | "github.com/gin-gonic/gin" | ||
9 | ) | ||
10 | |||
11 | func CORSMiddleware() gin.HandlerFunc { | ||
12 | return func(c *gin.Context) { | ||
13 | c.Writer.Header().Set("Access-Control-Allow-Origin", "*") | ||
14 | c.Writer.Header().Set("Access-Control-Allow-Credentials", "true") | ||
15 | c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With") | ||
16 | c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT") | ||
17 | |||
18 | if c.Request.Method == "OPTIONS" { | ||
19 | c.AbortWithStatus(204) | ||
20 | return | ||
21 | } | ||
22 | |||
23 | c.Next() | ||
24 | } | ||
25 | } | ||
26 | |||
27 | func generateToken() string { | ||
28 | token := make([]byte, 32) | ||
29 | rand.Read(token) | ||
30 | return base64.StdEncoding.EncodeToString(token) | ||
31 | } | ||
32 | |||
33 | type User struct { | ||
34 | Username string | ||
35 | Password string | ||
36 | } | ||
37 | |||
38 | var users = map[string]User{ | ||
39 | "user1": {"user1", "password1"}, | ||
40 | } | ||
41 | |||
42 | func setupRouter() *gin.Engine { | ||
43 | // Disable Console Color | ||
44 | // gin.DisableConsoleColor() | ||
45 | r := gin.Default() | ||
46 | r.Use(CORSMiddleware()) | ||
47 | |||
48 | api := r.Group("api/v1") | ||
49 | |||
50 | api.POST("/auth", func(c *gin.Context) { | ||
51 | username, password, ok := c.Request.BasicAuth() | ||
52 | if !ok { | ||
53 | c.Header("WWW-Authenticate", `Basic realm="Please enter your username and password."`) | ||
54 | c.AbortWithStatus(http.StatusUnauthorized) | ||
55 | return | ||
56 | } | ||
57 | |||
58 | user, exists := users[username] | ||
59 | |||
60 | if !exists || user.Password != password { | ||
61 | c.AbortWithStatus(http.StatusUnauthorized) | ||
62 | return | ||
63 | } | ||
64 | |||
65 | // Generate a simple API token | ||
66 | apiToken := generateToken() | ||
67 | c.JSON(http.StatusOK, gin.H{"token": apiToken}) | ||
68 | }) | ||
69 | |||
70 | stats := api.Group("stats") | ||
71 | |||
72 | stats.GET("/", func(c *gin.Context) { | ||
73 | c.JSON(http.StatusOK, gin.H{"status": "ok"}) | ||
74 | }) | ||
75 | |||
76 | stats.POST("/", func(c *gin.Context) { | ||
77 | c.JSON(http.StatusCreated, gin.H{"status": "created"}) | ||
78 | }) | ||
79 | |||
80 | stats.GET("/:uuid", func(c *gin.Context) { | ||
81 | c.JSON(http.StatusOK, gin.H{"status": "ok", "uuid": c.Param("uuid")}) | ||
82 | }) | ||
83 | |||
84 | stats.PATCH("/:uuid", func(c *gin.Context) { | ||
85 | c.JSON(http.StatusNoContent, gin.H{"status": "No Content"}) | ||
86 | }) | ||
87 | |||
88 | stats.DELETE("/:uuid", func(c *gin.Context) { | ||
89 | c.JSON(http.StatusNoContent, gin.H{"status": "No Content"}) | ||
90 | }) | ||
91 | |||
92 | return r | ||
93 | } | ||
94 | |||
95 | func main() { | ||
96 | r := setupRouter() | ||
97 | // Listen and Server in 0.0.0.0:8080 | ||
98 | r.Run(":8080") | ||
99 | } | ||