-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
128 lines (115 loc) · 3.5 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
package main
import (
"encoding/json"
"log"
"math/rand"
"net/http"
"os"
"strconv"
"time"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
)
type Elements struct {
Name string `json:"name"`
Appearance string `json:"appearance"`
AtomicMass float64 `json:"atomic_mass"`
Boil float64 `json:"boil"`
Category string `json:"category"`
Color interface{} `json:"color"`
Density float64 `json:"density"`
DiscoveredBy string `json:"discovered_by"`
Melt float64 `json:"melt"`
MolarHeat float64 `json:"molar_heat"`
NamedBy string `json:"named_by"`
Number int `json:"number"`
Period int `json:"period"`
Phase string `json:"phase"`
Source string `json:"source"`
SpectralImg string `json:"spectral_img"`
Summary string `json:"summary"`
Symbol string `json:"symbol"`
Xpos int `json:"xpos"`
Ypos int `json:"ypos"`
Shells []int `json:"shells"`
ElectronConfiguration string `json:"electron_configuration"`
ElectronConfigurationSemantic string `json:"electron_configuration_semantic"`
ElectronAffinity float64 `json:"electron_affinity"`
ElectronegativityPauling float64 `json:"electronegativity_pauling"`
IonizationEnergies []float64 `json:"ionization_energies"`
CpkHex string `json:"cpk-hex"`
}
var element []Elements
// queryParam function
// all element route
func allElement(c *gin.Context) {
c.JSON(http.StatusOK, element)
}
// element by name
func elementByName(c *gin.Context) {
for _, item := range element {
if item.Name == c.Param("name") {
c.JSON(http.StatusOK, item)
}
}
}
// element by number
func elementByNumber(c *gin.Context) {
for _, item := range element {
if strconv.Itoa(item.Number) == c.Param("number") {
c.JSON(http.StatusOK, item)
}
}
}
// element by symbol
func elementBySymbol(c *gin.Context) {
for _, item := range element {
if item.Symbol == c.Param("symbol") {
c.JSON(http.StatusOK, item)
}
}
}
// element by phase
func elementByPhase(c *gin.Context) {
for _, item := range element {
if item.Phase == c.Param("phase") {
c.JSON(http.StatusOK, item)
}
}
}
// random element
func randomElement(c *gin.Context) {
min := 1
max := 119
c.JSON(http.StatusOK, element[rand.Intn(max-min)+min])
}
func main() {
er := godotenv.Load()
if er != nil {
log.Fatal("Error loading .env file")
}
file, err := os.ReadFile("element.json")
if err != nil {
panic("data not found!")
}
err = json.Unmarshal(file, &element)
if err != nil {
panic("data not found!")
}
r := gin.Default()
r.Use(cors.New(
cors.Config{
AllowOrigins: []string{"*"},
AllowMethods: []string{"GET", "POST"},
AllowCredentials: true,
MaxAge: 12 * time.Hour,
}))
r.GET("/", allElement)
r.GET("/name/:name", elementByName)
r.GET("/number/:number", elementByNumber)
r.GET("/symbol/:symbol", elementBySymbol)
r.GET("/phase/:phase", elementByPhase)
r.GET("/random", randomElement)
r.Run()
}