Skip to content

Instantly share code, notes, and snippets.

@jotak
Created February 20, 2018 15:34
Show Gist options
  • Save jotak/057fd291126cb2087bade4da58334e77 to your computer and use it in GitHub Desktop.
Save jotak/057fd291126cb2087bade4da58334e77 to your computer and use it in GitHub Desktop.
Setup server with external prometheus + CORS allowed
package main
import (
"log"
"net/http"
"time"
"github.com/swift-sunshine/swscore/config"
"github.com/swift-sunshine/swscore/routing"
)
func setupConfig() {
conf := config.NewConfig()
conf.PrometheusServiceUrl = "http://prometheus-istio-system.127.0.0.1.nip.io"
config.Set(conf)
}
// Integration test. Manual for the moment.
func main() {
setupConfig()
startServer()
// Open up your browser and hit http://127.0.0.1:8000/api/namespaces/tutorial/services/preference/metrics
}
func corsAllowed(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")
next.ServeHTTP(w, r)
})
}
func startServer() {
conf := config.Get()
router := routing.NewRouter(conf)
router.Use(corsAllowed)
srv := &http.Server{
Handler: router,
Addr: "127.0.0.1:8000",
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
log.Fatal(srv.ListenAndServe())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment