-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathinterfaces.go
92 lines (80 loc) · 2.31 KB
/
interfaces.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
// Copyright (c) 2019, Viet Tran, 200Lab Team.
package goservice
import (
"github.com/200Lab-Education/go-sdk/logger"
"github.com/gin-gonic/gin"
)
// Convenience option method for creating/initializing a service
type Option func(*service)
// HTTP Server Handler for register some routes and gin handlers
type HttpServerHandler = func(*gin.Engine)
// A kind of server job
type Function func(ServiceContext) error
// The storage store all db connection in service
type Storage interface {
Get(prefix string) (interface{}, bool)
MustGet(prefix string) interface{}
}
type PrefixRunnable interface {
HasPrefix
Runnable
}
type HasPrefix interface {
GetPrefix() string
Get() interface{}
}
// The heart of SDK, Service represents for a real micro service
// with its all components
type Service interface {
// A part of Service, it's passed to all handlers/functions
ServiceContext
// Name of the service
Name() string
// Version of the service
Version() string
// Gin HTTP Server wrapper
HTTPServer() HttpServer
// Init with options, they can be db connections or
// anything the service need handle before starting
Init() error
// This method returns service if it is registered on discovery
IsRegistered() bool
// Start service and its all component.
// It will be stopped if any service return error
Start() error
// Stop service and its all component.
Stop()
// Method export all flags to std/terminal
// We might use: "> .env" to move its content .env file
OutEnv()
}
// Service Context: A wrapper for all things needed for developing a service
type ServiceContext interface {
// Logger for a specific service, usually it has a prefix to distinguish
// with each others
Logger(prefix string) logger.Logger
// Get component with prefix
Get(prefix string) (interface{}, bool)
MustGet(prefix string) interface{}
Env() string
}
// Runnable is an abstract object in SDK
// Almost components are Runnable. SDK will manage their lifecycle
// InitFlags -> Configure -> Run -> Stop
type Runnable interface {
Name() string
InitFlags()
Configure() error
Run() error
Stop() <-chan bool
}
// GIN HTTP server for REST API
type HttpServer interface {
Runnable
// Add handlers to GIN
AddHandler(HttpServerHandler)
// Return server config
//GetConfig() http_server.Config
// URI that the server is listening
URI() string
}