create minimum service
This commit is contained in:
parent
d6a6dba172
commit
3eb7a46929
8
.idea/.gitignore
vendored
Normal file
8
.idea/.gitignore
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
8
.idea/modules.xml
Normal file
8
.idea/modules.xml
Normal file
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/trashlog.iml" filepath="$PROJECT_DIR$/.idea/trashlog.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
9
.idea/trashlog.iml
Normal file
9
.idea/trashlog.iml
Normal file
@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="WEB_MODULE" version="4">
|
||||
<component name="Go" enabled="true" />
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$" />
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
6
.idea/vcs.xml
Normal file
6
.idea/vcs.xml
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
105
app/app.go
Normal file
105
app/app.go
Normal file
@ -0,0 +1,105 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/BlackBroker/trashlog/router"
|
||||
"github.com/BlackBroker/trashlog/version"
|
||||
"github.com/skeris/appInit"
|
||||
"github.com/themakers/hlog"
|
||||
"go.uber.org/zap"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type App struct {
|
||||
err chan error
|
||||
logger *zap.Logger
|
||||
}
|
||||
|
||||
var (
|
||||
errInvalidOptions = errors.New("invalid options")
|
||||
)
|
||||
|
||||
type Options struct {
|
||||
Development bool `env:"DEVELOPMENT" default:"true"`
|
||||
AppName string `env:"APP_NAME" default:"trashlog"`
|
||||
Addr string `env:"APP_ADDR" default:"localhost:7113"`
|
||||
}
|
||||
|
||||
|
||||
var _ appInit.CommonApp = (*App)(nil)
|
||||
|
||||
type InfoSvcStarted struct{}
|
||||
type InfoSvcReady struct{}
|
||||
type InfoSvcShutdown struct{
|
||||
Signal string
|
||||
}
|
||||
type ErrorCanNotServe struct{
|
||||
Err error
|
||||
}
|
||||
type ErrorFailToConnectTarantool struct{
|
||||
Err error
|
||||
}
|
||||
|
||||
var zapOptions = []zap.Option{
|
||||
zap.AddCaller(),
|
||||
zap.AddCallerSkip(2),
|
||||
zap.AddStacktrace(zap.ErrorLevel),
|
||||
}
|
||||
|
||||
func New(ctx context.Context, opts interface{}) (appInit.CommonApp, error) {
|
||||
var (
|
||||
err error
|
||||
errChan = make(chan error)
|
||||
logger *zap.Logger
|
||||
options Options
|
||||
ok bool
|
||||
)
|
||||
|
||||
if options, ok = opts.(Options); !ok {
|
||||
return App{}, errInvalidOptions
|
||||
}
|
||||
|
||||
if options.Development {
|
||||
logger, err = zap.NewDevelopment(zapOptions...)
|
||||
} else {
|
||||
logger, err = zap.NewProduction(zapOptions...)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
logger = logger.Named(options.AppName)
|
||||
|
||||
logger = logger.With(
|
||||
zap.String("SvcCommit", version.Commit),
|
||||
zap.String("SvcVersion", version.Release),
|
||||
zap.String("SvcBuildTime", version.BuildTime),
|
||||
)
|
||||
|
||||
log := hlog.New(logger)
|
||||
log.Emit(InfoSvcStarted{})
|
||||
|
||||
mux := router.NewRouter()
|
||||
|
||||
log.Emit(InfoSvcReady{})
|
||||
if err := http.ListenAndServe(options.Addr, mux); err != nil {
|
||||
log.Emit(ErrorCanNotServe{
|
||||
Err: err,
|
||||
})
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return App{
|
||||
err: errChan,
|
||||
logger: logger,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (a App) GetLogger() *zap.Logger {
|
||||
return a.logger
|
||||
}
|
||||
|
||||
func (a App) GetErr() chan error {
|
||||
return a.err
|
||||
}
|
54
getenv.go
Normal file
54
getenv.go
Normal file
@ -0,0 +1,54 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/BlackBroker/trashlog/app"
|
||||
"os"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
ENV = "env"
|
||||
DEFAULT = "default"
|
||||
)
|
||||
|
||||
func getEnv() interface{} {
|
||||
var res app.Options
|
||||
|
||||
r := reflect.ValueOf(res)
|
||||
for i := 0; i < r.NumField(); i++ {
|
||||
v := r.Type().Field(i).Tag.Get(ENV)
|
||||
d := r.Type().Field(i).Tag.Get(DEFAULT)
|
||||
|
||||
env, ok := os.LookupEnv(v)
|
||||
var val string
|
||||
if ok {
|
||||
val = env
|
||||
} else {
|
||||
val = d
|
||||
}
|
||||
|
||||
switch t := r.Type().Field(i).Type.Name(); t {
|
||||
case "string":
|
||||
reflect.ValueOf(&res).Elem().Field(i).SetString(val)
|
||||
case "bool":
|
||||
if strings.ToLower(val) == "true" {
|
||||
reflect.ValueOf(&res).Elem().Field(i).SetBool(true)
|
||||
} else {
|
||||
reflect.ValueOf(&res).Elem().Field(i).SetBool(false)
|
||||
}
|
||||
case "uint64":
|
||||
num, err := strconv.ParseUint(val, 10, 64)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
reflect.ValueOf(&res).Elem().Field(i).SetUint(num)
|
||||
default:
|
||||
panic(errors.New("Something strange happend: " + t))
|
||||
}
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
9
handlers/handlers.go
Normal file
9
handlers/handlers.go
Normal file
@ -0,0 +1,9 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func Liveness(writer http.ResponseWriter, _ *http.Request) {
|
||||
writer.WriteHeader(http.StatusOK)
|
||||
}
|
10
main.go
Normal file
10
main.go
Normal file
@ -0,0 +1,10 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/BlackBroker/trashlog/app"
|
||||
"github.com/skeris/appInit"
|
||||
)
|
||||
|
||||
func main() {
|
||||
appInit.Initialize(app.New, getEnv())
|
||||
}
|
21
router/router.go
Normal file
21
router/router.go
Normal file
@ -0,0 +1,21 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"github.com/BlackBroker/trashlog/handlers"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func NewRouter(additional... map[string]http.HandlerFunc) *http.ServeMux {
|
||||
apiMux := http.NewServeMux()
|
||||
|
||||
apiMux.HandleFunc("/liveness", handlers.Liveness)
|
||||
apiMux.HandleFunc("/readiness", handlers.Liveness)
|
||||
|
||||
for _, handler := range additional {
|
||||
for endpoint, handler := range handler {
|
||||
apiMux.HandleFunc(endpoint, handler)
|
||||
}
|
||||
}
|
||||
|
||||
return apiMux
|
||||
}
|
10
version/version.go
Normal file
10
version/version.go
Normal file
@ -0,0 +1,10 @@
|
||||
package version
|
||||
|
||||
var (
|
||||
// BuildTime is a time label of the moment when the binary was built
|
||||
BuildTime = "unset"
|
||||
// Commit is a last commit hash at the moment when the binary was built
|
||||
Commit = "unset"
|
||||
// Release is a semantic version of current build
|
||||
Release = "unset"
|
||||
)
|
Loading…
Reference in New Issue
Block a user