-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathp418Vocabulary.go
134 lines (121 loc) · 4.77 KB
/
p418Vocabulary.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
122
123
124
125
126
127
128
129
130
131
132
133
package main
import (
"log"
"strings"
"net/http"
"github.com/gorilla/mux"
)
// MyServer struct for mux router
type MyServer struct {
r *mux.Router
}
func main() {
htmlRouter := mux.NewRouter().StrictSlash(true)
htmlRouter.HandleFunc("/voc/documentation", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "./html/voc/documentation.html")
})
htmlRouter.HandleFunc("/voc/examples", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "./html/voc/examples.html")
})
htmlRouter.HandleFunc("/voc/examples/repository/minimal", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "./html/voc/static/schema/examples/repository/minimal.jsonld")
})
htmlRouter.HandleFunc("/voc/examples/repository/full", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "./html/voc/static/schema/examples/repository/full.jsonld")
})
htmlRouter.HandleFunc("/voc/examples/dataset/minimal", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "./html/voc/static/schema/examples/resource/dataset-minimal.jsonld")
})
htmlRouter.HandleFunc("/voc/examples/dataset/full", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "./html/voc/static/schema/examples/resource/dataset-full.jsonld")
})
htmlRouter.HandleFunc("/voc/schema", Conneg)
htmlRouter.HandleFunc("/voc/schema.{ext}", Conneg)
htmlRouter.PathPrefix("/voc/static").Handler(http.StripPrefix("/voc/static", http.FileServer(http.Dir("./html/voc/static"))))
htmlRouter.HandleFunc("/voc/", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "./html/voc/index.html")
})
htmlRouter.HandleFunc("/voc/{resource}", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "./html/voc/index.html")
})
http.Handle("/", &MyServer{htmlRouter})
err := http.ListenAndServe(":9900", nil)
// http 2.0 http.ListenAndServeTLS(":443", "server.crt", "server.key", nil)
if err != nil {
log.Fatal(err)
}
}
func (s *MyServer) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
rw.Header().Set("Access-Control-Allow-Origin", "*")
rw.Header().Set("Access-Control-Allow-Methods", "POST, GET")
rw.Header().Set("Access-Control-Allow-Headers",
"Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
// Let the Gorilla work
s.r.ServeHTTP(rw, req)
}
func addDefaultHeaders(fn http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
fn(w, r)
}
}
// Conneg handles content negotiation for RDF requests
func Conneg(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
fileExtension := vars["ext"]
//log.Printf("File Extension: '" + fileExtension + "'")
switch fileExtension {
case "owl":
http.ServeFile(w, r, "./html/voc/static/schema/schema.owl")
break
case "rdf":
http.ServeFile(w, r, "./html/voc/static/schema/schema.owl")
break
case "xml":
http.ServeFile(w, r, "./html/voc/static/schema/schema.owl")
break
case "ttl":
http.ServeFile(w, r, "./html/voc/static/schema/schema.ttl")
break
case "n3":
http.ServeFile(w, r, "./html/voc/static/schema/schema.n3")
break
case "json":
http.ServeFile(w, r, "./html/voc/static/schema/schema.jsonld")
break
case "jsonld":
http.ServeFile(w, r, "./html/voc/static/schema/schema.jsonld")
break
default:
// Check for Accept header
accept := r.Header.Get("Accept")
//log.Printf("Accept: '" + accept + "'")
switch {
case CaseInsensitiveContains(accept, "application/rdf+xml"):
http.ServeFile(w, r, "./html/voc/static/schema/schema.owl")
break
case CaseInsensitiveContains(accept, "application/xml"):
http.ServeFile(w, r, "./html/voc/static/schema/schema.owl")
break
case CaseInsensitiveContains(accept, "application/rdf+turtle"):
http.ServeFile(w, r, "./html/voc/static/schema/schema.ttl")
break
case CaseInsensitiveContains(accept, "application/rdf+n3"):
http.ServeFile(w, r, "./html/voc/static/schema/schema.n3")
break
case CaseInsensitiveContains(accept, "application/json"):
http.ServeFile(w, r, "./html/voc/static/schema/schema.jsonld")
break
case CaseInsensitiveContains(accept, "application/ld+json"):
http.ServeFile(w, r, "./html/voc/static/schema/schema.jsonld")
break
default:
http.ServeFile(w, r, "./html/voc/static/schema/schema.jsonld")
break
}
}
}
func CaseInsensitiveContains(s, substr string) bool {
s, substr = strings.ToUpper(s), strings.ToUpper(substr)
return strings.Contains(s, substr)
}