Skip to content

Commit

Permalink
Merge pull request #55 from USEPA/open-api-server-url
Browse files Browse the repository at this point in the history
Fixed Swagger UI not working
  • Loading branch information
jthomp13 authored Jun 17, 2020
2 parents 31a3643 + 2264915 commit 1c430f2
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 206 deletions.
140 changes: 0 additions & 140 deletions api-revampd.json

This file was deleted.

16 changes: 8 additions & 8 deletions html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -257,15 +257,15 @@ <h3>Some Links</h3>
<div class="jumbotron text-center" style="margin-bottom:0">
<p>Footer</p>
</div>
<script>
<!--# if expr="$host = localhost" -->
var apiUrl = "http://localhost:8080"
<!--# else -->
var apiUrl="https://revampd-api.app.cloud.gov"
<!--# endif -->
</script>
<script src="https://unpkg.com/vue"></script>
<script src = "https://unpkg.com/axios/dist/axios.min.js"> </script>
<script>
<!--# if expr="$host = localhost" -->
const API_URL = "http://localhost:8080"
<!--# else -->
const API_URL ="https://revampd-api.app.cloud.gov"
<!--# endif -->
</script>
<script src = "src/app.js"></script>
<!--Footer-->
<!-- start line 112 OneEPA-->
Expand Down Expand Up @@ -334,4 +334,4 @@ <h3>Some Links</h3>
</footer>
<!--end line 174 OneEPA-->
</body>
</html>
</html>
4 changes: 3 additions & 1 deletion html/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
this.getUnitData("/units/findByOperatingYear",this.selected);
},
methods: {

getUnitData(section, year) {
let url = apiUrl+section+"?operatingYear="+ year;
let url = API_URL+section+"?operatingYear="+ year;

console.log(url);
axios.get(url).then(response => {
console.log(response.data);
Expand Down
12 changes: 10 additions & 2 deletions html/swaggerUI/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,17 @@
<script src="./swagger-ui-standalone-preset.js"> </script>
<script>
window.onload = function() {
// Begin Swagger UI call region

<!--# if expr="$host = localhost" -->
const API_URL = "http://localhost:8080"
<!--# else -->
const API_URL ="https://revampd-api.app.cloud.gov"
<!--# endif -->

var apiSpecUrl = API_URL + "/swagger/api-revampd.json";

const ui = SwaggerUIBundle({
url: "http://localhost:9080/swaggerUI/api-revampd.json",
url: apiSpecUrl,
dom_id: '#swagger-ui',
deepLinking: true,
presets: [
Expand Down
44 changes: 0 additions & 44 deletions html/swaggerUI/nginnx.conf

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@
"title": "RevAMPD Open API Spec 3.0",
"version": "1.0"
},
"servers": [
{
"url": "http://localhost:8080/"
}
],
"components": {
"schemas": {
"Unit": {
Expand Down
18 changes: 12 additions & 6 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,29 @@ func findUnitsByOperatingYear(w http.ResponseWriter, r *http.Request, dbService
}
}

//Allows all sites to access the API. This can be fine tuned
func enableCors(w *http.ResponseWriter) {
(*w).Header().Set("Access-Control-Allow-Origin", "*")
//Enable Cross-Origin Resource Sharing (CORS)
func enableCors(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
(w).Header().Set("Access-Control-Allow-Origin", "*")
handler.ServeHTTP(w, r)
})
}
func main() {

func main() {
dbService, err := CreateDatabaseService()

if err != nil {
log.Fatal("Could create database service: " + err.Error())
}

http.HandleFunc("/units/findByOperatingYear", func(w http.ResponseWriter, r *http.Request) {
enableCors(&w)
findUnitsByOperatingYear(w, r, dbService)
})

fs := http.FileServer(http.Dir("./api-spec"))
http.Handle("/swagger/", http.StripPrefix("/swagger", fs))

log.Debug("Starting revAMPD API backend, listening on port " + os.Getenv("PORT"))
http.ListenAndServe(":"+os.Getenv("PORT"), nil)
//Enable CORS at the server level
http.ListenAndServe(":"+os.Getenv("PORT"), enableCors(http.DefaultServeMux))
}

0 comments on commit 1c430f2

Please sign in to comment.