Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
mj-sakellaropoulos committed Mar 15, 2024
2 parents 69f776e + 6891f58 commit 95d2df5
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 139 deletions.
11 changes: 9 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
FROM golang:1.22 AS build

WORKDIR /go/src/app

COPY go.mod go.sum ./
RUN go mod download

COPY . .

ENV CGO_ENABLED=0 GOOS=linux GOPROXY=direct
RUN go install
RUN go build -v -o app .

FROM alpine:3.19
COPY --from=build /go/src/app/app /go/bin/app
COPY --from=build /go/src/app/templates /go/bin/templates
COPY --from=build /go/src/app/docs /go/bin/docs

WORKDIR /go/bin
RUN mkdir -p /go/bin/tmp

RUN mkdir -p /go/bin/tmp && chown -R 10001:10001 /go/bin/tmp

ENTRYPOINT ["/go/bin/app"]
12 changes: 6 additions & 6 deletions k8s/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ spec:
labels:
app: hackqc2024
annotations:
vault.hashicorp.com/tls-skip-verify: 'true'
vault.hashicorp.com/tls-skip-verify: "true"
vault.hashicorp.com/agent-inject: "true"
vault.hashicorp.com/role: "secret-reader"
vault.hashicorp.com/agent-inject-template-hackqc2024: |
{{- with secret "kv/data/hackqc2024/default/secrets" }}
export API_KEY="{{ .Data.api_key }}"
export JEU_DE_DONNEES="{{ .Data.jeu_de_donnees }}"
{{- with secret "kv/hackqc2024/default/secrets" -}}
export API_KEY="{{ .Data.data.api_key }}"
export JEU_DE_DONNEES="{{ .Data.data.jeu_de_donnees }}"
{{- end }}
spec:
Expand All @@ -43,7 +43,7 @@ spec:
ephemeral-storage: 10Gi
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
readOnlyRootFilesystem: false
runAsNonRoot: true
runAsUser: 10001
runAsGroup: 10001
Expand All @@ -60,4 +60,4 @@ spec:
persistentVolumeClaim:
claimName: clover-db
securityContext:
fsGroup: 10001
fsGroup: 10001
2 changes: 1 addition & 1 deletion k8s/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ kind: Kustomization
namespace: hackqc2024
images:
- name: ghcr.io/clubcedille/hackqc2024
newTag: commit-20e512d486a294e0aa50d5336b7210d79a1df992
newTag: commit-8842f4e48ee2869034ce95ba17f583f45a98a76a

resources:
- namespace.yaml
Expand Down
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,16 @@ func main() {
},
})

authorized := r.Group("/")

store := cookie.NewStore([]byte(GIN_SESSION_SECRET))
store.Options(sessions.Options{MaxAge: 60 * 60 * 24}) // expire in a day

r.Use(sessions.Sessions(GIN_SESSION_NAME, store))

r.Use(LoginMiddleware())

r.LoadHTMLGlob("templates/**/*.html")

registerRoutes(r, db)
authRegisterRoutes(r, authorized, db)

err = r.Run()
if err != nil {
Expand Down
18 changes: 13 additions & 5 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,24 @@ import (
"github.com/gin-gonic/gin"
)

// Check if user is logged in
// Check if user is logged in to enable a group of features
func AuthRequiredMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
session.GetActiveSession(c)
log.Printf("(middleware) accountid value: %s", session.ActiveSession.AccountId)
if session.ActiveSession.AccountId == "" {
redirectURL := c.Request.URL.Path
c.SetCookie("redirect_url", redirectURL, 3600, "/", "", false, true)
c.Redirect(http.StatusSeeOther, "/login")
c.Abort()
return
c.SetCookie("redirect_url", redirectURL, 3600, "/", "", false, true)
c.Redirect(http.StatusSeeOther, "/login")
c.Abort()
return
}
}
}

func LoginMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
log.Println("(middleware) getting active session")
session.GetActiveSession(c)
}
}
117 changes: 56 additions & 61 deletions routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,79 +4,18 @@ import (
"net/http"

"github.com/ClubCedille/hackqc2024/pkg/pages"
"github.com/ClubCedille/hackqc2024/pkg/session"
"github.com/gin-gonic/gin"
"github.com/ostafen/clover/v2"
)

func authRegisterRoutes(r *gin.Engine, group *gin.RouterGroup, db *clover.DB) {
group.Use(AuthRequiredMiddleware())
{
// Help
group.POST("/create-help", func(c *gin.Context) {
pages.CreateHelp(c, db)
})

// Event
group.POST("/create-event", func(c *gin.Context) {
pages.CreateEvent(c, db)
})

// Manage posts
group.GET("/manage-post", func(c *gin.Context) {
pages.GetManagedPost(c, db)
})

group.GET("/delete-event/:id", func(c *gin.Context) {
pages.GetEventDetailsAboutToBeDelete(c, db)
})

group.DELETE("/event/delete/:id", func(c *gin.Context) {
pages.DeleteEvent(c, db)
})

group.GET("/delete-help/:id", func(c *gin.Context) {
pages.GetHelpDetailsAboutToBeDelete(c, db)
})

group.DELETE("/help/delete/:id", func(c *gin.Context) {
pages.DeleteHelp(c, db)
})

group.GET("/update-event/:id", func(c *gin.Context) {
pages.GetEventDetailAboutToBeModified(c, db)
})

group.POST("/event/update/:id", func(c *gin.Context) {
pages.UpdateEvent(c, db)
})

group.GET("/update-help/:id", func(c *gin.Context) {
pages.GetHelpDetailAboutToBeModified(c, db)
})

group.POST("/help/update/:id", func(c *gin.Context) {
pages.UpdateHelp(c, db)
})
group.POST("/help/comment", func(c *gin.Context) {
pages.PostCreateHelpComment(c, db)
})
group.POST("/event/comment", func(c *gin.Context) {
pages.PostCreateEventComment(c, db)
})
}
}

func registerRoutes(r *gin.Engine, db *clover.DB) {
r.Static("/static", "./templates/static")

r.GET("/", func(c *gin.Context) {
session.GetActiveSession(c)
c.Redirect(http.StatusSeeOther, "/map")
})

r.GET("/map", func(c *gin.Context) {
session.GetActiveSession(c)
pages.MapPage(c, db)
})

Expand Down Expand Up @@ -155,4 +94,60 @@ func registerRoutes(r *gin.Engine, db *clover.DB) {
r.GET("/help/:id", func(c *gin.Context) {
pages.HelpDetails(c, db)
})

// The requests below require the user to be authenticated
// Help
r.POST("/create-help", AuthRequiredMiddleware(), func(c *gin.Context) {
pages.CreateHelp(c, db)
})

// Event
r.POST("/create-event", AuthRequiredMiddleware(), func(c *gin.Context) {
pages.CreateEvent(c, db)
})

// Manage posts
r.GET("/manage-post", AuthRequiredMiddleware(), func(c *gin.Context) {
pages.GetManagedPost(c, db)
})

r.GET("/delete-event/:id", AuthRequiredMiddleware(), func(c *gin.Context) {
pages.GetEventDetailsAboutToBeDelete(c, db)
})

r.DELETE("/event/delete/:id", AuthRequiredMiddleware(), func(c *gin.Context) {
pages.DeleteEvent(c, db)
})

r.GET("/delete-help/:id", AuthRequiredMiddleware(), func(c *gin.Context) {
pages.GetHelpDetailsAboutToBeDelete(c, db)
})

r.DELETE("/help/delete/:id", AuthRequiredMiddleware(), func(c *gin.Context) {
pages.DeleteHelp(c, db)
})

r.GET("/update-event/:id", AuthRequiredMiddleware(), func(c *gin.Context) {
pages.GetEventDetailAboutToBeModified(c, db)
})

r.POST("/event/update/:id", AuthRequiredMiddleware(), func(c *gin.Context) {
pages.UpdateEvent(c, db)
})

r.GET("/update-help/:id", AuthRequiredMiddleware(), func(c *gin.Context) {
pages.GetHelpDetailAboutToBeModified(c, db)
})

r.POST("/help/update/:id", AuthRequiredMiddleware(), func(c *gin.Context) {
pages.UpdateHelp(c, db)
})

r.POST("/help/comment", AuthRequiredMiddleware(), func(c *gin.Context) {
pages.PostCreateHelpComment(c, db)
})

r.POST("/event/comment", AuthRequiredMiddleware(), func(c *gin.Context) {
pages.PostCreateEventComment(c, db)
})
}
17 changes: 9 additions & 8 deletions templates/forms/helpForm.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,22 @@
<textarea class="w3-input" name="how_to_use_help" required></textarea>
</div>

<div class="w3-margin-bottom">
<label>D'autres personnes peuvent-elles se joindrent à l'aide ?</label>
<div x-data="{helpPossible: false}" class="w3-margin-bottom">
<label>D'autres personnes peuvent-elles se joindre à l'aide ?</label>
<div class="w3-padding w3-cell">
<input id="helpPossible" class="w3-radio" type="radio" name="need_help" value="true" required>
<input class="w3-radio" type="radio" name="need_help" value="true" x-model="helpPossible"
required>
<label style="display: inline;">Oui</label>
</div>
<div class="w3-cell">
<input class="w3-radio" type="radio" name="need_help" value="false">
<input class="w3-radio" type="radio" name="need_help" value="false" x-model="helpPossible">
<label style="display: inline;">Non</label>
</div>
</div>

<div class="w3-margin-bottom">
<label>Comment aider</label>
<textarea class="w3-input" name="how_to_help" required></textarea>
<div class="w3-margin-bottom" x-bind:class="helpPossible=='true' ? '' : 'w3-hide'">
<label>Comment aider</label>
<textarea class="w3-input" name="how_to_help"></textarea>
</div>
</div>
</fieldset>
<fieldset>
Expand Down
Loading

0 comments on commit 95d2df5

Please sign in to comment.