-
Notifications
You must be signed in to change notification settings - Fork 128
/
Copy pathweb.go-example
75 lines (62 loc) · 1.84 KB
/
web.go-example
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
package main
import (
"flag"
"os"
"net/http"
"strings"
"text/template"
"github.com/fvbock/endless"
log "github.com/mgutz/logxi/v1"
)
var host = flag.String("host", "0.0.0.0", "Host")
var port = flag.String("port", "8080", "Port")
var staticContent = flag.String("staticPath", "./swagger-ui", "Path to folder with Swagger UI")
var apiurl = flag.String("api", "http://127.0.0.1", "The base path URI of the API service")
func IndexHandler(w http.ResponseWriter, r *http.Request) {
isJsonRequest := false
if acceptHeaders, ok := r.Header["Accept"]; ok {
for _, acceptHeader := range acceptHeaders {
if strings.Contains(acceptHeader, "json") {
isJsonRequest = true
break
}
}
}
if isJsonRequest {
w.Write([]byte(resourceListingJson))
} else {
http.Redirect(w, r, "/swagger-ui/", http.StatusFound)
}
}
func ApiDescriptionHandler(w http.ResponseWriter, r *http.Request) {
apiKey := strings.Trim(r.RequestURI, "/")
if json, ok := apiDescriptionsJson[apiKey]; ok {
t, e := template.New("desc").Parse(json)
if e != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
t.Execute(w, *apiurl)
} else {
w.WriteHeader(http.StatusNotFound)
}
}
func main() {
flag.Parse()
// To serve a directory on disk (/tmp) under an alternate URL
// path (/tmpfiles/), use StripPrefix to modify the request
// URL's path before the FileServer sees it:
http.HandleFunc("/", IndexHandler)
http.Handle("/swagger-ui/", http.StripPrefix("/swagger-ui/", http.FileServer(http.Dir(*staticContent))))
for apiKey, _ := range apiDescriptionsJson {
http.HandleFunc("/"+apiKey+"/", ApiDescriptionHandler)
}
listenTo := *host + ":" + *port
log.Info("Star listen to %s", listenTo)
err := endless.ListenAndServe(listenTo, http.DefaultServeMux)
if err != nil {
log.Error(err.Error())
}
log.Info("Server on %s stopped", listenTo)
os.Exit(0)
}