forked from Raditss/Tubes3_13521019_BE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
157 lines (138 loc) · 3.89 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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"regexp"
"strconv"
"strings"
"github.com/gorilla/mux"
)
func JSONWriter(w http.ResponseWriter, data interface{}) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(data)
}
type APIServer struct {
port string
}
// ErrorResponse defines the structure of error responses
type ErrorResponse struct {
Error string `json:"error"`
}
// ResultResponse defines the structure of successful result responses
type ResultResponse struct {
Result string `json:"result"`
}
func FuncHandler(w http.ResponseWriter, r *http.Request) {
// Parse request body
var requestBody struct {
Expr string `json:"expr"`
Alg string `json:"alg"`
}
if err := json.NewDecoder(r.Body).Decode(&requestBody); err != nil {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(ErrorResponse{err.Error()})
return
}
input := requestBody.Expr
input = input + "?"
parts := strings.Split(input, "?")
Alg := requestBody.Alg
var err error
result := ""
var resultarr = make([]string, len(parts))
if len(parts) > 1 {
for i := 0; i < len(parts)-1; i++ {
input = strings.ToLower(parts[i])
inputType := defineType(input)
var numres float64
fmt.Println(inputType)
if input != "" {
switch inputType {
case "calculator":
numres, err = calculate(input)
resultarr[i] = strconv.FormatFloat(numres, 'f', 2, 64)
fmt.Println(resultarr[i])
case "date":
resultarr[i] = getDayOfWeek(input)
fmt.Println(resultarr[i])
case "textQuestion":
qtype := defineQuestionType(input)
switch {
case qtype == "add":
resultarr[i] = addQuestionToDB(input)
fmt.Println(resultarr[i])
case qtype == "del":
resultarr[i] = delQuestion(input)
fmt.Println(resultarr[i])
case qtype == "question":
if Alg == "KMP" {
resultarr[i] = getAnswerKMP(input)
fmt.Println(resultarr[i])
} else if Alg == "BM" {
resultarr[i] = getAnswerBM(input)
fmt.Println(resultarr[i])
}
default:
resultarr[i] = ""
}
}
}
}
}
if err != nil {
// Return error if input expression is invalid
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(ErrorResponse{err.Error()})
return
}
result = strings.Join(resultarr, " ㋡ ")
// Return successful result
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(ResultResponse{result})
}
func defineType(input string) string {
// define what type is the input
// textQuestionRegex := regexp.MustCompile(`^[A-Za-z0-9\s\?\.,!]+$`)
calculatorRegex := regexp.MustCompile(`^[\d\+\-\*\/\^\(\)]+$`)
dateRegex := regexp.MustCompile(`^hari apa tanggal (\d{1,2})/(\d{1,2})/(\d{4})$`)
var questionType string
switch {
case calculatorRegex.MatchString(input):
questionType = "calculator"
case dateRegex.MatchString(input):
questionType = "date"
// case textQuestionRegex.MatchString(input):
// questionType = "textQuestion"
default:
questionType = "textQuestion"
}
return questionType
}
func NewAPIServer(port string) *APIServer {
return &APIServer{port: port}
}
func (s *APIServer) Start() {
// Create router
router := mux.NewRouter()
// Add handler for arithmetic expressions
router.HandleFunc("/api/question", FuncHandler).Methods("POST")
// Add CORS middleware to router
handler := corsMiddleware(router)
// Start server
log.Fatal(http.ListenAndServe(s.port, handler))
}
func corsMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
w.Header().Set("Access-Control-Max-Age", "86400") // 24 hours
if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK)
return
}
next.ServeHTTP(w, r)
})
}