36 lines
839 B
Go
36 lines
839 B
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"runtime"
|
|
|
|
"git.satori.love/gameui/webp/api"
|
|
"github.com/graphql-go/graphql"
|
|
)
|
|
|
|
func main() {
|
|
runtime.GOMAXPROCS(runtime.NumCPU())
|
|
|
|
schema, err := api.NewSchema()
|
|
if err != nil {
|
|
log.Fatalln("创建Schema失败", err)
|
|
}
|
|
http.HandleFunc("/api", func(w http.ResponseWriter, r *http.Request) {
|
|
query := r.URL.Query().Get("query")
|
|
params := graphql.Params{Schema: schema, RequestString: query}
|
|
result := graphql.Do(params)
|
|
if len(result.Errors) > 0 {
|
|
fmt.Printf("failed to execute graphql operation, errors: %+v", result.Errors)
|
|
http.Error(w, result.Errors[0].Error(), 500)
|
|
return
|
|
}
|
|
rJSON, _ := json.MarshalIndent(result.Data, "", " ")
|
|
w.Write(rJSON)
|
|
})
|
|
fmt.Println("Now server is running on port 6001")
|
|
http.ListenAndServe(":6001", nil)
|
|
}
|