tmp

package
v1.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 14, 2021 License: MIT Imports: 0 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DirCore          = "core"
	DirCmd           = "cmd"
	DirDatabase      = "database"
	DirHub           = "hub"
	DirHubHelper     = "hub_helper"
	DirHelper        = "helper"
	DirHandlers      = "handlers"
	DirHandler       = "%v_handler"
	DirHandlerHelper = "%v_helper"
	DirStore         = "store"
	DirDBSStore      = "%v_store"
	DirSource        = "source"
	DirConfigs       = "configs"
	DirUploads       = "uploads"
)
View Source
const (
	FileMain          = "main.go"
	FileDatabase      = "database.go"
	FileServer        = "server.go"
	FileModel         = "model.go"
	FileFunctions     = "functions.go"
	FileHelper        = "helper.go"
	FileHandler       = "handler.go"
	FileHubMiddleware = "middleware.go"
	FileStore         = "store.go"
	FileHub           = "hub.go"
	FileConfigServer  = "configServer.json"
	FileMod           = "go.mod"
	FileSum           = "go.sum"
	FileDocker        = "Dockerfile"
	FileComposeBuild  = "docker-compose-build.yaml"
	FileComposeLocal  = "docker-compose-local.yaml"
	FileReadme        = "README.md"
)
View Source
const ComposeBuildTmp = `` /* 192-byte string literal not displayed */
View Source
const ComposeLocalTmp = `version: "3"

services: {{range $i,$k := .DBS}}{{if eq $k "` + string(Mongodb) + `"}}
   {{print $i}}:
      container_name: {{print $i}}
      image: mongo
      restart: always
      environment:
         MONGO_INITDB_DATABASE:
      volumes:
         - ./source/db:/data/db
         - ./source/dbres:/data/res
      networks:
         - net
      command: mongod --auth {{else if eq $k "` + string(Postgres) + `"}}
   {{print $i}}:
      container_name: {{print $i}}
      image: postgres
      restart: always
      environment:
          POSTGRES_PASSWORD:
          POSTGRES_USER:
          POSTGRES_DB:
      ports:
          - "5432:5432"
      volumes:
          - ../source/db:/var/lib/postgresql/data
      networks:
          - net {{end}}
      {{end}}
   server:
      container_name: {{print .ModuleName}}
      image: localhost:5000/{{print .ModuleName}}:latest
      restart: always
      expose: 8090
      networks:
         - net {{if gt (len .DBS) 0}}
      depends_on: {{range $i,$k := .DBS}}
         {{printf "- %v" $i}}{{end}}{{end}}
      volumes:
         - ./source/uploads:/server/source/uploads
         - ./source/configs:/server/source/configs

networks:
   net:
      driver: bridge`
View Source
const ConfigTmp = `{
	"host": "",
	"port": 8090,
	"dbs": { {{range $i,$k := .DBS}}{{if eq $k "` + string(Postgres) + `"}}
		"{{print $i}}": {
			"type": "postgres",
			"host": "0.0.0.0",
			"port": 5432,
			"name": "",
			"user": "postgres",
			"password": "1"
		}{{else if eq $k "` + string(Mongodb) + `"}}
		"{{print $i}}": {
			"type": "mongodb",
			"host": "0.0.0.0",
			"port": 27017,
			"name": "",
			"user": "mongodb",
			"password": "1"
		}{{end}},{{end}}
	},
	"handlers": { {{range $i,$k := .Handlers}}{{if eq $k "` + string(TCP) + `"}}
		"{{print $i}}": {
			"type": "tcp",
			"is_tsl": false
		}{{else if eq $k "` + string(MQTT) + `"}}
		"{{print $i}}": {
			"type": "mqtt",
			"host": "0.0.0.0",
			"port": 1883,
			"user": "",
			"password": "",
			"is_tsl": false
		}{{else if eq $k "` + string(WS) + `"}}
		"{{print $i}}": {
			"type": "ws",
			"password": "1",
			"is_tsl": false
		}{{end}},{{end}}
	}
}`
View Source
const DatabaseTmp = `package database{{$module := .ModuleName}}

import (
	"fmt" 
	{{if isOnePostgres}}
	"database/sql"
	_ "github.com/lib/pq"{{end}}
	{{printf "\"%v/helper\"" $module}}
	{{range $i,$k := .DBS}}
	{{printf "\"%v/store/%v_store\"" $module $i }}{{end}}
	{{if isOneMongo}}
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"{{end}}
)

const ( {{range $i,$k := .DBS}}
	{{printf "_%v = \"%v\"" $i $i}}{{end}}
)

type DB interface { {{range $i,$k := .DBS}}{{if eq $k "` + string(Mongodb) + `"}}
	{{printf "%v() *mongo.Database" (title $i)}}{{else if eq $k "` + string(Postgres) + `"}}
	{{printf "%v() *sql.DB" (title $i)}}{{end}}
	{{printf "%v_store() %v_store.Store" (title $i) $i}}{{end}}
	Close()
}

type DBForHandler interface { {{range $i,$k := .DBS}}
	{{printf "%v_store() %v_store.Store" (title $i) $i}}{{end}}
}

type db struct { {{range $i,$k := .DBS}}
	{{printf "_%v *d" $i}}{{end}}
}

type d struct {
	store  interface{}
	conn   interface{}
	dbName string
}

func InitDB(conf *helper.Config) (DB DB, err error) {
	db := &db{}
	DB = db

	var conn interface{} {{range $i,$k := .DBS}}
	if v, ok := conf.DBS[{{printf "_%v" $i}}]; ok {
		conn, err = {{if eq $k "` + string(Mongodb) + `"}}connMongo(v){{else if eq $k "` + string(Postgres) + `"}}connPostgres(v){{end}}
		if err != nil {
			return nil, fmt.Errorf("db not initializing: %v", err)
		}
		db.{{printf "_%v" $i}} = &d{
			store:  {{print $i}}_store.InitStore({{if eq $k "` + string(Mongodb) + `"}}conn.(*mongo.Client).Database(v.Name){{else if eq $k "` + string(Postgres) + `"}}conn.(*sql.DB){{end}}),
			dbName: v.Name,
			conn:   conn,
		}
		helper.Log.Servicef("db %q initializing", {{printf "_%v" $i}})
	}{{end}}

	helper.Log.Service("db initializing")
	return
}

func (d *db) Close() { {{range $i,$k := .DBS}}{{if eq $k "` + string(Mongodb) + `"}}
	{{printf "d._%v.conn.(*mongo.Client).Disconnect(helper.Ctx)" $i}}{{else if eq $k "` + string(Postgres) + `"}}
	{{printf "d._%v.conn.(*sql.DB).Close()" $i}}{{end}}
	{{printf "helper.Log.Servicef(%v, _%v)" "\"db %q stoped\"" $i}}{{end}}
}
{{range $i,$k := .DBS}}{{if eq $k "` + string(Mongodb) + `"}}
{{printf "func (d *db) %v() *mongo.Database { return d._%v.conn.(*mongo.Client).Database(d._%v.dbName)" (title $i) $i $i}}}{{else if eq $k "` + string(Postgres) + `"}}
{{printf "func (d *db) %v() *sql.DB { return d._%v.conn.(*sql.DB)}" (title $i) $i}}{{end}}
{{printf "func (d *db) %v_store() %v_store.Store { return d._%v.store.(%v_store.Store)}" (title $i) $i $i $i}}{{end}}
{{if isOnePostgres}}
func connPostgres(v *helper.DbConfig) (conn *sql.DB, err error) {
	conn, err = sql.Open("postgres", fmt.Sprintf("user=%v password=%v host=%v port=%v dbname=%v sslmode=disable", v.User, v.Password, v.Host, v.Port, v.Name))
	if err != nil {
		return conn, fmt.Errorf("db not connected: %v", err)
	}
	if err = conn.PingContext(helper.Ctx); err != nil {
		return conn, fmt.Errorf("db not pinged: %v", err)
	}
	return
}{{end}}
{{if isOneMongo}}
func connMongo(v *helper.DbConfig) (conn *mongo.Client, err error) {
	opt := options.Client().ApplyURI(fmt.Sprintf("mongodb://%v:%v", v.Host, v.Port)).SetAuth(options.Credential{AuthMechanism: "SCRAM-SHA-256", Username: v.User, Password: v.Password})
	conn, err = mongo.Connect(helper.Ctx, opt)
	if err != nil {
		return conn, fmt.Errorf("db not connected: %v", err)
	}
	if err = conn.Ping(helper.Ctx, nil); err != nil {
		return conn, fmt.Errorf("db not pinged: %v", err)
	}
	return
}{{end}}`
View Source
const DockerTmp = `` /* 320-byte string literal not displayed */
View Source
const HandlerHelperTmp = `package {{printf "%v_helper" (index . 0)}}
{{if eq (index . 1) "` + WS + `"}}
import "net"
{{end}}
type Handler interface { {{if eq (index . 1) "` + WS + `"}}
	GetAll() map[int64]map[string]net.Conn
	GetConn(userID int64) map[string]net.Conn 
	DeleteConn(userID int64, ipAddr string){{end}}
}`
View Source
const HandlerMQTTTmp = `` /* 2775-byte string literal not displayed */
View Source
const HandlerTCPTmp = `` /* 1541-byte string literal not displayed */
View Source
const HandlerWSTmp = `` /* 1389-byte string literal not displayed */
View Source
const HelperFuncTmp = `` /* 1330-byte string literal not displayed */
View Source
const HelperModelTmp = `package helper

import (
	"context"
	"os"
	"sync"

	"github.com/0LuigiCode0/logger"
)

var Ctx context.Context
var CloseCtx context.CancelFunc
var Log *logger.Logger
var Wg sync.WaitGroup
var C chan os.Signal = make(chan os.Signal, 3)

//Config модель конфига
type Config struct {
	Host     string                    ` + "`json:\"host\"`" + `
	Port     int                       ` + "`json:\"port\"`" + `
	DBS      map[string]*DbConfig      ` + "`json:\"dbs\"`" + `
	Handlers map[string]*HandlerConfig ` + "`json:\"handlers\"`" + `
}

type DbConfig struct {
	Type     DBType ` + "`json:\"type\"`" + `
	Host     string ` + "`json:\"host\"`" + `
	Port     int    ` + "`json:\"port\"`" + `
	Name     string ` + "`json:\"name\"`" + `
	User     string ` + "`json:\"user\"`" + `
	Password string ` + "`json:\"password\"`" + `
}

type HandlerConfig struct {
	Type     HandlerType ` + "`json:\"type\"`" + `
	Host     string      ` + "`json:\"host\"`" + `
	Port     int         ` + "`json:\"port\"`" + `
	User     string      ` + "`json:\"user\"`" + `
	Password string      ` + "`json:\"password\"`" + `
	IsTSL    bool        ` + "`json:\"is_tsl\"`" + `
}

//ErrLocal модель локализации ошибок
type ErrLocal struct {
	TitleEn string ` + "`bson:\"title_en\" json:\"title_en\"`" + `
	TitleRu string ` + "`bson:\"title_ru\" json:\"title_ru\"`" + `
}

//Модель для отправки по ws
type WsType string

const ()

type SendModel struct {
	Type WsType      ` + "`json:\"type\"`" + `
	Data interface{} ` + "`json:\"data\"`" + `
}

//ResponseError модель ошибки
type ResponseError struct {
	Code ErrCode ` + "`json:\"code\"`" + `
	Msg  string  ` + "`json:\"msg\"`" + `
}

//ResponseModel модель ответа
type ResponseModel struct {
	Success bool        ` + "`json:\"success\"`" + `
	Result  interface{} ` + "`json:\"result\"`" + `
}

type DBType string

const (
	Postgres DBType = "postgres"
	Mongodb  DBType = "mongodb"
)

type HandlerType string

const (
	TCP  HandlerType = "tcp"
	MQTT HandlerType = "mqtt"
	WS   HandlerType = "ws"
)

//Основные константы
const (
	UploadDir  = "./source/uploads/"
	ConfigDir  = "./source/configs/"
	ConfigFiel = "configServer.json"
	Secret     = "RB6SmP4h"
)

//Названеия коллекции

type Collection string

const ()

//Ключи контекста

type CtxKey int

const (
	CtxKeyValue CtxKey = iota
)

//Коды ошибок

type ErrCode byte

const (
	ErrorNotFound ErrCode = iota
	ErrorExist
	ErrorSave
	ErrorUpdate
	ErrorDelete
	ErorrAccessDeniedToken
	ErorrAccessDeniedParams
	ErrorInvalidParams
	ErrorParse
	ErrorOpen
	ErrorClose
	ErrorWrite
	ErrorRead
	ErrorGenerate
	ErrorSend
)
const (
	KeyErrorNotFound      = "not found"
	KeyErrorExist         = "already exists"
	KeyErrorSave          = "create is failed"
	KeyErrorUpdate        = "update is failed"
	KeyErrorDelete        = "delete is failed"
	KeyErorrAccessDenied  = "access denied"
	KeyErrorInvalidParams = "invalid params"
	KeyErrorParse         = "parse is failed"
	KeyErrorOpen          = "open is failed"
	KeyErrorClose         = "close is failed"
	KeyErrorRead          = "read is failed"
	KeyErrorWrite         = "write is faleid"
	KeyErrorGenerate      = "generate is falied"
	KeyErrorSend          = "send is faied"
)

//Роли пользователей

type Role byte

const (
	RoleSuperAdmin Role = iota + 1
	RoleAdmin
	RoleUser
)

//Статусы

//Значения сортировки

//Типы полей`
View Source
const HubHelperTmp = `` /* 1171-byte string literal not displayed */
View Source
const HubTmp = `` /* 2284-byte string literal not displayed */
View Source
const MainTmp = `` /* 498-byte string literal not displayed */
View Source
const MiddleWareMQTTTmp = `package {{printf "%v_handler" (index . 0)}}`
View Source
const MiddleWareWSTmp = `` /* 974-byte string literal not displayed */
View Source
const MiddlewareTCPTmp = `` /* 637-byte string literal not displayed */
View Source
const ModTmp = `` /* 560-byte string literal not displayed */
View Source
const ServerTmp = `` /* 1777-byte string literal not displayed */
View Source
const StoreTmp = `package {{printf "%v_store" (index . 0)}}
{{if eq (index . 1) "` + string(Mongodb) + `"}}
import "go.mongodb.org/mongo-driver/mongo"{{else if eq (index . 1) "` + string(Postgres) + `"}}
import "database/sql"{{end}}

type Store interface { {{if eq (index . 1) "` + string(Postgres) + `"}}
	Begin() (*sql.Tx, error){{end}}
}

type store struct{ {{if eq (index . 1) "` + string(Postgres) + `"}}
	db *sql.DB{{end}}
}

func InitStore({{if eq (index . 1) "` + string(Mongodb) + `"}}db *mongo.Database{{else if eq (index . 1) "` + string(Postgres) + `"}}db *sql.DB{{end}}) Store {
	return &store{ {{if eq (index . 1) "` + string(Postgres) + `"}}
		db: db,{{end}}
	}
}
{{if eq (index . 1) "` + string(Postgres) + `"}}
func (s *store) Begin() (*sql.Tx, error) {
	return s.db.Begin()
}{{end}}`

Variables

This section is empty.

Functions

This section is empty.

Types

type DBType

type DBType string
const (
	Postgres DBType = "postgres"
	Mongodb  DBType = "mongodb"
)

type HandlerType

type HandlerType string
const (
	TCP  HandlerType = "tcp"
	MQTT HandlerType = "mqtt"
	WS   HandlerType = "ws"
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL