github.com/humans-group/gqlgen@v0.7.2/example/chat/server/server.go (about)

     1  package main
     2  
     3  import (
     4  	"log"
     5  	"net/http"
     6  	"net/url"
     7  	"time"
     8  
     9  	"github.com/99designs/gqlgen/example/chat"
    10  	"github.com/99designs/gqlgen/handler"
    11  	"github.com/gorilla/websocket"
    12  	"github.com/opentracing/opentracing-go"
    13  	"github.com/rs/cors"
    14  	"sourcegraph.com/sourcegraph/appdash"
    15  	appdashtracer "sourcegraph.com/sourcegraph/appdash/opentracing"
    16  	"sourcegraph.com/sourcegraph/appdash/traceapp"
    17  )
    18  
    19  func main() {
    20  	startAppdashServer()
    21  
    22  	c := cors.New(cors.Options{
    23  		AllowedOrigins:   []string{"http://localhost:3000"},
    24  		AllowCredentials: true,
    25  	})
    26  
    27  	http.Handle("/", handler.Playground("Todo", "/query"))
    28  	http.Handle("/query", c.Handler(handler.GraphQL(chat.NewExecutableSchema(chat.New()),
    29  		handler.WebsocketUpgrader(websocket.Upgrader{
    30  			CheckOrigin: func(r *http.Request) bool {
    31  				return true
    32  			},
    33  		}))),
    34  	)
    35  	log.Fatal(http.ListenAndServe(":8085", nil))
    36  }
    37  
    38  func startAppdashServer() opentracing.Tracer {
    39  	memStore := appdash.NewMemoryStore()
    40  	store := &appdash.RecentStore{
    41  		MinEvictAge: 5 * time.Minute,
    42  		DeleteStore: memStore,
    43  	}
    44  
    45  	url, err := url.Parse("http://localhost:8700")
    46  	if err != nil {
    47  		log.Fatal(err)
    48  	}
    49  	tapp, err := traceapp.New(nil, url)
    50  	if err != nil {
    51  		log.Fatal(err)
    52  	}
    53  	tapp.Store = store
    54  	tapp.Queryer = memStore
    55  
    56  	go func() {
    57  		log.Fatal(http.ListenAndServe(":8700", tapp))
    58  	}()
    59  	tapp.Store = store
    60  	tapp.Queryer = memStore
    61  
    62  	collector := appdash.NewLocalCollector(store)
    63  	tracer := appdashtracer.NewTracer(collector)
    64  	opentracing.InitGlobalTracer(tracer)
    65  
    66  	log.Println("Appdash web UI running on HTTP :8700")
    67  	return tracer
    68  }