-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from faulteh/1-add-a-web-framework
1 add a web framework
- Loading branch information
Showing
25 changed files
with
784 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
root = "." | ||
testdata_dir = "testdata" | ||
tmp_dir = "tmp" | ||
|
||
[build] | ||
args_bin = [] | ||
bin = "./tmp/bot" | ||
cmd = "go build -o ./tmp/bot ./cmd/bot" | ||
delay = 1000 | ||
exclude_dir = ["assets", "tmp", "vendor", "testdata"] | ||
exclude_file = [] | ||
exclude_regex = ["_test.go"] | ||
exclude_unchanged = false | ||
follow_symlink = false | ||
full_bin = "" | ||
include_dir = [] | ||
include_ext = ["go", "tpl", "tmpl", "html"] | ||
include_file = [] | ||
kill_delay = "0s" | ||
log = "bot-build-errors.log" | ||
poll = false | ||
poll_interval = 0 | ||
post_cmd = [] | ||
pre_cmd = [] | ||
rerun = false | ||
rerun_delay = 500 | ||
send_interrupt = false | ||
stop_on_error = false | ||
|
||
[color] | ||
app = "" | ||
build = "yellow" | ||
main = "magenta" | ||
runner = "green" | ||
watcher = "cyan" | ||
|
||
[log] | ||
main_only = false | ||
silent = false | ||
time = false | ||
|
||
[misc] | ||
clean_on_exit = false | ||
|
||
[proxy] | ||
app_port = 0 | ||
enabled = false | ||
proxy_port = 0 | ||
|
||
[screen] | ||
clear_on_rebuild = false | ||
keep_scroll = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
root = "." | ||
testdata_dir = "testdata" | ||
tmp_dir = "tmp" | ||
|
||
[build] | ||
args_bin = [] | ||
bin = "./tmp/web" | ||
cmd = "go build -o ./tmp/web ./cmd/web" | ||
delay = 1000 | ||
exclude_dir = ["assets", "tmp", "vendor", "testdata"] | ||
exclude_file = [] | ||
exclude_regex = ["_test.go"] | ||
exclude_unchanged = false | ||
follow_symlink = false | ||
full_bin = "" | ||
include_dir = [] | ||
include_ext = ["go", "tpl", "tmpl", "html"] | ||
include_file = [] | ||
kill_delay = "0s" | ||
log = "web-build-errors.log" | ||
poll = false | ||
poll_interval = 0 | ||
post_cmd = [] | ||
pre_cmd = [] | ||
rerun = false | ||
rerun_delay = 500 | ||
send_interrupt = false | ||
stop_on_error = false | ||
|
||
[color] | ||
app = "" | ||
build = "yellow" | ||
main = "magenta" | ||
runner = "green" | ||
watcher = "cyan" | ||
|
||
[log] | ||
main_only = false | ||
silent = false | ||
time = false | ||
|
||
[misc] | ||
clean_on_exit = false | ||
|
||
[proxy] | ||
app_port = 0 | ||
enabled = false | ||
proxy_port = 0 | ||
|
||
[screen] | ||
clear_on_rebuild = false | ||
keep_scroll = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,4 +24,5 @@ go.work.sum | |
# env file | ||
.env | ||
|
||
bin/ | ||
bin/ | ||
tmp/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
// Package web runs Gin based webserver for the application. | ||
// auth.go has endpoints to handle authentication and session management | ||
package main | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"log" | ||
"net/http" | ||
|
||
"github.com/gin-contrib/sessions" | ||
"github.com/gin-gonic/gin" | ||
"golang.org/x/oauth2" | ||
|
||
"github.com/faulteh/nap-and-go/config" | ||
) | ||
|
||
// AuthRequired is middleware to check if the user is authenticated | ||
func AuthRequired() gin.HandlerFunc { | ||
return func(c *gin.Context) { | ||
session := sessions.Default(c) | ||
user := session.Get("user") | ||
if user == nil { | ||
// User not logged in, redirect to login | ||
c.Redirect(http.StatusFound, "/login") | ||
c.Abort() | ||
return | ||
} | ||
// User is logged in; proceed to the next handler | ||
c.Next() | ||
} | ||
} | ||
|
||
// homeHandler redirects to login or servers page based on the session | ||
func homeHandler(c *gin.Context) { | ||
session := sessions.Default(c) | ||
user := session.Get("user") | ||
if user == nil { | ||
// Redirect to login page | ||
c.Redirect(http.StatusFound, "/login") | ||
return | ||
} | ||
// Redirect to servers page | ||
c.Redirect(http.StatusFound, "/servers/") | ||
} | ||
|
||
// loginPageHandler handles the home page rendering login page | ||
func loginPageHandler(c *gin.Context) { | ||
// Render the home page | ||
c.HTML(http.StatusOK, "login.html", nil) | ||
} | ||
|
||
// loginRedirectHandler redirects the user to the Discord OAuth2 login page | ||
func loginRedirectHandler(c *gin.Context) { | ||
oauth2Config := config.LoadDiscordConfig().OAuth2Config() | ||
url := oauth2Config.AuthCodeURL("state-token", oauth2.AccessTypeOffline) | ||
c.Redirect(http.StatusFound, url) | ||
} | ||
|
||
// discordCallbackHandler handles the Discord OAuth2 callback | ||
func discordCallbackHandler(c *gin.Context) { | ||
state := c.Query("state") | ||
if state != "state-token" { | ||
// Redirect back to login page such that the login page | ||
c.Redirect(http.StatusFound, "/login") | ||
return | ||
} | ||
|
||
code := c.Query("code") | ||
if code == "" { | ||
// Redirect back to login page such that the login page | ||
c.Redirect(http.StatusFound, "/login") | ||
return | ||
} | ||
|
||
// Exchange code for token | ||
oauth2Config := config.LoadDiscordConfig().OAuth2Config() | ||
token, err := oauth2Config.Exchange(context.Background(), code) | ||
if err != nil { | ||
log.Printf("Token exchange failed: %v\n", err) | ||
// Redirect back to login page such that the login page | ||
c.Redirect(http.StatusFound, "/login") | ||
return | ||
} | ||
|
||
// Fetch user info | ||
client := oauth2Config.Client(context.Background(), token) | ||
resp, err := client.Get("https://discord.com/api/users/@me") | ||
if err != nil { | ||
log.Printf("Failed to get user info: %v\n", err) | ||
// Redirect back to login page such that the login page | ||
c.Redirect(http.StatusFound, "/login") | ||
return | ||
} | ||
defer resp.Body.Close() //nolint:errcheck | ||
|
||
var user map[string]interface{} | ||
if err := json.NewDecoder(resp.Body).Decode(&user); err != nil { | ||
log.Printf("Failed to parse user info: %v\n", err) | ||
// Redirect back to login page such that the login page | ||
c.Redirect(http.StatusFound, "/login") | ||
return | ||
} | ||
|
||
// Store user data in session | ||
session := sessions.Default(c) | ||
session.Set("user", user) | ||
session.Set("token", token) | ||
if err := session.Save(); err != nil { | ||
log.Printf("Failed to save session: %v\n", err) | ||
// Redirect back to login page such that the login page | ||
c.Redirect(http.StatusFound, "/login") | ||
return | ||
} | ||
|
||
// Redirect to the servers page | ||
c.Redirect(http.StatusFound, "/servers/") | ||
} | ||
|
||
// logoutHandler handles the logout | ||
func logoutHandler(c *gin.Context) { | ||
session := sessions.Default(c) | ||
session.Clear() | ||
session.Save() //nolint:errcheck | ||
// Redirect back to login page | ||
c.Redirect(http.StatusFound, "/login") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Package main provides the web UI | ||
// dashboard.go has the handlers for the dashboard view and configuration of the bot | ||
package main | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
// dashboardHandler handles the dashboard view | ||
func dashboardHandler(c *gin.Context) { | ||
// Render the dashboard view | ||
c.HTML(http.StatusOK, "dashboard.html", nil) | ||
} |
Oops, something went wrong.