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
2 changes: 1 addition & 1 deletion Containerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM docker.io/golang:1.25-alpine AS build
FROM docker.io/golang:1.26.2-alpine AS build

WORKDIR /src/
RUN apk add git
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ Counters also has support for Docker Compose, a extended version of docker that

(This is a really cool thing! If you use docker often, check it out!)
```
docker compose up
docker compose up --build
```

## TODO
- [] Add Swagger
- [] Add API Integration
- [] Add Logging (similar to Votes "Actions")
- [] Time Zone support
93 changes: 69 additions & 24 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,31 @@ package main
import (
"net/http"
"sort"
"time"

"github.com/WeatherGod3218/counters/database"
"github.com/gin-gonic/gin"

cshAuth "github.com/computersciencehouse/csh-auth/v2"
"go.mongodb.org/mongo-driver/v2/bson"
)

func translateTime(inputTime string) time.Time {
timeZone, _ := time.LoadLocation("America/New_York")

currentTime := time.Now()
func GetHomePage(c *gin.Context) {
userAny, exists := c.Get("cshauth")

timeConverted, err := time.ParseInLocation("2006-01-02T15:04", inputTime, timeZone)
if err != nil {
timeConverted = currentTime
if !exists {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to load CSH context!"})
return
}

if timeConverted.After(currentTime) {
timeConverted = currentTime
user, ok := userAny.(*cshAuth.Claims)
if !ok {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to load CSH context!"})
return
}
return timeConverted
}

func GetHomePage(c *gin.Context) {
user := GetUserData(c)

counters, err := database.GetAllCounters(c)

if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to load counters!"})
return
}

Expand All @@ -51,7 +45,17 @@ func GetHomePage(c *gin.Context) {
}

func GetCreatePage(c *gin.Context) {
user := GetUserData(c)
userAny, exists := c.Get("cshauth")

if !exists {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to load CSH context!"})
return
}
user, ok := userAny.(*cshAuth.Claims)
if !ok {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to load CSH context!"})
return
}

c.HTML(http.StatusOK, "create.tmpl", gin.H{
"Username": user.Username,
Expand All @@ -62,7 +66,17 @@ func GetCreatePage(c *gin.Context) {
}

func GetResetPage(c *gin.Context) {
user := GetUserData(c)
userAny, exists := c.Get("cshauth")

if !exists {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to load CSH context!"})
return
}
user, ok := userAny.(*cshAuth.Claims)
if !ok {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to load CSH context!"})
return
}

idToUse := c.Param("id")
c.HTML(http.StatusOK, "reset.tmpl", gin.H{
Expand All @@ -75,8 +89,17 @@ func GetResetPage(c *gin.Context) {
}

func LoadCounter(c *gin.Context) {
userAny, exists := c.Get("cshauth")
if !exists {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to load CSH context!"})
return
}
user, ok := userAny.(*cshAuth.Claims)
if !ok {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to load CSH context!"})
return
}
idToUse := c.Param("id")
user := GetUserData(c)

counter, err := database.GetCounterFromId(c, idToUse)
if err != nil {
Expand Down Expand Up @@ -106,8 +129,19 @@ func LoadCounter(c *gin.Context) {
}

func ResetCounter(c *gin.Context) {
resetTime := translateTime(c.PostForm("reset-time"))
user := GetUserData(c)
userAny, exists := c.Get("cshauth")

if !exists {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to load CSH context!"})
return
}
user, ok := userAny.(*cshAuth.Claims)
if !ok {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to load CSH context!"})
return
}

resetTime := TranslateTime(c.PostForm("reset-time"))

reset := &database.Reset{
Id: bson.NewObjectID(),
Expand All @@ -128,8 +162,19 @@ func ResetCounter(c *gin.Context) {
}

func CreateCounter(c *gin.Context) {
resetTime := translateTime(c.PostForm("reset-time"))
user := GetUserData(c)
userAny, exists := c.Get("cshauth")

if !exists {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to load CSH context!"})
return
}
user, ok := userAny.(*cshAuth.Claims)
if !ok {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to load CSH context!"})
return
}

resetTime := TranslateTime(c.PostForm("reset-time"))

reset := &database.Reset{
Id: bson.NewObjectID(),
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/WeatherGod3218/counters
go 1.25.7

require (
github.com/computersciencehouse/csh-auth/v2 v2.0.1
github.com/computersciencehouse/csh-auth/v2 v2.0.2
github.com/gin-gonic/gin v1.12.0
github.com/sirupsen/logrus v1.9.4
go.mongodb.org/mongo-driver/v2 v2.6.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ github.com/computersciencehouse/csh-auth/v2 v2.0.0 h1:uRP8Ti/pb1SaSSBvhk3dLVpHag
github.com/computersciencehouse/csh-auth/v2 v2.0.0/go.mod h1:JelmcPFBLpo8wnmErF4Zoa4UJKi0psgUDpcuTq6pWbY=
github.com/computersciencehouse/csh-auth/v2 v2.0.1 h1:rtGa9cBDZJZZJ8aeGefwAM/rzPMxWNAGDwuVDZI80ro=
github.com/computersciencehouse/csh-auth/v2 v2.0.1/go.mod h1:uL/2UDr9GJlAQORsn+rmFc18YkBsZPymYJbwb+uTRig=
github.com/computersciencehouse/csh-auth/v2 v2.0.2 h1:xlxv2w2cmiWriVNhwA8Io1FWI54sDmv7c12gkVAcK30=
github.com/computersciencehouse/csh-auth/v2 v2.0.2/go.mod h1:uL/2UDr9GJlAQORsn+rmFc18YkBsZPymYJbwb+uTRig=
github.com/coreos/go-oidc v2.5.0+incompatible h1:6W0vGJR3Tu0r0PwfmjOrRZSlfxeEln8dsejt3ZWIvwo=
github.com/coreos/go-oidc v2.5.0+incompatible/go.mod h1:CgnwVTmzoESiwO9qyAFEMiHoZ1nMCKZlZ9V6mm3/LKc=
github.com/coreos/go-oidc/v3 v3.18.0 h1:V9orjXynvu5wiC9SemFTWnG4F45v403aIcjWo0d41+A=
Expand Down
16 changes: 7 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,18 @@ import (

var DEV_FORCE_IS_EBOARD bool = os.Getenv("DEV_FORCE_IS_EBOARD") == "true"

var oidcClient = OIDCClient{}

func main() {
database.Client = database.Connect()

hostUrl := os.Getenv("SERVER_HOST")
auth, err := cshAuth.Init(
os.Getenv("AUTH_OIDC_ID"),
os.Getenv("AUTH_OIDC_SECRET"),
os.Getenv("SERVER_HOST"),
os.Getenv("SERVER_HOST")+"/auth/login",
os.Getenv("SERVER_HOST")+"/auth/callback",
hostUrl,
hostUrl+"/auth/login",
hostUrl+"/auth/callback",
[]string{"profile", "email", "groups"},
)
oidcClient.setupOidcClient(os.Getenv("AUTH_OIDC_ID"), os.Getenv("AUTH_OIDC_SECRET"))

if err != nil {
logging.Logger.WithFields(logrus.Fields{"error": err, "module": "main", "method": "main"}).Fatal("error initializing csh-auth")
Expand All @@ -39,11 +37,11 @@ func main() {
router.StaticFS("/static", http.Dir("static"))
router.LoadHTMLGlob("templates/*")

router.GET("/auth/login", auth.HandleLogin) // This endpoint should match the path for loginURL
router.GET("/auth/callback", auth.HandleCallback) // This endpoint should match the path for callbackURL
router.GET("/auth/login", auth.HandleLogin)
router.GET("/auth/callback", auth.HandleCallback)
router.GET("/auth/logout", auth.HandleLogout)

router.Use(auth.HeaderMiddleware())
router.Use(auth.CookieMiddleware())

router.GET("/", GetHomePage)
router.GET("/counters/:id", LoadCounter)
Expand Down
1 change: 0 additions & 1 deletion templates/header.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item"><a class="nav-link text-light" href="/"><i class="bi bi-cast me-1"></i>View</a></li>
<li class="nav-item"><a class="nav-link text-light" href="/create"><i class="bi bi-plus-circle-fill me-1"></i>Create</a></li>

</ul>

<div class="d-flex flex-column flex-lg-row align-items-lg-center gap-2">
Expand Down
Loading
Loading