gonewsctl stub, groups create/delete queries, http server for API.
This commit is contained in:
parent
bc7b9925a1
commit
2e10081b99
10
cmd/gonewsctl/main.go
Normal file
10
cmd/gonewsctl/main.go
Normal file
@ -0,0 +1,10 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
// stdlib
|
||||
"log"
|
||||
)
|
||||
|
||||
func main() {
|
||||
log.Println("Starting gonewsctl...")
|
||||
}
|
@ -12,6 +12,7 @@ import (
|
||||
"sources.dev.pztrn.name/gonews/gonews/configuration"
|
||||
"sources.dev.pztrn.name/gonews/gonews/database"
|
||||
"sources.dev.pztrn.name/gonews/gonews/eventer"
|
||||
"sources.dev.pztrn.name/gonews/gonews/httpserver"
|
||||
"sources.dev.pztrn.name/gonews/gonews/networker"
|
||||
)
|
||||
|
||||
@ -22,6 +23,8 @@ func main() {
|
||||
database.Initialize()
|
||||
eventer.Initialize()
|
||||
commands.Initialize()
|
||||
httpserver.Initialize()
|
||||
httpserver.Start()
|
||||
networker.Initialize()
|
||||
|
||||
eventer.InitializeCompleted()
|
||||
|
@ -4,6 +4,8 @@ package configuration
|
||||
type config struct {
|
||||
// Database represents database configuration.
|
||||
Database Database `yaml:"database"`
|
||||
// HTTP represents HTTP server configuration.
|
||||
HTTP HTTP `yaml:"http"`
|
||||
// Network represents network stack configuration.
|
||||
Network []Network `yaml:"network"`
|
||||
}
|
||||
@ -20,6 +22,16 @@ type Database struct {
|
||||
Timeout int64 `yaml:"timeout"`
|
||||
}
|
||||
|
||||
type HTTP struct {
|
||||
// Listen is an address in form "ip:port" on which HTTP server
|
||||
// will listen to requests.
|
||||
Listen string `yaml:"listen"`
|
||||
// WaitForSeconds is a timeout for waiting for HTTP server to be
|
||||
// ready. If timeout will be passed and HTTP server will not be ready
|
||||
// to process requests - we will exit.
|
||||
WaitForSeconds int `yaml:"wait_for_seconds"`
|
||||
}
|
||||
|
||||
type Network struct {
|
||||
// Address represents address to bing in form of "ip:port".
|
||||
Address string `yaml:"address"`
|
||||
|
45
database/migrations/2_create_groups_info_table.go
Normal file
45
database/migrations/2_create_groups_info_table.go
Normal file
@ -0,0 +1,45 @@
|
||||
package migrations
|
||||
|
||||
import (
|
||||
// stdlib
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
// CreateGroupsTableUp creates table with groups information.
|
||||
func CreateGroupsTableUp(tx *sql.Tx) error {
|
||||
if _, err := tx.Exec(`
|
||||
CREATE TABLE groups (
|
||||
uuid UUID NOT NULL,
|
||||
group_name TEXT NOT NULL,
|
||||
group_description TEXT,
|
||||
articles_count INTEGER NOT NULL DEFAULT 0,
|
||||
created_at TIMESTAMP WITH TIME ZONE NOT NULL,
|
||||
last_message_at TIMESTAMP WITH TIME ZONE NOT NULL
|
||||
);
|
||||
|
||||
COMMENT ON COLUMN groups.uuid IS 'Group UUID';
|
||||
COMMENT ON COLUMN groups.group_name IS 'Group name';
|
||||
COMMENT ON COLUMN groups.group_description IS 'Group description';
|
||||
COMMENT ON COLUMN groups.articles_count IS 'Articles count in group';
|
||||
COMMENT ON COLUMN groups.created_at IS 'Group creation timestamp';
|
||||
COMMENT ON COLUMN groups.last_message_at IS 'When last message appeared in this group?';
|
||||
|
||||
CREATE INDEX groups_uuid_idx ON groups(uuid);
|
||||
CREATE INDEX groups_name_idx ON groups(group_name);
|
||||
CREATE INDEX groups_created_at_idx ON groups(created_at);
|
||||
|
||||
`); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// CreateGroupsTableDown deletes table with groups information.
|
||||
func CreateGroupsTableDown(tx *sql.Tx) error {
|
||||
if _, err := tx.Exec(`DROP TABLE groups;`); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
@ -19,6 +19,7 @@ func Initialize() {
|
||||
_ = goose.SetDialect("postgres")
|
||||
|
||||
goose.AddNamedMigration("1_create_users_table.go", CreateUsersTableUp, CreateUsersTableDown)
|
||||
goose.AddNamedMigration("2_create_groups_info_table.go", CreateGroupsTableUp, CreateGroupsTableDown)
|
||||
}
|
||||
|
||||
// Migrate parses environment for necessary parameters and starts
|
||||
|
16
go.mod
Normal file
16
go.mod
Normal file
@ -0,0 +1,16 @@
|
||||
module sources.dev.pztrn.name/gonews/gonews
|
||||
|
||||
go 1.13
|
||||
|
||||
require (
|
||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
|
||||
github.com/jmoiron/sqlx v1.2.0
|
||||
github.com/labstack/echo v3.3.10+incompatible
|
||||
github.com/labstack/gommon v0.3.0 // indirect
|
||||
github.com/lib/pq v1.2.0
|
||||
github.com/pressly/goose v2.6.0+incompatible
|
||||
github.com/rs/zerolog v1.15.0 // indirect
|
||||
github.com/stretchr/testify v1.4.0
|
||||
google.golang.org/appengine v1.6.5 // indirect
|
||||
gopkg.in/yaml.v2 v2.2.4
|
||||
)
|
62
go.sum
Normal file
62
go.sum
Normal file
@ -0,0 +1,62 @@
|
||||
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
|
||||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
|
||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
|
||||
github.com/go-sql-driver/mysql v1.4.0 h1:7LxgVwFb2hIQtMm87NdgAVfXjnt4OePseqT1tKx+opk=
|
||||
github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
|
||||
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
github.com/jmoiron/sqlx v1.2.0 h1:41Ip0zITnmWNR/vHV+S4m+VoUivnWY5E4OJfLZjCJMA=
|
||||
github.com/jmoiron/sqlx v1.2.0/go.mod h1:1FEQNm3xlJgrMD+FBdI9+xvCksHtbpVBBw5dYhBSsks=
|
||||
github.com/labstack/echo v3.3.10+incompatible h1:pGRcYk231ExFAyoAjAfD85kQzRJCRI8bbnE7CX5OEgg=
|
||||
github.com/labstack/echo v3.3.10+incompatible/go.mod h1:0INS7j/VjnFxD4E2wkz67b8cVwCLbBmJyDaka6Cmk1s=
|
||||
github.com/labstack/gommon v0.3.0 h1:JEeO0bvc78PKdyHxloTKiF8BD5iGrH8T6MSeGvSgob0=
|
||||
github.com/labstack/gommon v0.3.0/go.mod h1:MULnywXg0yavhxWKc+lOruYdAhDwPK9wf0OL7NoOu+k=
|
||||
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
|
||||
github.com/lib/pq v1.2.0 h1:LXpIM/LZ5xGFhOpXAQUIMM1HdyqzVYM13zNdjCEEcA0=
|
||||
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
|
||||
github.com/mattn/go-colorable v0.1.2 h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx8mU=
|
||||
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
|
||||
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
|
||||
github.com/mattn/go-isatty v0.0.9 h1:d5US/mDsogSGW37IV293h//ZFaeajb69h+EHFsv2xGg=
|
||||
github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ=
|
||||
github.com/mattn/go-sqlite3 v1.9.0 h1:pDRiWfl+++eC2FEFRy6jXmQlvp4Yh3z1MJKg4UeYM/4=
|
||||
github.com/mattn/go-sqlite3 v1.9.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
|
||||
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
|
||||
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/pressly/goose v2.6.0+incompatible h1:3f8zIQ8rfgP9tyI0Hmcs2YNAqUCL1c+diLe3iU8Qd/k=
|
||||
github.com/pressly/goose v2.6.0+incompatible/go.mod h1:m+QHWCqxR3k8D9l7qfzuC/djtlfzxr34mozWDYEu1z8=
|
||||
github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ=
|
||||
github.com/rs/zerolog v1.15.0 h1:uPRuwkWF4J6fGsJ2R0Gn2jB1EQiav9k3S6CSdygQJXY=
|
||||
github.com/rs/zerolog v1.15.0/go.mod h1:xYTKnLHcpfU2225ny5qZjxnj9NvkumZYjJHlAThCjNc=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
|
||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
|
||||
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
|
||||
github.com/valyala/fasttemplate v1.0.1 h1:tY9CJiPnMXf1ERmG2EyK7gNUd+c6RKGD0IfU8WdUSz8=
|
||||
github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8=
|
||||
github.com/valyala/fasttemplate v1.1.0 h1:RZqt0yGBsps8NGvLSGW804QQqCUYYLsaOjTVHy1Ocw4=
|
||||
github.com/valyala/fasttemplate v1.1.0/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8=
|
||||
github.com/zenazn/goji v0.9.0/go.mod h1:7S9M489iMyHBNxwZnk9/EHS098H4/F6TATF2mIxtB1Q=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20190425163242-31fd60d6bfdc/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
|
||||
google.golang.org/appengine v1.6.5 h1:tycE03LOZYQNhDpS27tcQdAzLCVMaj7QT2SXxebnpCM=
|
||||
google.golang.org/appengine v1.6.5/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I=
|
||||
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
@ -3,7 +3,11 @@ database:
|
||||
params: "connect_timeout=10&sslmode=disable"
|
||||
timeout: 10
|
||||
|
||||
http:
|
||||
listen: "127.0.0.1:43210"
|
||||
wait_for_seconds: 10
|
||||
|
||||
network:
|
||||
- address: "0.0.0.0:1199"
|
||||
- address: "127.0.0.1:1199"
|
||||
type: "client"
|
||||
limit: 50
|
113
httpserver/exported.go
Normal file
113
httpserver/exported.go
Normal 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
39
httpserver/strictjson.go
Normal 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
|
||||
}
|
Loading…
Reference in New Issue
Block a user