Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.env*
vote
.idea/
.idea/
.vscode/
4 changes: 2 additions & 2 deletions constitutional.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ func InitConstitution() {
startOfDay := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, time.Local)
nextMidnight := startOfDay.AddDate(0, 0, 1)
fmt.Println(nextMidnight)
fmt.Println(nextMidnight.Sub(time.Now()))
ticker := time.NewTicker(nextMidnight.Sub(time.Now()))
fmt.Println(time.Until(nextMidnight))
ticker := time.NewTicker(time.Until(nextMidnight))
first := true
go func() {
for {
Expand Down
6 changes: 3 additions & 3 deletions database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const (
Updated UpsertResult = 1
)

var Client *mongo.Client = Connect()
var Client *mongo.Client
var db = ""

func Connect() *mongo.Client {
Expand All @@ -33,7 +33,7 @@ func Connect() *mongo.Client {

logging.Logger.WithFields(logrus.Fields{"module": "database", "method": "Connect"}).Info("beginning database connection")

ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

uri := os.Getenv("VOTE_MONGODB_URI")
Expand All @@ -54,7 +54,7 @@ func Connect() *mongo.Client {
}

func Disconnect() {
ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

if err := Client.Disconnect(ctx); err != nil {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.24.1
require (
github.com/computersciencehouse/csh-auth v0.1.0
github.com/gin-gonic/gin v1.11.0
github.com/joho/godotenv v1.5.1
github.com/sirupsen/logrus v1.9.3
github.com/slack-go/slack v0.17.3
github.com/stretchr/testify v1.11.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
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/compress v1.18.1 h1:bcSGx7UbpBqMChDtsF28Lw6v/G94LPrrbMbdC3JH2co=
Expand Down
33 changes: 32 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/computersciencehouse/vote/logging"
"github.com/computersciencehouse/vote/sse"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"github.com/sirupsen/logrus"
"go.mongodb.org/mongo-driver/bson/primitive"
"mvdan.cc/xurls/v2"
Expand Down Expand Up @@ -57,6 +58,8 @@ func MakeLinks(s string) template.HTML {
var oidcClient = OIDCClient{}

func main() {
godotenv.Load()
database.Client = database.Connect()
r := gin.Default()
r.StaticFS("/static", http.Dir("static"))
r.SetFuncMap(template.FuncMap{
Expand Down Expand Up @@ -126,7 +129,34 @@ func main() {
closedPolls = uniquePolls(closedPolls)

c.HTML(200, "index.tmpl", gin.H{
"Polls": polls,
"Polls": polls,
"Username": claims.UserInfo.Username,
"FullName": claims.UserInfo.FullName,
})
}))

r.GET("/closed", csh.AuthWrapper(func(c *gin.Context) {
cl, _ := c.Get("cshauth")
claims := cl.(cshAuth.CSHClaims)

closedPolls, err := database.GetClosedVotedPolls(c, claims.UserInfo.Username)
if err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}
ownedPolls, err := database.GetClosedOwnedPolls(c, claims.UserInfo.Username)
if err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}
closedPolls = append(closedPolls, ownedPolls...)

sort.Slice(closedPolls, func(i, j int) bool {
return closedPolls[i].Id > closedPolls[j].Id
})
closedPolls = uniquePolls(closedPolls)

c.HTML(200, "closed.tmpl", gin.H{
"ClosedPolls": closedPolls,
"Username": claims.UserInfo.Username,
"FullName": claims.UserInfo.FullName,
Expand Down Expand Up @@ -430,6 +460,7 @@ func main() {
"IsOpen": poll.Open,
"IsHidden": poll.Hidden,
"CanModify": canModify,
"CanVote": canVote(claims.UserInfo, *poll, poll.AllowedUsers),
"Username": claims.UserInfo.Username,
"FullName": claims.UserInfo.FullName,
"Gatekeep": poll.Gatekeep,
Expand Down
51 changes: 51 additions & 0 deletions templates/closed.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>CSH Vote</title>
<!-- <link rel="stylesheet" href="https://themeswitcher.csh.rit.edu/api/get" /> -->
<link
rel="stylesheet"
href="https://assets.csh.rit.edu/csh-material-bootstrap/4.3.1/dist/csh-material-bootstrap.min.css"
media="screen"
/>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.13.1/font/bootstrap-icons.min.css">

<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<style>
ul {
list-style: none;
}
</style>
</head>
<body>

{{ template "nav" . }}
<div class="container main p-5">
<h2>
<div class="d-inline">Closed Polls</div>
</h2>
<br />
<div>
<ul class="list-group">
{{ range $i, $poll := .ClosedPolls }}
<li>
<a
class="list-group-item list-group-item-action"
href="/results/{{ $poll.Id }}"
>
<span style="font-size: 1.1rem">
{{ $poll.ShortDescription }}
</span>

<span
><i>(created by {{ $poll.CreatedBy }})</i></span
>
</a>
</li>
{{ end }}
</ul>
</div>
</div>
</body>
</html>
17 changes: 5 additions & 12 deletions templates/create.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,14 @@
<title>CSH Vote</title>
<!-- <link rel="stylesheet" href="https://themeswitcher.csh.rit.edu/api/get" /> -->
<link rel="stylesheet" href="https://assets.csh.rit.edu/csh-material-bootstrap/4.3.1/dist/csh-material-bootstrap.min.css" media="screen">

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.13.1/font/bootstrap-icons.min.css">

<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-primary">
<div class="container">
<a class="navbar-brand" href="/">Vote</a>
<div class="nav navbar-nav ml-auto">
<div class="navbar-user">
<img alt="User profile photo" src="https://profiles.csh.rit.edu/image/{{ .Username }}" />
<span class="text-light">{{ .FullName }}</span>
<a href="/auth/logout" style="color: #c3c3c3;"><i>(logout)</i></a>
</div>
</div>
</div>
</nav>
{{ template "nav" . }}

<div class="container main p-5">
<h2>Create Poll</h2>
<form action="/create" method="POST">
Expand Down
18 changes: 5 additions & 13 deletions templates/hidden.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
href="https://assets.csh.rit.edu/csh-material-bootstrap/4.3.1/dist/csh-material-bootstrap.min.css"
media="screen"
/>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.13.1/font/bootstrap-icons.min.css">

<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<style>
#lockdown {
Expand All @@ -20,19 +23,8 @@
</style>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-primary">
<div class="container">
<a class="navbar-brand" href="/">Vote</a>
<div class="nav navbar-nav ml-auto">
<div class="navbar-user">
<img src="https://profiles.csh.rit.edu/image/{{ .Username }}" />
<span class="text-light">{{ .FullName }}</span>
<a href="/auth/logout" style="color: #c3c3c3"><i>(logout)</i></a>
</div>
</div>
</div>
</nav>

{{ template "nav" . }}

<div
style="text-align: center; font-size: 1.2rem"
class="main p-5 error-page align-center"
Expand Down
53 changes: 9 additions & 44 deletions templates/index.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
href="https://assets.csh.rit.edu/csh-material-bootstrap/4.3.1/dist/csh-material-bootstrap.min.css"
media="screen"
/>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.13.1/font/bootstrap-icons.min.css">

<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<style>
ul {
Expand All @@ -16,19 +19,8 @@
</style>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-primary">
<div class="container">
<a class="navbar-brand" href="/">Vote</a>
<div class="nav navbar-nav ml-auto">
<div class="navbar-user">
<img src="https://profiles.csh.rit.edu/image/{{ .Username }}" />
<span class="text-light">{{ .FullName }}</span>
<a href="/auth/logout" style="color: #c3c3c3;"><i>(logout)</i></a>
</div>
</div>
</div>
</nav>

{{ template "nav" . }}

<div class="container main p-5">
<h2>
<div class="d-inline">Active Polls</div>
Expand All @@ -47,43 +39,16 @@
class="list-group-item list-group-item-action"
href="/poll/{{ $poll.Id }}"
>
<span style="font-size: 1.1rem; white-space: normal; word-wrap: break-word;">{{
$poll.ShortDescription
}}</span>

<span
><i>(created by {{ $poll.CreatedBy }})</i></span
>
</a>
</li>
{{
end
}}
</ul>
</div>
<br />
<h3>Closed Polls</h3>
<br />
<div>
<ul class="list-group">
{{ range $i, $poll := .ClosedPolls }}
<li>
<a
class="list-group-item list-group-item-action"
href="/results/{{ $poll.Id }}"
>
<span style="font-size: 1.1rem">{{
$poll.ShortDescription
}}</span>
<span style="font-size: 1.1rem; white-space: normal; word-wrap: break-word;">
{{ $poll.ShortDescription }}
</span>

<span
><i>(created by {{ $poll.CreatedBy }})</i></span
>
</a>
</li>
{{
end
}}
{{ end }}
</ul>
</div>
</div>
Expand Down
36 changes: 36 additions & 0 deletions templates/nav.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{{ define "nav" }}
<nav class="navbar navbar-expand-lg navbar-dark bg-primary">
<div class="container">
<a class="navbar-brand" href="/">Vote</a>

<button class="navbar-toggler collapsed" type="button" data-toggle="collapse" data-target="#navbar"
aria-controls="navbar" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div id="navbar" class="collapse navbar-collapse">
<a class="nav-item nav-link text-light" href="/"><i class="bi bi-exclamation-circle-fill"></i> Open Polls</a>
<a class="nav-item nav-link text-light" href="/closed"><i class="bi bi-archive-fill"></i> Closed Polls</a>
<a class="nav-item nav-link text-light" href="/create"><i class="bi bi-plus-circle-fill"></i> Create Poll</a>
<div class="nav navbar-nav ml-auto">
<div class="navbar-user">
<img src="https://profiles.csh.rit.edu/image/{{ .Username }}" />
<span class="text-light">{{ .FullName }}</span>
<a href="/auth/logout" style="color: #c3c3c3;"><i>(logout)</i></a>
</div>
</div>
</div>
</div>

<script>
// Toggle the navbar when the hamburger menu is clicked
document.addEventListener('DOMContentLoaded', () => {
let toggler = document.querySelector('.navbar-toggler');
let navbar = document.querySelector('#navbar');
toggler.addEventListener('click', () => {
navbar.classList.toggle('show');
});
});
</script>
</nav>

{{ end }}
16 changes: 4 additions & 12 deletions templates/poll.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
<title>CSH Vote</title>
<!-- <link rel="stylesheet" href="https://themeswitcher.csh.rit.edu/api/get" /> -->
<link rel="stylesheet" href="https://assets.csh.rit.edu/csh-material-bootstrap/4.3.1/dist/csh-material-bootstrap.min.css" media="screen"/>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.13.1/font/bootstrap-icons.min.css">

<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
input[type='number']{
Expand All @@ -12,18 +15,7 @@
</style>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-primary">
<div class="container">
<a class="navbar-brand" href="/">Vote</a>
<div class="nav navbar-nav ml-auto">
<div class="navbar-user">
<img src="https://profiles.csh.rit.edu/image/{{ .Username }}" />
<span class="text-light">{{ .FullName }}</span>
<a href="/auth/logout" style="color: #c3c3c3;"><i>(logout)</i></a>
</div>
</div>
</div>
</nav>
{{ template "nav" . }}

<div class="container main p-5">
<h2 style="white-space: normal; word-wrap: break-word;">{{ .ShortDescription }}</h2>
Expand Down
Loading