-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
137 lines (113 loc) · 3.36 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
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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"strconv"
"time"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/etag"
"github.com/gofiber/fiber/v2/middleware/favicon"
"github.com/gofiber/fiber/v2/middleware/limiter"
)
type Response struct {
Data *Pincode `json:"data"`
OK bool `json:"ok"`
Message string `json:"message"`
}
type Pincode struct {
Pincode int `json:"pincode"`
District string `json:"district"`
Taluk string `json:"taluk"`
State string `json:"state"`
}
func GetPincode() []Pincode {
pincodes := []Pincode{}
content, err := ioutil.ReadFile("./public/pincode.json")
if err != nil {
fmt.Println(err)
return pincodes
}
err = json.Unmarshal(content, &pincodes)
if err != nil {
fmt.Println(err)
return pincodes
}
return pincodes
}
func IndexOf(a []Pincode, x int) int {
for i, n := range a {
if x == n.Pincode {
return i
}
}
return -1
}
var InvalidPincodeMessage = Response{OK: false, Message: "Invalid Pincode"}
var RecordNotFoundMessage = Response{OK: false, Message: "Record Not Found"}
var RateLimiter = limiter.New(limiter.Config{
Next: func(c *fiber.Ctx) bool { return c.IP() == "127.0.0.1" },
Max: 5,
Expiration: 30 * time.Second,
KeyGenerator: func(c *fiber.Ctx) string { return c.Get("x-forwarded-for") },
LimitReached: func(c *fiber.Ctx) error {
c.SendStatus(fiber.StatusTooManyRequests)
return c.JSON(Response{OK: false, Message: "Rate Limit Reached"})
},
})
var FiberConfig = fiber.Config{
GETOnly: true,
CaseSensitive: true,
StrictRouting: true,
ServerHeader: "Fiber",
AppName: "Indian Pincode",
Immutable: true,
}
func PincodeHandler(c *fiber.Ctx) error {
c.Response().Header.EnableNormalizing()
c.Response().Header.SetContentType("application/json")
c.Response().Header.Set("Access-Control-Allow-Origin", "*")
c.Response().Header.Set("referrer-policy", "no-referrer")
c.Response().Header.Set("x-frame-options", "SAMEORIGIN")
c.Response().Header.Set("vary", "origin")
c.Response().Header.Set("x-content-type-options", "nosniff")
c.Response().Header.Set("x-dns-prefetch-control", "off")
c.Response().Header.Set("x-download-options", "noopen")
c.Response().Header.Set("x-permitted-cross-domain-policies", "none")
c.Response().Header.Set("x-xss-protection", "0")
_pincode := c.Params("pincode")
if len(_pincode) != 6 {
return c.JSON(InvalidPincodeMessage)
}
i, err := strconv.Atoi(_pincode)
if err != nil {
fmt.Println(err)
return c.JSON(InvalidPincodeMessage)
}
pincodes := GetPincode()
idx := IndexOf(pincodes, i)
if idx == -1 {
return c.JSON(RecordNotFoundMessage)
}
return c.JSON(Response{Data: &pincodes[idx], OK: true, Message: "Success"})
}
func main() {
port := os.Getenv("PORT")
if port == "" {
log.Fatal("$PORT must be set")
}
app := fiber.New(FiberConfig)
app.Static(`/static`, "./public")
app.Use(etag.New())
app.Use(RateLimiter)
app.Use(favicon.New(favicon.Config{File: "./public/favicon.ico"}))
api := app.Group("/api/v1")
api.Get("/pincode/:pincode", PincodeHandler)
app.Get("**", func(c *fiber.Ctx) error {
c.SendStatus(fiber.StatusNotFound)
return c.JSON(Response{OK: false, Message: "Invalid Request"})
})
app.Listen(":" + port)
}