forked from devopsfaith/api2html
/
engine.go
116 lines (97 loc) · 3.84 KB
/
engine.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Package engine contains all the required for building and running an API2HTML server
//
// func Run(cfgPath string, devel bool) error {
// errNilEngine := fmt.Errorf("serve cmd aborted: nil engine")
// e, err := engine.New(cfgPath, devel)
// if err != nil {
// log.Println("engine creation aborted:", err.Error())
// return err
// }
// if e == nil {
// log.Println("engine creation aborted:", errNilEngine.Error())
// return errNilEngine
// }
//
// time.Sleep(time.Second)
//
// return eW.Run(fmt.Sprintf(":%d", port))
// }
package engine
import (
"fmt"
"io"
"net/http"
"github.com/gin-gonic/gin"
newrelic "github.com/newrelic/go-agent"
)
// Config is a struct with all the required definitions for building an API2HTML engine
type Config struct {
Pages []Page `json:"pages"`
StaticTXTContent []string `json:"static_txt_content"`
Robots bool `json:"robots"`
Sitemap bool `json:"sitemap"`
Templates map[string]string `json:"templates"`
Layouts map[string]string `json:"layouts"`
Extra map[string]interface{} `json:"extra"`
PublicFolder *PublicFolder `json:"public_folder"`
NewRelic *NewRelic `json:"newrelic"`
}
// PublicFolder contains the info regarding the static contents to be served
type PublicFolder struct {
Path string `json:"path_to_folder"`
Prefix string `json:"url_prefix"`
}
// NewRelic contains the info regarding the app name and the newrelic license key
type NewRelic struct {
AppName string `json:"app_name"`
License string `json:"license"`
}
// Page defines the behaviour of the engine for a given URL pattern
type Page struct {
Name string
URLPattern string
BackendURLPattern string
Template string
Layout string
CacheTTL string
Header string
IsArray bool
Extra map[string]interface{}
}
// New creates a gin engine with the default Factory
func New(cfgPath string, devel bool) (*gin.Engine, error) {
return DefaultFactory.New(cfgPath, devel)
}
// Backend defines the signature of the function that creates a response for a request
// to a given backend
type Backend func(params map[string]string, headers map[string]string, c *gin.Context) (*http.Response, error)
// Renderer defines the interface for the template renderers
type Renderer interface {
Render(io.Writer, interface{}) error
}
// RendererFunc is a function implementing the Renderer interface
type RendererFunc func(io.Writer, interface{}) error
// Render implements the Renderer interface
func (rf RendererFunc) Render(w io.Writer, v interface{}) error { return rf(w, v) }
// Subscription is a struct to be used to be notified after a change in the watched renderer
type Subscription struct {
// Name is the name to watch
Name string
// In is the channel where the new renderer should be sent after a change
In chan Renderer
}
// ErrorRenderer is a renderer that always returns the injected error
type ErrorRenderer struct {
Error error
}
// Render implements the Renderer interface by returning the injected error
func (r ErrorRenderer) Render(_ io.Writer, _ interface{}) error { return r.Error }
// ErrNoResponseGeneratorDefined is the error returned when no ResponseGenerator has been defined
var ErrNoResponseGeneratorDefined = fmt.Errorf("no response generator defined")
// ErrNoBackendDefined is the error returned when no Backend has been defined
var ErrNoBackendDefined = fmt.Errorf("no backend defined")
// ErrNoRendererDefined is the error returned when no Renderer has been defined
var ErrNoRendererDefined = fmt.Errorf("no rendered defined")
// EmptyRenderer is the Renderer to be use if no other is defined
var EmptyRenderer = ErrorRenderer{ErrNoRendererDefined}
var newrelicApp *newrelic.Application