This repository has been archived by the owner on Jul 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 311
/
handle_mirrors.go
146 lines (128 loc) · 3.9 KB
/
handle_mirrors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package admin
import (
"fmt"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"github.com/google/exposure-notifications-server/internal/mirror/database"
"github.com/google/exposure-notifications-server/internal/mirror/model"
)
// HandleMirrorsSave handles the create/update actions for mirrors.
func (s *Server) HandleMirrorsSave() func(c *gin.Context) {
return func(c *gin.Context) {
var form mirrorFormData
if err := c.Bind(&form); err != nil {
ErrorPage(c, err.Error())
return
}
ctx := c.Request.Context()
m := TemplateMap{}
db := database.New(s.env.Database())
mirror := &model.Mirror{}
if idParam := c.Param("id"); idParam != "0" {
cfgID, err := strconv.ParseInt(idParam, 10, 64)
if err != nil {
ErrorPage(c, "unable to parse `id` param.")
return
}
mirror, err = db.GetMirror(ctx, cfgID)
if err != nil {
ErrorPage(c, fmt.Sprintf("Error loading mirror: %v", err))
return
}
}
switch form.Action {
case "delete":
if err := db.DeleteMirror(ctx, mirror); err != nil {
ErrorPage(c, fmt.Sprintf("Failed to delete mirror: %v", err))
return
}
m.AddSuccess(fmt.Sprintf("Deleted mirror %d", mirror.ID))
c.Redirect(http.StatusSeeOther, "/")
case "save":
if err := form.PopulateMirror(mirror); err != nil {
ErrorPage(c, fmt.Sprintf("error processing mirror: %v", err))
return
}
updateFn := db.AddMirror
if mirror.ID != 0 {
updateFn = db.UpdateMirror
}
if err := updateFn(ctx, mirror); err != nil {
ErrorPage(c, fmt.Sprintf("Error writing mirror: %v", err))
return
}
m.AddSuccess(fmt.Sprintf("Updated mirror %d", mirror.ID))
m["mirror"] = mirror
c.HTML(http.StatusOK, "mirror", m)
default:
ErrorPage(c, "Invalid form action")
}
}
}
// HandleMirrorsShow handles the show action for mirrors.
func (s *Server) HandleMirrorsShow() func(c *gin.Context) {
return func(c *gin.Context) {
ctx := c.Request.Context()
m := TemplateMap{}
db := database.New(s.env.Database())
mirror := &model.Mirror{}
if idParam := c.Param("id"); idParam != "0" {
id, err := strconv.ParseInt(idParam, 10, 64)
if err != nil {
ErrorPage(c, "unable to parse `id` param.")
return
}
mirror, err = db.GetMirror(ctx, id)
if err != nil {
ErrorPage(c, fmt.Sprintf("Error loading mirror: %v", err))
return
}
}
var mirrorFiles []*model.MirrorFile
if mirror.ID != 0 {
var err error
mirrorFiles, err = db.ListFiles(ctx, mirror.ID)
if err != nil {
ErrorPage(c, fmt.Sprintf("Error loading mirror files: %v", err))
return
}
}
m["mirror"] = mirror
m["mirrorFiles"] = mirrorFiles
c.HTML(http.StatusOK, "mirror", m)
}
}
type mirrorFormData struct {
Action string `form:"action" binding:"required"`
IndexFile string `form:"index-file" binding:"required"`
ExportRoot string `form:"export-root"`
CloudStorageBucket string `form:"cloud-storage-bucket" binding:"required"`
FilenameRoot string `form:"filename-root"`
FilenameRewrite string `form:"filename-rewrite"`
}
func (f *mirrorFormData) PopulateMirror(m *model.Mirror) error {
m.IndexFile = f.IndexFile
m.ExportRoot = f.ExportRoot
m.CloudStorageBucket = f.CloudStorageBucket
m.FilenameRoot = f.FilenameRoot
if f.FilenameRewrite != "" {
m.FilenameRewrite = &f.FilenameRewrite
} else {
m.FilenameRewrite = nil
}
return nil
}