-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
85 lines (66 loc) · 1.75 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
package main
import (
"log/slog"
"net/http"
"os"
"github.com/stsh89/web-math-go/app"
"github.com/stsh89/web-math-go/equations"
"github.com/stsh89/web-math-go/providers/notion"
"github.com/gin-gonic/gin"
)
func main() {
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
config := buildConfig()
r := gin.Default()
r.GET("/", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"List all equations": "GET /equations",
"Create equation": "POST /equations",
"Delete equation": "DELETE /equations",
"Get points for given equation on a range from -5 to 5": "GET /calculate",
})
})
r.GET("/equations", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"Equations": equations.ListEquations(logger, &config),
})
})
r.POST("/equations", func(c *gin.Context) {
var json struct {
Term string `json:"term" binding:"required"`
}
c.Bind(&json)
c.JSON(http.StatusOK, gin.H{
"ID": equations.CreateEquation(json.Term, logger, &config),
})
})
r.DELETE("/equations", func(c *gin.Context) {
var json struct {
Term string `json:"term" binding:"required"`
}
c.Bind(&json)
c.JSON(http.StatusOK, gin.H{
"ID": equations.DeleteEquation(json.Term, logger, &config),
})
})
r.POST("/calculate", func(c *gin.Context) {
var json struct {
Term string `json:"term" binding:"required"`
Args map[string]float64 `json:"args" binding:"required"`
}
c.Bind(&json)
points := equations.Calculate(json.Term, json.Args, logger, &config)
c.JSON(http.StatusOK, gin.H{
"points": points,
})
})
r.Run()
}
func buildConfig() app.Config {
return app.Config{
NotionConfig: ¬ion.Configuration{
ApiKey: os.Getenv("NOTION_API_KEY"),
DatabaseId: os.Getenv("NOTION_DATABASE_ID"),
},
}
}