-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
58 lines (45 loc) · 1.29 KB
/
main.go
File metadata and controls
58 lines (45 loc) · 1.29 KB
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
package main
import (
"fmt"
"log"
"net/http"
"github.com/Moustafa-Moustafa/equation"
"github.com/gorilla/mux"
)
// Solve the system of linear equations
func solve(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
sol, err := solveEquations(params["equation1"], params["equation2"])
if err != nil {
fmt.Fprint(w, err)
return
}
fmt.Fprintf(w, "Solution: x = %f, y = %f", sol.FirstUnknown, sol.SecondUnknown)
}
func solveEquations(firstEquation string, secondEquation string) (equation.Solution, error) {
parser := *equation.NewParser()
e1, err := parser.Parse(firstEquation)
if err != nil {
return equation.Solution{0, 0}, err
}
e2, err := parser.Parse(secondEquation)
if err != nil {
return equation.Solution{0, 0}, err
}
solver := *equation.NewSolver()
sol, err := solver.Solve([]equation.Equation{e1, e2})
if err != nil {
return equation.Solution{0, 0}, err
}
return sol, nil
}
func home(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Invalid page: please use %s/solve/{equation1},{equation2}", r.Host)
}
func main() {
router := mux.NewRouter()
router.HandleFunc("/solve/{equation1},{equation2}", solve).Methods("GET")
router.PathPrefix("/").HandlerFunc(home)
// router.HandleFunc("/*", home)
log.Fatal(http.ListenAndServe(":8000", router))
}