From ccb06f2184c5e14476bea1527104b80139773f29 Mon Sep 17 00:00:00 2001 From: Ada Werefox Date: Mon, 21 Apr 2025 21:05:50 -0700 Subject: [PATCH] We can interface with a db now. --- .gitignore | 3 ++ db/.gitkeep | 0 src/gin-cpularp.go => gin-cpularp.go | 19 +++++----- go.mod | 9 +++++ go.sum | 10 ++++++ lib/dbcommands/dbcommands.go | 24 +++++++++++++ lib/dbcommands/go.mod | 12 +++++++ lib/dbcommands/go.sum | 14 ++++++++ src/css/style.css | 7 +++- src/templates/badrequest.html | 2 +- src/templates/dashboard.html | 20 +++++++---- src/templates/infoblock.html | 0 src/templates/internalservererror.html | 2 +- src/templates/root.html | 11 +++--- src/templates/userinfo.html | 50 +++++++++----------------- tailwind.config.js | 0 16 files changed, 126 insertions(+), 57 deletions(-) create mode 100644 db/.gitkeep rename src/gin-cpularp.go => gin-cpularp.go (94%) create mode 100644 lib/dbcommands/dbcommands.go create mode 100644 lib/dbcommands/go.mod create mode 100644 lib/dbcommands/go.sum create mode 100644 src/templates/infoblock.html create mode 100644 tailwind.config.js diff --git a/.gitignore b/.gitignore index 7bd16c5..a38a6f9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,9 @@ # Ignore config, don't commit credentials. **/config.toml +# Ignore db/, don't commit the database. +*.db + # Ignore node_modules. **/node_modules/* diff --git a/db/.gitkeep b/db/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/src/gin-cpularp.go b/gin-cpularp.go similarity index 94% rename from src/gin-cpularp.go rename to gin-cpularp.go index 1b3ec10..ef0a6f6 100644 --- a/src/gin-cpularp.go +++ b/gin-cpularp.go @@ -8,6 +8,7 @@ import ( "os" "strconv" + // "example.com/lib/dbcommands" "github.com/gin-gonic/gin" "github.com/pelletier/go-toml/v2" "github.com/ravener/discord-oauth2" @@ -24,7 +25,6 @@ type appConfig struct { } type discordUser struct { - LoggedIn bool Id string Username string Avatar string @@ -51,7 +51,6 @@ type discordUser struct { var config appConfig var oauthConfig *oauth2.Config var oauthToken *oauth2.Token -var userInfo discordUser func parseConfig(configPath string) { configFile, err := os.Open(configPath) @@ -80,7 +79,7 @@ func createDiscordOAuthConfig(clientId string, clientSecret string, redirectUrl } func loginDisplay(context *gin.Context) { - if userInfo.LoggedIn { + if oauthToken.Valid() { context.Redirect(http.StatusTemporaryRedirect, "/dashboard") } else { context.HTML(http.StatusOK, "root.html", gin.H{ @@ -104,6 +103,10 @@ func authCallback(context *gin.Context) { "message": "internalservererror", }) } + context.Redirect(http.StatusTemporaryRedirect, "/dashboard") +} + +func getDiscordUserInfo(context *gin.Context) discordUser { response, err := oauthConfig.Client(context.Request.Context(), oauthToken).Get("https://discord.com/api/users/@me") if err != nil || response.StatusCode != 200 { responseMessage := "" @@ -125,13 +128,12 @@ func authCallback(context *gin.Context) { } var discordUserInfo discordUser json.Unmarshal(body, &discordUserInfo) - discordUserInfo.LoggedIn = true - userInfo = discordUserInfo - context.Redirect(http.StatusTemporaryRedirect, "/dashboard") + return discordUserInfo } func dashboardDisplay(context *gin.Context) { - if userInfo.LoggedIn { + if oauthToken.Valid() { + userInfo := getDiscordUserInfo(context) context.HTML(http.StatusOK, "dashboard.html", gin.H{ "discordUser": gin.H{ "id": userInfo.Id, @@ -163,6 +165,7 @@ func dashboardDisplay(context *gin.Context) { } func main() { + // db := dbcommands.InitializeDatabase() parseConfig("config.toml") oauthConfig = createDiscordOAuthConfig(config.OAuth.ClientID, config.OAuth.ClientSecret, config.OAuth.RedirectUrl) app := gin.Default() @@ -172,5 +175,5 @@ func main() { app.GET("/login", loginRedirect) app.GET("/auth/callback", authCallback) app.GET("/dashboard", dashboardDisplay) - app.Run() + app.Run(":8080") } diff --git a/go.mod b/go.mod index 0eb5a4f..d0181b6 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,10 @@ module gin-cpularp go 1.24.2 +replace example.com/lib/dbcommands => ./lib/dbcommands + require ( + example.com/lib/dbcommands v0.0.0 github.com/bytedance/sonic v1.13.2 // indirect github.com/bytedance/sonic/loader v0.2.4 // indirect github.com/cloudwego/base64x v0.1.5 // indirect @@ -14,10 +17,13 @@ require ( github.com/go-playground/universal-translator v0.18.1 // indirect github.com/go-playground/validator/v10 v10.26.0 // indirect github.com/goccy/go-json v0.10.5 // indirect + github.com/jinzhu/inflection v1.0.0 // indirect + github.com/jinzhu/now v1.1.5 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/klauspost/cpuid/v2 v2.2.10 // indirect github.com/leodido/go-urn v1.4.0 // indirect github.com/mattn/go-isatty v0.0.20 // indirect + github.com/mattn/go-sqlite3 v1.14.22 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/pelletier/go-toml/v2 v2.2.4 // indirect @@ -33,4 +39,7 @@ require ( golang.org/x/text v0.24.0 // indirect google.golang.org/protobuf v1.36.6 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect + gorm.io/driver/sqlite v1.5.7 // indirect + gorm.io/gorm v1.25.12 // indirect ) + diff --git a/go.sum b/go.sum index bbcb6c6..d29dfc8 100644 --- a/go.sum +++ b/go.sum @@ -24,6 +24,10 @@ github.com/go-playground/validator/v10 v10.26.0/go.mod h1:I5QpIEbmr8On7W0TktmJAu github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4= github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= +github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= +github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= +github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= @@ -34,6 +38,8 @@ 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/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.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU= +github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= @@ -77,5 +83,9 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8 gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gorm.io/driver/sqlite v1.5.7 h1:8NvsrhP0ifM7LX9G4zPB97NwovUakUxc+2V2uuf3Z1I= +gorm.io/driver/sqlite v1.5.7/go.mod h1:U+J8craQU6Fzkcvu8oLeAQmi50TkwPEhHDEjQZXDah4= +gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8= +gorm.io/gorm v1.25.12/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ= nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50= rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4= diff --git a/lib/dbcommands/dbcommands.go b/lib/dbcommands/dbcommands.go new file mode 100644 index 0000000..602d4d2 --- /dev/null +++ b/lib/dbcommands/dbcommands.go @@ -0,0 +1,24 @@ +package dbcommands + +import ( + "log" + + "gorm.io/driver/sqlite" + "gorm.io/gorm" +) + +type User struct { + gorm.Model + Name string + Id string + LoginToken string +} + +func InitializeDatabase() *gorm.DB { + db, err := gorm.Open(sqlite.Open("db/main.db"), &gorm.Config{}) + if err != nil { + log.Fatal("Failed to connect to database.") + } + db.AutoMigrate(&User{}) + return db +} diff --git a/lib/dbcommands/go.mod b/lib/dbcommands/go.mod new file mode 100644 index 0000000..1255bec --- /dev/null +++ b/lib/dbcommands/go.mod @@ -0,0 +1,12 @@ +module example.com/lib/dbc + +go 1.24.2 + +require ( + github.com/jinzhu/inflection v1.0.0 // indirect + github.com/jinzhu/now v1.1.5 // indirect + github.com/mattn/go-sqlite3 v1.14.22 // indirect + golang.org/x/text v0.14.0 // indirect + gorm.io/driver/sqlite v1.5.7 // indirect + gorm.io/gorm v1.25.12 // indirect +) diff --git a/lib/dbcommands/go.sum b/lib/dbcommands/go.sum new file mode 100644 index 0000000..76138d5 --- /dev/null +++ b/lib/dbcommands/go.sum @@ -0,0 +1,14 @@ +github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= +github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= +github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= +github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU= +github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= +golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +gorm.io/driver/sqlite v1.5.7 h1:8NvsrhP0ifM7LX9G4zPB97NwovUakUxc+2V2uuf3Z1I= +gorm.io/driver/sqlite v1.5.7/go.mod h1:U+J8craQU6Fzkcvu8oLeAQmi50TkwPEhHDEjQZXDah4= +gorm.io/gorm v1.25.7-0.20240204074919-46816ad31dde h1:9DShaph9qhkIYw7QF91I/ynrr4cOO2PZra2PFD7Mfeg= +gorm.io/gorm v1.25.7-0.20240204074919-46816ad31dde/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8= +gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8= +gorm.io/gorm v1.25.12/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ= diff --git a/src/css/style.css b/src/css/style.css index bc9e89b..2b936d9 100644 --- a/src/css/style.css +++ b/src/css/style.css @@ -1 +1,6 @@ -@import "tailwindcss" source("../templates"); \ No newline at end of file +@import "tailwindcss" source("../templates"); +@theme { + --color-primary: oklch(0.1 0 0); + --color-secondary: oklch(0.85 0.1733 93.56); + --color-accent: oklch(0.6 0.1556 246.71); +} \ No newline at end of file diff --git a/src/templates/badrequest.html b/src/templates/badrequest.html index 0b86b6d..c202743 100644 --- a/src/templates/badrequest.html +++ b/src/templates/badrequest.html @@ -13,7 +13,7 @@
-
+

Oops!

There was some problem with the authentication process...

{{ .message }}

diff --git a/src/templates/dashboard.html b/src/templates/dashboard.html index 4038df9..5419b66 100644 --- a/src/templates/dashboard.html +++ b/src/templates/dashboard.html @@ -4,20 +4,26 @@ Dashboard - -
-
-
-
-

> USER AUTHENTICATED

+
+
+
+
+
+
+

> USER AUTHENTICATED

+
+
+
+
+ {{ template "userinfo.html" .discordUser }}
- {{ template "userinfo.html" .discordUser }}
diff --git a/src/templates/infoblock.html b/src/templates/infoblock.html new file mode 100644 index 0000000..e69de29 diff --git a/src/templates/internalservererror.html b/src/templates/internalservererror.html index e9345be..ca751cf 100644 --- a/src/templates/internalservererror.html +++ b/src/templates/internalservererror.html @@ -13,7 +13,7 @@
-
+

Oops!

We experienced some kind of server error...

{{ .message }}

diff --git a/src/templates/root.html b/src/templates/root.html index ae04554..8af1b41 100644 --- a/src/templates/root.html +++ b/src/templates/root.html @@ -4,7 +4,6 @@ Hello! - @@ -13,14 +12,14 @@
-
-

Welcome!

+
+

Welcome!

-
+

There really isn't much here yet, but you can log in with Discord.

- -
+ +
Login to Discord
diff --git a/src/templates/userinfo.html b/src/templates/userinfo.html index 15b239e..36f3161 100644 --- a/src/templates/userinfo.html +++ b/src/templates/userinfo.html @@ -1,35 +1,19 @@ -
-

> ID: {{ .id }}

-

> USERNAME: {{ .username }}

-

> DISPLAY NAME: {{ .global_name }}

-

> AVATAR

-
- -
-

> BANNER

-
- -
-

discriminator: {{ .discriminator }}

-

public_flags: {{ .public_flags }}

-

flags: {{ .flags }}

-

accent_color: {{ .accent_color }}

-
- avatar_decoration_data -
-
-

asset: {{ .avatar_decoration_data.asset }}

-

sku_id: {{ .avatar_decoration_data.sku_id }}

-

expires_at: {{ .avatar_decoration_data.expires_at }}

-
+
+
+
+ +
-
-

collectibles: {{ .collectibles }}

-

banner_color: {{ .banner_color }}

-

clan: {{ .clan }}

-

primary_guild: {{ .primary_guild }}

-

mfa_enabled: {{ .mfa_enabled }}

-

locale: {{ .locale }}

-

premium_type: {{ .premium_type }}

+
+

+ {{ .global_name }} +

+ + @{{ .username }} + +
+
\ No newline at end of file diff --git a/tailwind.config.js b/tailwind.config.js new file mode 100644 index 0000000..e69de29