/
server.go
150 lines (121 loc) · 3.62 KB
/
server.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
/*******************************************************************/
/*
/* Blog project
/*
/* Copyright (C) 2019 Sergey Denisov.
/*
/* Written by Sergey Denisov aka LittleBuster (DenisovS21@gmail.com)
/* Github: https://github.com/LittleBuster
/* https://github.com/futcamp
/*
/* This library is free software; you can redistribute it and/or
/* modify it under the terms of the GNU General Public Licence 3
/* as published by the Free Software Foundation; either version 3
/* of the Licence, or (at your option) any later version.
/*
/*******************************************************************/
package server
import (
"encoding/json"
"fmt"
"strconv"
"github.com/LittleBuster/blog/router/services/post"
"github.com/LittleBuster/blog/router/utils/configs"
"github.com/buaazp/fasthttprouter"
"github.com/google/logger"
"github.com/valyala/fasthttp"
)
type Server interface {
Start(ip string, port int, api string) error
}
type WebServer struct {
configs configs.Configurable
}
// NewWebServer make new struct
func NewWebServer(cfg configs.Configurable) Server {
return &WebServer{
configs: cfg,
}
}
// Start start web server
func (w *WebServer) Start(ip string, port int, api string) error {
router := fasthttprouter.New()
router.GET("/", w.IndexHandler)
router.GET(fmt.Sprintf("/api/%s/post/categories", api), w.CatHandler)
router.GET(fmt.Sprintf("/api/%s/post/articles/:cat", api), w.ArtHandler)
return fasthttp.ListenAndServe(fmt.Sprintf("%s:%d", ip, port), router.Handler)
}
//ErrorResponse send error response
func (w *WebServer) ErrorResponse(ctx *fasthttp.RequestCtx, reason string) {
errResp := ErrorResponse{
Service: "post",
Result: false,
Reason: reason,
}
bytesOut, _ := json.Marshal(&errResp)
logger.Error(reason)
ctx.Response.Header.Add("Content-Type", "application/json")
_, err := ctx.Write(bytesOut)
if err != nil {
logger.Error(fmt.Sprintf("Fail to send response"))
}
}
// IndexHandler main server handler
func (w *WebServer) IndexHandler(ctx *fasthttp.RequestCtx) {
indexResp := IndexResponse{
Service: "router",
}
bytesOut, _ := json.Marshal(&indexResp)
ctx.Response.Header.Add("Content-Type", "application/json")
_, err := ctx.Write(bytesOut)
if err != nil {
logger.Error("IndexHandler: Fail to send response")
}
}
// CatHandler get categories handler
func (w *WebServer) CatHandler(ctx *fasthttp.RequestCtx) {
var err error
client := post.NewPostClient(w.configs.Settings().Post.Ip,
w.configs.Settings().Post.Port)
dataResp := DataResponse{
Service: "post",
Result: true,
}
dataResp.Data, err = client.Categories()
if err != nil {
w.ErrorResponse(ctx, "Fail to get categories data from service")
return
}
bytesOut, _ := json.Marshal(&dataResp)
ctx.Response.Header.Add("Content-Type", "application/json")
_, err = ctx.Write(bytesOut)
if err != nil {
logger.Error("CatHandler: Fail to send response")
}
}
// ArtHandler get posts handler
func (w *WebServer) ArtHandler(ctx *fasthttp.RequestCtx) {
var err error
category, err := strconv.Atoi(ctx.UserValue("cat").(string))
if err != nil {
w.ErrorResponse(ctx, "Fail to get categories data from service")
return
}
client := post.NewPostClient(w.configs.Settings().Post.Ip,
w.configs.Settings().Post.Port)
dataResp := DataResponse{
Service: "post",
Result: true,
}
dataResp.Data, err = client.Posts(category)
if err != nil {
w.ErrorResponse(ctx, "Fail to get posts data from service")
return
}
bytesOut, _ := json.Marshal(&dataResp)
ctx.Response.Header.Add("Content-Type", "application/json")
_, err = ctx.Write(bytesOut)
if err != nil {
logger.Error("PostHandler: Fail to send response")
}
}