diff --git a/.air-bot.toml b/.air-bot.toml new file mode 100644 index 0000000..7515754 --- /dev/null +++ b/.air-bot.toml @@ -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 diff --git a/.air.toml b/.air.toml new file mode 100644 index 0000000..b0840ce --- /dev/null +++ b/.air.toml @@ -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 diff --git a/.gitignore b/.gitignore index 4308b6d..39dd70c 100644 --- a/.gitignore +++ b/.gitignore @@ -24,4 +24,5 @@ go.work.sum # env file .env -bin/ \ No newline at end of file +bin/ +tmp/ diff --git a/Dockerfile b/Dockerfile index 1a83065..3288ea8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,6 +4,10 @@ FROM golang:1.23-alpine AS builder # Install dependencies required to run the Makefile RUN apk add --no-cache make +# Set up Go environment variables +ENV GOPATH=/golang +ENV PATH=$GOPATH/bin:$PATH + # Set the working directory WORKDIR /app @@ -14,7 +18,7 @@ COPY . . RUN make build # Use a minimal base image for the final container -FROM alpine:latest +FROM alpine:latest AS deploy # Set the working directory WORKDIR /app @@ -24,8 +28,12 @@ RUN apk add --no-cache ca-certificates # Copy the binary from the builder COPY --from=builder /app/bin/bot /app/bin/bot +COPY --from=builder /app/bin/web /app/bin/web # Set environment variables for database connection +ENV GOPATH=/golang +ENV PATH=$GOPATH/bin:$PATH + ENV DB_HOST=postgres ENV DB_PORT=5432 ENV DB_USER=napandgo @@ -36,5 +44,36 @@ ENV DB_SSLMODE=disable # Expose any required ports (optional) EXPOSE 8080 +# Run the container as a non-root user +USER nobody + # Run the bot binary CMD ["/app/bin/bot"] + +# Development image we actually use go so we can use hot reloading with air +FROM builder AS dev + +# Set environment variables for database connection +ENV GOPATH=/golang +ENV PATH=$GOPATH/bin:$PATH + +ENV DB_HOST=postgres +ENV DB_PORT=5432 +ENV DB_USER=napandgo +ENV DB_PASSWORD=napandgo +ENV DB_NAME=napandgo +ENV DB_SSLMODE=disable + +# Install air for Go for hot reloading +RUN apk add --no-cache git +RUN go install github.com/air-verse/air@latest +RUN git config --global --add safe.directory /app + +# Expose any required ports (optional) +EXPOSE 8080 + +# Run the container as a non-root user +USER root + +# Run the bot binary +CMD ["air", "-c", ".air-bot.toml"] diff --git a/Makefile b/Makefile index 04050ab..018ee9a 100644 --- a/Makefile +++ b/Makefile @@ -23,7 +23,7 @@ bot: # Build the web interface binary web: - $(GO_BUILD_CMD) -o bin/web ./cmd/web + CGO_ENABLED=$(CGO_ENABLED) $(GO_BUILD_CMD) -o bin/web ./cmd/web # Run both bot and web run: build diff --git a/README.md b/README.md index e02364d..4844faf 100644 --- a/README.md +++ b/README.md @@ -12,3 +12,47 @@ My way to learn Go by creating a Discord bot with a bunch of features - Replies to a message when it is sent a message in a channel Features will be added as they get completed + +## Usage + +The application is designed to be run in a collection of containers with Postgres. + +``` +docker compose up -d +docker compose logs -f +``` + +## Create a Discord application and Bot + +- Go to the [Discord Developer portal](https://discord.com/developers/applications) +- Click the *New Application* button at the top right + ![Discord application page](docs/images/screen-new-app.png) +- Give the application a name + ![Discord create app modal](docs/images/screen-create-app.png) +- Click the OAuth menu item and configure the a redirect URI. Don't forget to save the page! + - For development http://localhost:3000/api/oauth + - For production https://YOURSERVER/api/oauth + ![Discord OAuth page](docs/images/screen-bot-settings.png) +- Click the *Reset Secret* button and copy the new secret that you are given. This will become DISCORD_CLIENT_SECRET in your environment file +- Copy the Client ID - this will become DISCORD_CLIENT_ID in your environment file. +- Click the Bot menu item, and configure the bot settings (you will need to enable several Intents as noted below) + ![Discord Bot page](docs/images/screen-bot-settings2.png) +- Click the *Reset Token* button and copy the new token that you receive. This will become your DISCORD_BOT_TOKEN environment variable. + +## Installation + +- Create an .env file using the .env-prod-example as a template +- Enter your values for DISCORD_CLIENT_ID, DISCORD_CLIENT_SECRET and DISCORD_BOT_TOKEN from the above procedure. + +## Development Notes + +### Project layout + +- cmd/bot -- stuff for the bot +- cmd/web -- stuff for the web UI +- config -- handles configuration (mainly loading by environment variables) +- db -- stuff for the database +- docs -- additional documentation +- models -- database models +- static -- web UI static files +- templates -- web UI templates diff --git a/cmd/web/auth.go b/cmd/web/auth.go new file mode 100644 index 0000000..354b74f --- /dev/null +++ b/cmd/web/auth.go @@ -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") +} \ No newline at end of file diff --git a/cmd/web/dashboard.go b/cmd/web/dashboard.go new file mode 100644 index 0000000..ea8593e --- /dev/null +++ b/cmd/web/dashboard.go @@ -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) +} diff --git a/cmd/web/servers.go b/cmd/web/servers.go new file mode 100644 index 0000000..5cec264 --- /dev/null +++ b/cmd/web/servers.go @@ -0,0 +1,75 @@ +// Package main provides the web UI +// servers.go has the handlers for the server view +package main + +import ( + "context" + "encoding/json" + "log" + "net/http" + + "github.com/gin-gonic/gin" + "github.com/gin-contrib/sessions" + "golang.org/x/oauth2" + + "github.com/faulteh/nap-and-go/config" +) + +// Guild represents a Discord guild/server +type Guild struct { + ID string `json:"id"` + Name string `json:"name"` + Icon string `json:"icon"` + Owner bool `json:"owner"` + Permissions int64 `json:"permissions"` +} + +// serversHandler handles the server view +func serversHandler(c *gin.Context) { + // Get the user session + session := sessions.Default(c) + token := session.Get("token").(*oauth2.Token) + + // Retrieve list of servers for user from discord + oauth2Config := config.LoadDiscordConfig().OAuth2Config() + client := oauth2Config.Client(context.Background(), token) + resp, err := client.Get("https://discord.com/api/users/@me/guilds") + if err != nil { + log.Printf("Failed to get user info: %v\n", err) + // For now just return an error + c.JSON(http.StatusInternalServerError, gin.H{ + "error": "Failed to get user info", + "msg": err.Error(), + }) + return + } + defer resp.Body.Close() //nolint:errcheck + + if resp.StatusCode != http.StatusOK { + log.Printf("Discord API responded with status %d\n", resp.StatusCode) + c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch servers"}) + return + } + + var guilds []Guild + if err := json.NewDecoder(resp.Body).Decode(&guilds); err != nil { + log.Printf("Error decoding guilds response: %v\n", err) + c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to decode servers"}) + return + } + + // Filter guilds where the user has admin permissions + var adminGuilds []Guild + const ADMINISTRATOR = 0x00000008 + for _, guild := range guilds { + if guild.Permissions&ADMINISTRATOR != 0 { + adminGuilds = append(adminGuilds, guild) + } + } + + // Render the servers view + c.HTML(http.StatusOK, "servers.html", gin.H{ + "Title": "Servers", + "Servers": adminGuilds, + }) +} diff --git a/cmd/web/web.go b/cmd/web/web.go new file mode 100644 index 0000000..4b750b0 --- /dev/null +++ b/cmd/web/web.go @@ -0,0 +1,62 @@ +// Package web runs Gin based webserver for the application. +package main + +import ( + "log" + "encoding/gob" + + "github.com/faulteh/nap-and-go/config" + "github.com/gin-contrib/sessions" + "github.com/gin-contrib/sessions/cookie" + "github.com/gin-gonic/gin" + "golang.org/x/oauth2" +) + +func main() { + // Initialize the web server + router := gin.Default() + + // Register the routes + registerRoutes(router) + + // Run the web server + addr := ":8080" + log.Printf("Starting web server on %s", addr) + err := router.Run(addr) + if err != nil { + log.Fatalf("failed to start web server: %v", err) + } +} + +// registerRoutes registers the routes for the web server. +func registerRoutes(router *gin.Engine) { + // Register map[string]interface{} with gob as we use it for user session data + gob.Register(map[string]interface{}{}) + // Register oauth2.Token with gob as we use it for user session data + gob.Register(&oauth2.Token{}) + + // Set up session store to store login sessions + store := cookie.NewStore([]byte(config.LoadSessionStoreSecret())) + router.Use(sessions.Sessions("nap-and-go", store)) + + // Load the templates + router.LoadHTMLGlob("templates/*.html") + + // Static files + router.Static("/static", "./static") + + // Define the routes + router.GET("/", homeHandler) + + // Auth routes + router.GET("/login", loginPageHandler) + router.GET("/auth/login", loginRedirectHandler) + router.GET("/auth/callback", discordCallbackHandler) + router.GET("/logout", logoutHandler) + + authenticated := router.Group("/servers") + authenticated.Use(AuthRequired()) + + authenticated.GET("/", serversHandler) + authenticated.GET("/:id", dashboardHandler) +} diff --git a/config/config.go b/config/config.go index 6132555..a14e8f2 100644 --- a/config/config.go +++ b/config/config.go @@ -4,6 +4,8 @@ package config import ( "fmt" "os" + + "golang.org/x/oauth2" ) // DBConfig holds the database configuration. @@ -16,6 +18,14 @@ type DBConfig struct { SSLMode string } +// DiscordConfig holds the Discord configuration. +type DiscordConfig struct { + clientID string + clientSecret string + botToken string + redirectURL string +} + // LoadDBConfig loads the database configuration from environment variables. func LoadDBConfig() *DBConfig { return &DBConfig{ @@ -42,3 +52,35 @@ func (c *DBConfig) DSN() string { return fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=%s", c.Host, c.Port, c.User, c.Password, c.DBName, c.SSLMode) } + +// Define Discord's OAuth2 endpoint +var discordEndpoint = oauth2.Endpoint{ + AuthURL: "https://discord.com/api/oauth2/authorize", + TokenURL: "https://discord.com/api/oauth2/token", +} + +// LoadDiscordConfig loads the Discord configuration from environment variables. +func LoadDiscordConfig() *DiscordConfig { + return &DiscordConfig{ + clientID: getEnv("DISCORD_CLIENT_ID", ""), + clientSecret: getEnv("DISCORD_CLIENT_SECRET", ""), + botToken: getEnv("DISCORD_BOT_TOKEN", ""), + redirectURL: getEnv("DISCORD_REDIRECT_URL", ""), + } +} + +// OAuth2Config Extend the DiscordConfig struct with a method OAuth2Config that returns an oauth2.Config. +func (c *DiscordConfig) OAuth2Config() *oauth2.Config { + return &oauth2.Config{ + ClientID: c.clientID, + ClientSecret: c.clientSecret, + Endpoint: discordEndpoint, + RedirectURL: c.redirectURL, + Scopes: []string{"identify", "email", "guilds"}, + } +} + +// LoadSessionStoreSecret Load the session store secret from an environment variable. +func LoadSessionStoreSecret() string { + return getEnv("SESSION_STORE_SECRET", "") +} diff --git a/docker-compose.yml b/docker-compose.yml index d0591cb..d2acb49 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,20 +3,37 @@ services: build: context: . dockerfile: Dockerfile + target: dev container_name: bot depends_on: - postgres - environment: - - DB_HOST=postgres - - DB_PORT=5432 - - DB_USER=${DB_USER} - - DB_PASSWORD=${DB_PASSWORD} - - DB_NAME=${DB_NAME} - - DB_SSLMODE=disable + # Get environment variables from .env file + env_file: + - path: ./.env volumes: - - ./bin:/app/bin - command: ["sh", "-c", "while :; do sleep infinity; done"] + - .:/app # command: ["./bin/bot"] + # For development we just run air + command: ["air", "-c", ".air-bot.toml"] + restart: always + + web: + build: + context: . + dockerfile: Dockerfile + target: dev + container_name: web + depends_on: + - bot + env_file: + - path: ./.env + volumes: + - .:/app + ports: + - "8080:8080" + # command: ["./bin/web"] + # For development we just run air + command: ["air"] restart: always postgres: diff --git a/docs/images/screen-bot-settings.png b/docs/images/screen-bot-settings.png new file mode 100644 index 0000000..d80130e Binary files /dev/null and b/docs/images/screen-bot-settings.png differ diff --git a/docs/images/screen-bot-settings2.png b/docs/images/screen-bot-settings2.png new file mode 100644 index 0000000..ec93797 Binary files /dev/null and b/docs/images/screen-bot-settings2.png differ diff --git a/docs/images/screen-create-app.png b/docs/images/screen-create-app.png new file mode 100644 index 0000000..aa3aa29 Binary files /dev/null and b/docs/images/screen-create-app.png differ diff --git a/docs/images/screen-new-app.png b/docs/images/screen-new-app.png new file mode 100644 index 0000000..0b645c5 Binary files /dev/null and b/docs/images/screen-new-app.png differ diff --git a/go.mod b/go.mod index 46ebcc5..0f77948 100644 --- a/go.mod +++ b/go.mod @@ -3,12 +3,27 @@ module github.com/faulteh/nap-and-go go 1.23.4 require ( + github.com/bwmarrin/discordgo v0.28.1 + github.com/gin-gonic/gin v1.10.0 gorm.io/driver/postgres v1.5.11 gorm.io/gorm v1.25.12 ) require ( - github.com/bwmarrin/discordgo v0.28.1 // indirect + github.com/bytedance/sonic v1.11.6 // indirect + github.com/bytedance/sonic/loader v0.1.1 // indirect + github.com/cloudwego/base64x v0.1.4 // indirect + github.com/cloudwego/iasm v0.2.0 // indirect + github.com/gabriel-vasile/mimetype v1.4.3 // indirect + github.com/gin-contrib/sessions v1.0.2 // indirect + github.com/gin-contrib/sse v0.1.0 // indirect + github.com/go-playground/locales v0.14.1 // indirect + github.com/go-playground/universal-translator v0.18.1 // indirect + github.com/go-playground/validator/v10 v10.20.0 // indirect + github.com/goccy/go-json v0.10.2 // indirect + github.com/gorilla/context v1.1.2 // indirect + github.com/gorilla/securecookie v1.1.2 // indirect + github.com/gorilla/sessions v1.2.2 // indirect github.com/gorilla/websocket v1.4.2 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect @@ -16,14 +31,28 @@ require ( github.com/jackc/puddle/v2 v2.2.2 // 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.7 // indirect + github.com/kr/text v0.2.0 // indirect + github.com/leodido/go-urn v1.4.0 // indirect + github.com/mattn/go-isatty v0.0.20 // 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.2 // indirect + github.com/rogpeppe/go-internal v1.13.1 // indirect + github.com/twitchyliquid64/golang-asm v0.15.1 // indirect + github.com/ugorji/go/codec v1.2.12 // indirect + golang.org/x/arch v0.8.0 // indirect golang.org/x/crypto v0.31.0 // indirect + golang.org/x/net v0.33.0 // indirect + golang.org/x/oauth2 v0.24.0 // indirect golang.org/x/sync v0.10.0 // indirect golang.org/x/sys v0.28.0 // indirect golang.org/x/text v0.21.0 // indirect + google.golang.org/protobuf v1.34.1 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) -replace github.com/faulteh/nap-and-go/pkg => ./pkg - replace github.com/faulteh/nap-and-go/config => ./config replace github.com/faulteh/nap-and-go/db => ./db diff --git a/go.sum b/go.sum index 6bd440a..19ae2ac 100644 --- a/go.sum +++ b/go.sum @@ -1,8 +1,45 @@ github.com/bwmarrin/discordgo v0.28.1 h1:gXsuo2GBO7NbR6uqmrrBDplPUx2T3nzu775q/Rd1aG4= github.com/bwmarrin/discordgo v0.28.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY= +github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0= +github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4= +github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM= +github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= +github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y= +github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w= +github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg= +github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY= +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= 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/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0= +github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk= +github.com/gin-contrib/sessions v1.0.2 h1:UaIjUvTH1cMeOdj3in6dl+Xb6It8RiKRF9Z1anbUyCA= +github.com/gin-contrib/sessions v1.0.2/go.mod h1:KxKxWqWP5LJVDCInulOl4WbLzK2KSPlLesfZ66wRvMs= +github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= +github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= +github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU= +github.com/gin-gonic/gin v1.10.0/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y= +github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= +github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= +github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= +github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= +github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= +github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= +github.com/go-playground/validator/v10 v10.20.0 h1:K9ISHbSaI0lyB2eWMPJo+kOS/FBExVwjEviJTixqxL8= +github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM= +github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= +github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= +github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/gorilla/context v1.1.2 h1:WRkNAv2uoa03QNIc1A6u4O7DAGMUVoopZhkiXWA2V1o= +github.com/gorilla/context v1.1.2/go.mod h1:KDPwT9i/MeWHiLl90fuTgrt4/wPcv75vFAZLaOOcbxM= +github.com/gorilla/securecookie v1.1.2 h1:YCIWL56dvtr73r6715mJs5ZvhtnY73hBvEF8kXD8ePA= +github.com/gorilla/securecookie v1.1.2/go.mod h1:NfCASbcHqRSY+3a8tlWJwsQap2VX5pwzwo4h3eOamfo= +github.com/gorilla/sessions v1.2.2 h1:lqzMYz6bOfvn2WriPUjNByzeXIlVzURcPmgMczkmTjY= +github.com/gorilla/sessions v1.2.2/go.mod h1:ePLdVu+jbEgHH+KWw8I1z2wqd0BAdAQh/8LRvBeoNcQ= github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= @@ -17,20 +54,65 @@ github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD 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= +github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM= +github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= +github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M= +github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= +github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +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/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= +github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= +github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM= +github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= 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/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII= +github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= +github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= +github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE= +github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= +golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= +golang.org/x/arch v0.8.0 h1:3wRIsP3pM4yUptoR96otTUOXI367OS0+c9eeRi9doIc= +golang.org/x/arch v0.8.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U= golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac= +golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM= +golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I= +golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4= +golang.org/x/oauth2 v0.24.0 h1:KTBBxWqUa0ykRPLtV69rRto9TLXcqYkeswu48x/gvNE= +golang.org/x/oauth2 v0.24.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI= golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ= golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA= golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= @@ -38,7 +120,13 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo= golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg= +google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= 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= @@ -46,3 +134,5 @@ gorm.io/driver/postgres v1.5.11 h1:ubBVAfbKEUld/twyKZ0IYn9rSQh448EdelLYk9Mv314= gorm.io/driver/postgres v1.5.11/go.mod h1:DX3GReXH+3FPWGrrgffdvCk3DQ1dwDPdmbenSkweRGI= 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/pkg/greetings/greetings.go b/pkg/greetings/greetings.go deleted file mode 100644 index 772e41b..0000000 --- a/pkg/greetings/greetings.go +++ /dev/null @@ -1,52 +0,0 @@ -// Package greetings contains a single function, Hello, which takes a name as input and returns a greeting. -package greetings - -import ( - "errors" - "fmt" - "math/rand" -) - -// Hello returns a greeting for the named person. -func Hello(name string) (string, error) { - // If no name was given, return an error with a message. - if name == "" { - return "", errors.New("empty name") - } - - // Return a greeting that embeds the name in a message. - message := fmt.Sprintf(randomFormat(), name) - return message, nil -} - -// Hellos returns a map that associates each of the named people -// with a greeting message. -func Hellos(names []string) (map[string]string, error) { - // A map to associate names with messages. - messages := make(map[string]string) - // Loop through the received slice of names, calling - // the Hello function to get a message for each name. - for _, name := range names { - message, err := Hello(name) - if err != nil { - return nil, err - } - // In the map, associate the retrieved message with - // the name. - messages[name] = message - } - return messages, nil -} - -func randomFormat() string { - // A slice of message formats. - formats := []string{ - "Hi, %v. Welcome!", - "Great to see you, %v!", - "Hail, %v! Well met!", - } - - // Return a randomly selected message format by specifying - // a random index for the slice of formats. - return formats[rand.Intn(len(formats))] -} diff --git a/pkg/greetings/greetings_test.go b/pkg/greetings/greetings_test.go deleted file mode 100644 index a3bd0d3..0000000 --- a/pkg/greetings/greetings_test.go +++ /dev/null @@ -1,26 +0,0 @@ -package greetings - -import ( - "testing" - "regexp" -) - -// TestHelloName calls greetings.Hello with a name, checking -// for a valid return value. -func TestHelloName(t *testing.T) { - name := "Ardenne" - want := regexp.MustCompile(`\b` + name + `\b`) - msg, err := Hello("Ardenne") - if !want.MatchString(msg) || err != nil { - t.Fatalf(`Hello("Ardenne") = %q, %v, want match for %#q, nil`, msg, err, want) - } -} - -// TestHelloEmpty calls greetings.Hello with an empty string, -// checking for an error. -func TestHelloEmpty(t *testing.T) { - msg, err := Hello("") - if msg != "" || err == nil { - t.Fatalf(`Hello("") = %q, %v, want "", error`, msg, err) - } -} diff --git a/templates/dashboard.html b/templates/dashboard.html new file mode 100644 index 0000000..e83fe83 --- /dev/null +++ b/templates/dashboard.html @@ -0,0 +1,3 @@ +{{ template "header.html" . }} +

Dashboard

+{{ template "footer.html" }} diff --git a/templates/footer.html b/templates/footer.html new file mode 100644 index 0000000..23f24f9 --- /dev/null +++ b/templates/footer.html @@ -0,0 +1,8 @@ +{{ define "footer.html"}} + + + + +{{ end }} \ No newline at end of file diff --git a/templates/header.html b/templates/header.html new file mode 100644 index 0000000..7baa23f --- /dev/null +++ b/templates/header.html @@ -0,0 +1,43 @@ +{{ define "header.html"}} + + + + + + {{ .Title }} + + + +
+

{{ .Title }}

+ +
+
+{{ end }} \ No newline at end of file diff --git a/templates/login.html b/templates/login.html new file mode 100644 index 0000000..e3c5b8c --- /dev/null +++ b/templates/login.html @@ -0,0 +1,57 @@ + + + + + + Login + + + + +
+

Welcome

+

Please log in to continue.

+ Login with Discord +
+ + diff --git a/templates/servers.html b/templates/servers.html new file mode 100644 index 0000000..7e32d8b --- /dev/null +++ b/templates/servers.html @@ -0,0 +1,12 @@ +{{ template "header.html" . }} +{{ if .Servers }} +

Your Servers

+ +{{ else }} +

You are not in any servers.

+{{ end }} +{{ template "footer.html" }}