-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
50 lines (42 loc) · 1.2 KB
/
api.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
package main
import (
"encoding/json"
"net/http"
"regexp"
)
// Service ...
type Service struct {
Name string
Port int
GatewayEndpoint string
ContainerRepository string
ImageTag string
}
// IsValidName checks if a string contains only asci letters , a-zA-Z
var IsValidName = regexp.MustCompile(`^[a-zA-Z]+$`).MatchString
// NewAPIRegistration ...
// @Sumary Register new API
// @Accept json
// @Param data body main.Service true "Service info"
// @Produce json
// @success 201
// @Failure 400
// @Router /api/new [post]
func NewAPIRegistration(responseWriter http.ResponseWriter, request *http.Request) {
var service Service
err := json.NewDecoder(request.Body).Decode(&service)
if err != nil {
http.Error(responseWriter, "Invalid request body", http.StatusBadRequest)
return
}
if !IsValidName(service.GatewayEndpoint) || !IsValidName(service.Name) {
http.Error(responseWriter, "Invalid name/endpoint, must match \"^[a-zA-Z]+$\"", http.StatusBadRequest)
return
}
err = CreateNewServiceConfig(service)
if err != nil {
http.Error(responseWriter, err.Error(), http.StatusConflict)
return
}
responseWriter.Write([]byte(service.Name + " created"))
}