-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
106 lines (89 loc) · 2.49 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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
"github.com/gin-gonic/gin"
)
type Words struct {
Words []word `json:"words"`
}
type word struct {
Sorangan string `json:"sorangan"`
Batur string `json:"batur"`
Loma string `json:"loma"`
Bindo string `json:"bindo"`
English string `json:"english"`
}
// CORS Middleware
func CORS(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", "*")
c.Header("Access-Control-Allow-Methods", "GET")
c.Header("Content-Type", "application/json")
}
func main() {
// initiate router using gin
router := gin.Default()
// We use our custom CORS Middleware
router.Use(CORS)
router.GET("/undakusukbasa", getAllWords)
router.GET("/undakusukbasa/:substring", getWordsBySubstring)
// run the router on port 8080
router.Run()
}
/**
* Function to get all the words
*/
func getAllWords(c *gin.Context) {
jsonFile, err := os.Open("./undakUsukBasa.json")
// if error happens
if err != nil {
fmt.Println(err)
}
// If no error
// defer the closing of our jsonFile so that we can parse it later on
defer jsonFile.Close()
// Save json content into byte
byteValue, _ := ioutil.ReadAll(jsonFile)
// Initiate interface
var words Words
json.Unmarshal(byteValue, &words)
// return json
c.IndentedJSON(http.StatusOK, words)
}
/**
* Function to get words containing suffix
*/
func getWordsBySubstring(c *gin.Context) {
substring := c.Param("substring")
jsonFile, err := os.Open("./undakUsukBasa.json")
// if error happens
if err != nil {
fmt.Println(err)
}
// If no error
// defer the closing of our jsonFile so that we can parse it later on
defer jsonFile.Close()
// Save json content into byte
byteValue, _ := ioutil.ReadAll(jsonFile)
// Initiate interface
var words Words
json.Unmarshal(byteValue, &words)
foundWords := []interface{}{}
for i := 0; i < len(words.Words); i++ {
soranganWord := strings.ToLower(words.Words[i].Sorangan)
baturWord := strings.ToLower(words.Words[i].Batur)
lomaWord := strings.ToLower(words.Words[i].Loma)
bindoWord := strings.ToLower(words.Words[i].Bindo)
englishWord := strings.ToLower(words.Words[i].English)
substring = strings.ToLower(substring)
if strings.Contains(soranganWord, substring) || strings.Contains(baturWord, substring) || strings.Contains(lomaWord, substring) || strings.Contains(bindoWord, substring) || strings.Contains(englishWord, substring) {
foundWords = append(foundWords, words.Words[i])
}
}
c.IndentedJSON(http.StatusOK, foundWords)
return
}