gonewsctl stub, groups create/delete queries, http server for API.

This commit is contained in:
2019-10-22 05:47:59 +05:00
parent bc7b9925a1
commit 2e10081b99
10 changed files with 306 additions and 1 deletions

113
httpserver/exported.go Normal file
View File

@@ -0,0 +1,113 @@
package httpserver
import (
// stdlib
"context"
"io/ioutil"
"log"
"net/http"
"strings"
"time"
// local
"sources.dev.pztrn.name/gonews/gonews/configuration"
// other
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
)
var (
Srv *echo.Echo
)
func Initialize() {
log.Println("Initializing HTTP server...")
Srv = echo.New()
Srv.Use(middleware.Recover())
Srv.Use(middleware.Logger())
Srv.DisableHTTP2 = true
Srv.HideBanner = true
Srv.HidePort = true
Srv.Binder = echo.Binder(&StrictJSONBinder{})
Srv.GET("/_internal/waitForOnline", waitForHTTPServerToBeUpHandler)
}
// Shutdown stops HTTP server. Returns true on success and false on failure.
func Shutdown() {
log.Println("Shutting down HTTP server...")
err := Srv.Shutdown(context.Background())
if err != nil {
log.Fatalln("Failed to stop HTTP server:", err.Error())
}
log.Println("HTTP server shutted down")
}
// Start starts HTTP server and checks that server is ready to process
// requests. Returns true on success and false on failure.
func Start() {
log.Println("Starting HTTP server on " + configuration.Cfg.HTTP.Listen + "...")
go func() {
err := Srv.Start(configuration.Cfg.HTTP.Listen)
if !strings.Contains(err.Error(), "Server closed") {
log.Fatalln("HTTP server critical error occurred:", err.Error())
}
}()
// Check that HTTP server was started.
httpc := &http.Client{Timeout: time.Second * 1}
checks := 0
for {
checks++
if checks >= configuration.Cfg.HTTP.WaitForSeconds {
log.Fatalln("HTTP server isn't up after", checks, "seconds")
}
time.Sleep(time.Second * 1)
resp, err := httpc.Get("http://" + configuration.Cfg.HTTP.Listen + "/_internal/waitForOnline")
if err != nil {
log.Println("HTTP error occurred, HTTP server isn't ready, waiting (error was: '" + err.Error() + "')")
continue
}
response, err1 := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err1 != nil {
log.Println("Failed to read response body, HTTP server isn't ready, waiting (error was: '" + err1.Error() + "')")
continue
}
log.Println("HTTP response with status '" + resp.Status + "' received")
if resp.StatusCode == http.StatusOK {
if len(response) == 0 {
log.Println("Response is empty, HTTP server isn't ready, waiting...")
continue
}
log.Printf("Got response with status code %d and body: %+v\n", resp.StatusCode, string(response))
if len(response) == 17 {
break
}
}
}
log.Println("HTTP server is ready to process requests")
}
func waitForHTTPServerToBeUpHandler(ec echo.Context) error {
response := map[string]string{
"error": "None",
}
return ec.JSON(200, response)
}

39
httpserver/strictjson.go Normal file
View File

@@ -0,0 +1,39 @@
package httpserver
import (
// stdlib
"encoding/json"
"fmt"
"net/http"
// other
"github.com/labstack/echo"
)
// StrictJSONBinder implements Binder interface for Echo. It will parse
// JSON in strict mode throwing errors on schema mismatches.
type StrictJSONBinder struct{}
// Bind parses JSON input.
func (sjb *StrictJSONBinder) Bind(i interface{}, c echo.Context) error {
req := c.Request()
if req.ContentLength == 0 {
return echo.NewHTTPError(http.StatusBadRequest, "Request body can't be empty")
}
// Decode it.
decoder := json.NewDecoder(req.Body)
decoder.DisallowUnknownFields()
if err := decoder.Decode(i); err != nil {
if ute, ok := err.(*json.UnmarshalTypeError); ok {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Unmarshal type error: expected=%v, got=%v, field=%v, offset=%v", ute.Type, ute.Value, ute.Field, ute.Offset))
} else if se, ok := err.(*json.SyntaxError); ok {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Syntax error: offset=%v, error=%v", se.Offset, se.Error()))
} else {
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
}
}
return nil
}