This repository was archived by the owner on Sep 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
79 lines (65 loc) · 1.87 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"github.com/gin-gonic/gin"
"wirepact.ch/basic-auth-api/config"
)
func main() {
port := config.Port
fmt.Printf("Starting API server on port %d.\n", port)
router := gin.Default()
secure := router.Group("/", gin.BasicAuth(gin.Accounts{
config.Username: config.Password,
}))
secure.GET("swapi/people", getPeopleFromSwapi)
router.OPTIONS("/swapi/people", cors)
err := router.Run(fmt.Sprintf(":%v", port))
if err != nil {
panic("Could not start server.")
}
}
const swapiUrl = "https://swapi.dev/api/people"
func cors(context *gin.Context) {
context.Writer.Header().Set("Access-Control-Allow-Origin", "*")
context.Writer.Header().Set("Access-Control-Allow-Methods", "*")
context.Writer.Header().Set("Access-Control-Allow-Headers", "*")
context.Writer.Header().Set("Access-Control-Max-Age", "3600")
context.Status(http.StatusNoContent)
}
func getPeopleFromSwapi(context *gin.Context) {
context.Writer.Header().Set("Access-Control-Allow-Origin", "*")
request, _ := http.NewRequest("GET", swapiUrl, nil)
client := &http.Client{}
response, err := client.Do(request)
if err != nil {
_ = context.AbortWithError(response.StatusCode, err)
return
} else if response.StatusCode > 299 {
context.AbortWithStatus(response.StatusCode)
return
}
body, err := ioutil.ReadAll(response.Body)
if err != nil {
_ = context.AbortWithError(response.StatusCode, err)
return
}
var result Result
if err = json.Unmarshal(body, &result); err != nil {
_ = context.AbortWithError(response.StatusCode, err)
return
}
context.JSON(http.StatusOK, result)
}
type Result struct {
Results []Person `json:"results"`
}
type Person struct {
Name string `json:"name"`
Gender string `json:"gender"`
HairColor string `json:"hair_color"`
EyeColor string `json:"eye_color"`
BirthYear string `json:"birth_year"`
}