/
main.go
69 lines (57 loc) · 1.22 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
package main
import (
"github.com/kataras/iris"
"github.com/kataras/iris/context"
"net/http"
"os"
"time"
)
const (
Port = "3000"
)
func main() {
app := iris.New()
app.Handle("GET", "/", func(ctx context.Context) {
ctx.HTML("<b> Hello API! </b>")
})
// API Routes
apiRoutes := app.Party("/api", logThisMiddleware)
{
apiRoutes.Get("/", getAPIInfo)
apiRoutes.Get("/todos", getTodos)
}
// Start listening
port := Port
if len(os.Getenv("PORT")) > 0 {
port = os.Getenv("PORT")
}
server := &http.Server{
Addr: ":" + port,
Handler: app,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
app.Run(iris.Server(server))
}
func logThisMiddleware(ctx context.Context) {
ctx.Application().Log("Path: %s | IP: %s", ctx.Path(), ctx.RemoteAddr())
ctx.Next()
}
func getAPIInfo(ctx context.Context) {
ctx.JSON(map[string]interface{}{
"version": "0.0.1",
})
}
type TodoModel struct {
Id int `json:"id"`
Title string `json:"title"`
}
var todos = map[string]TodoModel{
"1": TodoModel{Id: 1, Title: "Test1"},
"2": TodoModel{Id: 2, Title: "Test2"},
"3": TodoModel{Id: 3, Title: "Test3"},
}
func getTodos(ctx context.Context) {
ctx.JSON(todos)
}