Skip to content

Commit

Permalink
admin view
Browse files Browse the repository at this point in the history
  • Loading branch information
sethetter committed Feb 23, 2025
1 parent ba90728 commit 63d5661
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 8 deletions.
18 changes: 10 additions & 8 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ import (
)

type Config struct {
URL string `envconfig:"APP_URL" required:"true" default:"http://localhost:8080"`
Port string `envconfig:"PORT" required:"true" default:":8080"`
Env string `envconfig:"APP_ENV" required:"true" default:"debug"`
AppSecret string `envconfig:"APP_SECRET" required:"true"`
DatabaseURL string `envconfig:"DATABASE_URL" required:"true"`
Email *EmailConfig
Twitter *TwitterConfig
SlackHook string `envconfig:"SLACK_HOOK"`
URL string `envconfig:"APP_URL" required:"true" default:"http://localhost:8080"`
Port string `envconfig:"PORT" required:"true" default:":8080"`
Env string `envconfig:"APP_ENV" required:"true" default:"debug"`
AppSecret string `envconfig:"APP_SECRET" required:"true"`
DatabaseURL string `envconfig:"DATABASE_URL" required:"true"`
Email *EmailConfig
Twitter *TwitterConfig
SlackHook string `envconfig:"SLACK_HOOK"`
AdminUser string `envconfig:"ADMIN_USER" required:"true"`
AdminPassword string `envconfig:"ADMIN_PASSWORD" require:"true"`
}

type EmailConfig struct {
Expand Down
13 changes: 13 additions & 0 deletions pkg/server/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,19 @@ func (ctrl *Controller) ViewJob(ctx *gin.Context) {
ctx.HTML(200, "view", gin.H{"job": job, "description": template.HTML(description)})
}

func (ctrl *Controller) AdminIndex(ctx *gin.Context) {
jobs, err := data.GetAllJobs(ctrl.DB)
if err != nil {
log.Println(fmt.Errorf("failed to get jobs: %w", err))
ctx.AbortWithStatus(http.StatusInternalServerError)
return
}

ctx.HTML(http.StatusOK, "admin", gin.H{
"jobs": jobs,
})
}

func (ctrl *Controller) DeleteJob(ctx *gin.Context) {
id := ctx.Param("id")
err := data.DeleteJob(id, ctrl.DB)
Expand Down
13 changes: 13 additions & 0 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,18 @@ func NewServer(c *ServerConfig) (http.Server, error) {
authorized.POST("/jobs/:id/delete", ctrl.DeleteJob)
}

// Admin routes
admin := router.Group("/admin")
admin.Use(gin.BasicAuth(gin.Accounts{
c.Config.AdminUser: c.Config.AdminPassword,
}))
{
admin.GET("", ctrl.AdminIndex)
admin.GET("/jobs/:id/edit", ctrl.EditJob)
admin.POST("/jobs/:id", ctrl.UpdateJob)
admin.POST("/jobs/:id/delete", ctrl.DeleteJob)
}

return http.Server{
Addr: c.Config.Port,
Handler: router,
Expand All @@ -100,6 +112,7 @@ func renderer(templatePath string) multitemplate.Renderer {
r.AddFromFilesFuncs("new", funcMap, basePath, path.Join(templatePath, "new.html"))
r.AddFromFilesFuncs("edit", funcMap, basePath, path.Join(templatePath, "edit.html"))
r.AddFromFilesFuncs("view", funcMap, basePath, path.Join(templatePath, "view.html"))
r.AddFromFilesFuncs("admin", funcMap, basePath, path.Join(templatePath, "admin.html"))

return r
}
Expand Down
38 changes: 38 additions & 0 deletions templates/admin.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{{ define "content" }}
<div class="container mx-auto px-4 py-8">
<h1 class="text-3xl font-bold mb-6">Admin Dashboard</h1>

<div class="overflow-x-auto">
<table class="max-w-2xl bg-white border border-gray-300">
<thead>
<tr>
<th class="px-6 py-3 border-b text-left">Job</th>
<th class="px-6 py-3 border-b text-left">Email</th>
<th class="px-6 py-3 border-b text-left w-24">Actions</th>
</tr>
</thead>
<tbody>
{{ range .jobs }}
<tr class="hover:bg-gray-50">

<td class="px-6 py-4 border-b">
<a href="/admin/jobs/{{ .ID }}/edit" class="hover:underline">
{{ .Position }} at {{ .Organization }}
</a>
</td>
<td class="px-6 py-4 border-b">{{ .Email }}</td>
<td class="px-6 py-4 border-b">
<form method="POST"
action="/admin/jobs/{{ .ID }}/delete"
class="inline"
onsubmit="return confirm('Are you sure you want to delete this job posting?')">
<button type="submit" class="btn btn-sm btn-danger">Delete</button>
</form>
</td>
</tr>
{{ end }}
</tbody>
</table>
</div>
</div>
{{ end }}

0 comments on commit 63d5661

Please sign in to comment.