-
Notifications
You must be signed in to change notification settings - Fork 55
/
main.go
121 lines (100 loc) · 3.35 KB
/
main.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
117
118
119
120
121
package main
import (
"flag"
"log"
"net/http"
"path/filepath"
restfulspec "github.com/emicklei/go-restful-openapi/v2"
restful "github.com/emicklei/go-restful/v3"
"github.com/emicklei/mora/api/documents"
"github.com/emicklei/mora/api/statistics"
"github.com/emicklei/mora/session"
"github.com/go-openapi/spec"
"github.com/magiconair/properties"
)
var (
props *properties.Properties
propertiesFile = flag.String("config", "mora.properties", "the configuration file")
SwaggerPath string
MoraIcon string
)
func main() {
flag.Parse()
// Load configurations from a file
info("loading configuration from [%s]", *propertiesFile)
var err error
if props, err = properties.LoadFile(*propertiesFile, properties.UTF8); err != nil {
log.Fatalf("[mora][error] Unable to read properties:%v\n", err)
}
// Swagger configuration
SwaggerPath = props.GetString("swagger.path", "")
MoraIcon = filepath.Join(SwaggerPath, "images/mora.ico")
// New, shared session manager
sessMng := session.NewSessionManager(props.FilterPrefix("mongod."))
defer sessMng.CloseAll()
// accept and respond in JSON unless told otherwise
restful.DefaultRequestContentType(restful.MIME_JSON)
restful.DefaultResponseContentType(restful.MIME_JSON)
// gzip if accepted
restful.DefaultContainer.EnableContentEncoding(true)
// faster router
restful.DefaultContainer.Router(restful.CurlyRouter{})
// no need to access body more than once
// restful.DefaultContainer. SetCacheReadEntity(false)
// API Cross-origin requests
apiCors := props.GetBool("http.server.cors", false)
// Documents API
documents.Register(sessMng, restful.DefaultContainer, apiCors)
// Statistics API
if ok := props.GetBool("mora.statistics.enabl`e", false); ok {
statistics.Register(sessMng, restful.DefaultContainer)
}
addr := props.MustGet("http.server.host") + ":" + props.MustGet("http.server.port")
basePath := "http://" + addr
config := restfulspec.Config{
WebServices: restful.RegisteredWebServices(), // you control what services are visible
APIPath: "/apidocs.json",
PostBuildSwaggerObjectHandler: enrichSwaggerObject}
restful.DefaultContainer.Add(restfulspec.NewOpenAPIService(config))
http.HandleFunc("/", index)
// Serve favicon.ico
http.HandleFunc("/favion.ico", icon)
info("ready to serve on %s", basePath)
log.Fatal(http.ListenAndServe(addr, nil))
}
// If swagger is not on `/` redirect to it
func index(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, SwaggerPath, http.StatusMovedPermanently)
}
func icon(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, MoraIcon, http.StatusMovedPermanently)
}
// Log wrapper
func info(template string, values ...interface{}) {
log.Printf("[mora][info] "+template+"\n", values...)
}
func enrichSwaggerObject(swo *spec.Swagger) {
swo.Info = &spec.Info{
InfoProps: spec.InfoProps{
Title: "UserService",
Description: "Resource for managing Users",
Contact: &spec.ContactInfo{
ContactInfoProps: spec.ContactInfoProps{
Name: "john",
Email: "[email protected]",
URL: "http://johndoe.org",
},
},
License: &spec.License{
LicenseProps: spec.LicenseProps{
Name: "MIT",
URL: "http://mit.org",
},
},
Version: "1.0.0",
},
}
swo.Tags = []spec.Tag{{TagProps: spec.TagProps{
Name: "users",
Description: "Managing users"}}}
}