Skip to content

Commit 12aef83

Browse files
authored
Miscellanous qol changes (#78)
* Explicitly connect to database from main instead of from database.go * Added godotenv to automatically load .env if it exists * Small changes suggested by go compiler * Fixed incorrect method names in logs * Replaced context.TODO() with context.Background() * Updated gitignore to ignore .vscode * Revert "Small changes suggested by go compiler" This reverts commit 1dd301c. * Change suggested by go compiler
1 parent ccd3830 commit 12aef83

File tree

7 files changed

+21
-12
lines changed

7 files changed

+21
-12
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.env*
22
vote
3-
.idea/
3+
.idea/
4+
.vscode/

constitutional.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ func InitConstitution() {
4949
startOfDay := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, time.Local)
5050
nextMidnight := startOfDay.AddDate(0, 0, 1)
5151
fmt.Println(nextMidnight)
52-
fmt.Println(nextMidnight.Sub(time.Now()))
53-
ticker := time.NewTicker(nextMidnight.Sub(time.Now()))
52+
fmt.Println(time.Until(nextMidnight))
53+
ticker := time.NewTicker(time.Until(nextMidnight))
5454
first := true
5555
go func() {
5656
for {

database/database.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const (
2121
Updated UpsertResult = 1
2222
)
2323

24-
var Client *mongo.Client = Connect()
24+
var Client *mongo.Client
2525
var db = ""
2626

2727
func Connect() *mongo.Client {
@@ -33,7 +33,7 @@ func Connect() *mongo.Client {
3333

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

36-
ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second)
36+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
3737
defer cancel()
3838

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

5656
func Disconnect() {
57-
ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second)
57+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
5858
defer cancel()
5959

6060
if err := Client.Disconnect(ctx); err != nil {

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.24.1
55
require (
66
github.com/computersciencehouse/csh-auth v0.1.0
77
github.com/gin-gonic/gin v1.11.0
8+
github.com/joho/godotenv v1.5.1
89
github.com/sirupsen/logrus v1.9.3
910
github.com/slack-go/slack v0.17.3
1011
github.com/stretchr/testify v1.11.1

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX
4242
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
4343
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
4444
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
45+
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
46+
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
4547
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
4648
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
4749
github.com/klauspost/compress v1.18.1 h1:bcSGx7UbpBqMChDtsF28Lw6v/G94LPrrbMbdC3JH2co=

main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/computersciencehouse/vote/logging"
2020
"github.com/computersciencehouse/vote/sse"
2121
"github.com/gin-gonic/gin"
22+
"github.com/joho/godotenv"
2223
"github.com/sirupsen/logrus"
2324
"go.mongodb.org/mongo-driver/bson/primitive"
2425
"mvdan.cc/xurls/v2"
@@ -57,6 +58,8 @@ func MakeLinks(s string) template.HTML {
5758
var oidcClient = OIDCClient{}
5859

5960
func main() {
61+
godotenv.Load()
62+
database.Client = database.Connect()
6063
r := gin.Default()
6164
r.StaticFS("/static", http.Dir("static"))
6265
r.SetFuncMap(template.FuncMap{

users.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,22 +64,22 @@ func (client *OIDCClient) getAccessToken() int {
6464
authData.Set("grant_type", "client_credentials")
6565
resp, err := htclient.PostForm(cshAuth.ProviderURI+"/protocol/openid-connect/token", authData)
6666
if err != nil {
67-
logging.Logger.WithFields(logrus.Fields{"method": "setupOidcClient"}).Error(err)
67+
logging.Logger.WithFields(logrus.Fields{"method": "getAccessToken"}).Error(err)
6868
return 0
6969
}
7070
defer resp.Body.Close()
7171
if resp.StatusCode != http.StatusOK {
72-
logging.Logger.WithFields(logrus.Fields{"method": "setupOidcClient", "statusCode": resp.StatusCode}).Error(resp.Status)
72+
logging.Logger.WithFields(logrus.Fields{"method": "getAccessToken", "statusCode": resp.StatusCode}).Error(resp.Status)
7373
return 0
7474
}
7575
respData := make(map[string]interface{})
7676
err = json.NewDecoder(resp.Body).Decode(&respData)
7777
if err != nil {
78-
logging.Logger.WithFields(logrus.Fields{"method": "setupOidcClient"}).Error(err)
78+
logging.Logger.WithFields(logrus.Fields{"method": "getAccessToken"}).Error(err)
7979
return 0
8080
}
8181
if respData["error"] != nil {
82-
logging.Logger.WithFields(logrus.Fields{"method": "setupOidcClient"}).Error(respData)
82+
logging.Logger.WithFields(logrus.Fields{"method": "getAccessToken"}).Error(respData)
8383
return 0
8484
}
8585
client.accessToken = respData["access_token"].(string)
@@ -91,19 +91,20 @@ func (client *OIDCClient) GetActiveUsers() []OIDCUser {
9191
//active
9292
req, err := http.NewRequest("GET", client.providerBase+"/auth/admin/realms/csh/groups/a97a191e-5668-43f5-bc0c-6eefc2b958a7/members", nil)
9393
if err != nil {
94+
logging.Logger.WithFields(logrus.Fields{"method": "GetActiveUsers"}).Error(err)
9495
return nil
9596
}
9697
req.Header.Add("Authorization", "Bearer "+client.accessToken)
9798
resp, err := htclient.Do(req)
9899
if err != nil {
99-
logging.Logger.WithFields(logrus.Fields{"method": "GetAllUsers"}).Error(err)
100+
logging.Logger.WithFields(logrus.Fields{"method": "GetActiveUsers"}).Error(err)
100101
return nil
101102
}
102103
defer resp.Body.Close()
103104
ret := make([]OIDCUser, 0)
104105
err = json.NewDecoder(resp.Body).Decode(&ret)
105106
if err != nil {
106-
logging.Logger.WithFields(logrus.Fields{"method": "GetAllUsers"}).Error(err)
107+
logging.Logger.WithFields(logrus.Fields{"method": "GetActiveUsers"}).Error(err)
107108
return nil
108109
}
109110
return ret
@@ -118,6 +119,7 @@ func (client *OIDCClient) GetUserInfo(user *OIDCUser) {
118119
req, err := http.NewRequest("GET", client.providerBase+"/auth/admin/realms/csh/users/"+user.Uuid+arg, nil)
119120
// also "users/{user-id}/groups"
120121
if err != nil {
122+
logging.Logger.WithFields(logrus.Fields{"method": "GetUserInfo"}).Error(err)
121123
return
122124
}
123125
req.Header.Add("Authorization", "Bearer "+client.accessToken)

0 commit comments

Comments
 (0)