Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
cloud-assignment-02
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Nils Petter Skålerud
cloud-assignment-02
Commits
43ac9c83
Commit
43ac9c83
authored
2 years ago
by
Nils Petter Skålerud
Browse files
Options
Downloads
Patches
Plain Diff
Nearly done with deletion of webhook.
parent
2b3639df
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
cmd/main.go
+2
-2
2 additions, 2 deletions
cmd/main.go
handlers/webhook.go
+40
-6
40 additions, 6 deletions
handlers/webhook.go
utils/structs.go
+15
-5
15 additions, 5 deletions
utils/structs.go
with
57 additions
and
13 deletions
cmd/main.go
+
2
−
2
View file @
43ac9c83
...
...
@@ -13,7 +13,7 @@ import (
func
main
()
{
serverState
:=
utils
.
ServerState
{}
serverState
:=
utils
.
Make
ServerState
()
NewRequest
,
err
:=
http
.
NewRequest
(
http
.
MethodGet
,
utils
.
REST_COUNTRIES_URL
,
nil
)
if
err
!=
nil
{
...
...
@@ -100,7 +100,7 @@ func main() {
http
.
HandleFunc
(
utils
.
NOTIFICATIONS_PATH
,
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
handlers
.
WebhookRegistr
ationHandler
(
&
serverState
,
w
,
r
)
handlers
.
Notific
ationHandler
(
&
serverState
,
w
,
r
)
})
log
.
Println
(
"Starting server on port "
+
port
+
" ..."
)
...
...
This diff is collapsed.
Click to expand it.
handlers/webhook.go
+
40
−
6
View file @
43ac9c83
...
...
@@ -4,19 +4,30 @@ import (
"Assignment02/utils"
"encoding/json"
"net/http"
"strconv"
)
type
WebhookRegistrationSource
struct
{
func
NotificationHandler
(
serverState
*
utils
.
ServerState
,
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
if
r
.
Method
==
http
.
MethodPost
{
// Make sure the URL doesn't have anything extra
webhookRegistrationHandler
(
serverState
,
w
,
r
)
}
else
if
r
.
Method
==
http
.
MethodDelete
{
webhookDeletion
(
serverState
,
w
,
r
)
}
}
type
WebhookRegistrationBody
struct
{
Url
string
`json:"url"`
CountryCode
string
`json:"country"`
Calls
int
`json:"calls"`
}
type
WebhookRegistrationOutput
struct
{
Webhook
_i
d
int
`json:"webhook_id"`
Webhook
I
d
int
`json:"webhook_id"`
}
func
W
ebhookRegistrationHandler
(
serverState
*
utils
.
ServerState
,
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
func
w
ebhookRegistrationHandler
(
serverState
*
utils
.
ServerState
,
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
if
r
.
Method
!=
http
.
MethodPost
{
return
}
...
...
@@ -25,7 +36,7 @@ func WebhookRegistrationHandler(serverState *utils.ServerState, w http.ResponseW
return
}
var
command
WebhookRegistration
Source
var
command
WebhookRegistration
Body
decoder
:=
json
.
NewDecoder
(
r
.
Body
)
err
:=
decoder
.
Decode
(
&
command
)
if
err
!=
nil
{
...
...
@@ -36,11 +47,10 @@ func WebhookRegistrationHandler(serverState *utils.ServerState, w http.ResponseW
Url
:
command
.
Url
,
Event
:
command
.
CountryCode
,
}
registrationId
:=
serverState
.
InsertWebhook
(
registration
)
output
:=
WebhookRegistrationOutput
{
Webhook
_i
d
:
registrationId
,
Webhook
I
d
:
registrationId
,
}
encoder
:=
json
.
NewEncoder
(
w
)
err
=
encoder
.
Encode
(
output
)
...
...
@@ -49,3 +59,27 @@ func WebhookRegistrationHandler(serverState *utils.ServerState, w http.ResponseW
}
}
func
webhookDeletion
(
serverState
*
utils
.
ServerState
,
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
if
r
.
Method
!=
http
.
MethodDelete
{
return
}
// Extract the ID from the path
remainingPath
:=
r
.
URL
.
Path
[
len
(
utils
.
NOTIFICATIONS_PATH
)
:
]
if
remainingPath
==
""
{
return
}
// Interpret it as an int
id
,
err
:=
strconv
.
Atoi
(
remainingPath
)
if
err
!=
nil
{
return
}
deletionSuccess
:=
serverState
.
DeleteWebhook
(
id
)
if
!
deletionSuccess
{
w
.
WriteHeader
(
http
.
StatusBadRequest
)
return
}
}
This diff is collapsed.
Click to expand it.
utils/structs.go
+
15
−
5
View file @
43ac9c83
...
...
@@ -6,8 +6,14 @@ type ServerState struct {
startTime
time
.
Time
_useMocking
bool
webhook_id_tracker
int
webHooks
[]
WebhookRegistration
webhookIdTracker
int
webHooks
map
[
int
]
WebhookRegistration
}
func
MakeServerState
()
ServerState
{
out
:=
ServerState
{}
out
.
webHooks
=
make
(
map
[
int
]
WebhookRegistration
)
return
out
}
func
(
state
ServerState
)
UseMocking
()
bool
{
...
...
@@ -19,12 +25,16 @@ func (state ServerState) UptimeInSeconds() float64 {
}
func
(
state
*
ServerState
)
InsertWebhook
(
registration
WebhookRegistration
)
int
{
state
.
webHooks
=
append
(
state
.
web
H
ook
s
,
registration
)
output
:=
state
.
web
h
ook
_id_tracker
state
.
webhook
_id_t
racker
++
output
:=
state
.
web
h
ook
IdTracker
state
.
web
H
ook
s
[
output
]
=
registration
state
.
webhook
IdT
racker
++
return
output
}
func
(
state
*
ServerState
)
DeleteWebhook
(
id
int
)
bool
{
return
false
}
type
CountryItemName
struct
{
Common
string
`json:"common"`
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment