...
 
Commits (2)
......@@ -451,6 +451,10 @@ func getThumbnail(db *sql.DB, mediaURL, photoURL, thumbnailURL string) string {
return mediaURL + "/" + thumbnailURL
}
if os.Getenv("ENVIRONMENT") != "production" {
return mediaURL + "/" + thumbnailURL
}
var err error
fileExtension := filepath.Ext(photoURL)
rand.Seed(time.Now().Unix())
......
package main
import (
"fmt"
"os"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ses"
)
func sendErrorMailToAdmin(errorMessage string) {
conf := aws.Config{Region: aws.String(os.Getenv("AWS_SES_REGION"))}
awsSession := session.New(&conf)
sesSession := ses.New(awsSession)
sesEmailInput := &ses.SendEmailInput{
Destination: &ses.Destination{
ToAddresses: []*string{aws.String(os.Getenv("ADMIN_EMAIL"))},
},
Message: &ses.Message{
Body: &ses.Body{
Html: &ses.Content{
Data: aws.String(errorMessage)},
},
Subject: &ses.Content{
Data: aws.String("Error on GraphQL Server"),
},
},
Source: aws.String(os.Getenv("SERVER_EMAIL")),
ReplyToAddresses: []*string{
aws.String(os.Getenv("SERVER_EMAIL")),
},
}
_, err := sesSession.SendEmail(sesEmailInput)
if err != nil {
fmt.Println("Send e-mail to admin failed")
fmt.Println(err)
}
}
......@@ -11,7 +11,6 @@ import (
"os"
"github.com/go-redis/redis"
"github.com/graph-gophers/graphql-go/relay"
"gitlab.com/shereland/graphql/database"
)
......@@ -32,7 +31,7 @@ func main() {
w.Write(page)
}))
http.Handle("/graphql", authenticationMiddleware(&relay.Handler{Schema: schema}))
http.Handle("/graphql", authenticationMiddleware(&Handler{Schema: schema}))
log.Fatal(http.ListenAndServe(":8080", nil))
}
......
package main
import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
graphql "github.com/graph-gophers/graphql-go"
)
func MarshalID(kind string, spec interface{}) graphql.ID {
d, err := json.Marshal(spec)
if err != nil {
panic(fmt.Errorf("relay.MarshalID: %s", err))
}
return graphql.ID(base64.URLEncoding.EncodeToString(append([]byte(kind+":"), d...)))
}
func UnmarshalKind(id graphql.ID) string {
s, err := base64.URLEncoding.DecodeString(string(id))
if err != nil {
return ""
}
i := strings.IndexByte(string(s), ':')
if i == -1 {
return ""
}
return string(s[:i])
}
func UnmarshalSpec(id graphql.ID, v interface{}) error {
s, err := base64.URLEncoding.DecodeString(string(id))
if err != nil {
return err
}
i := strings.IndexByte(string(s), ':')
if i == -1 {
return errors.New("invalid graphql.ID")
}
return json.Unmarshal([]byte(s[i+1:]), v)
}
type Handler struct {
Schema *graphql.Schema
}
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var params struct {
Query string `json:"query"`
OperationName string `json:"operationName"`
Variables map[string]interface{} `json:"variables"`
}
if err := json.NewDecoder(r.Body).Decode(&params); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
response := h.Schema.Exec(r.Context(), params.Query, params.OperationName, params.Variables)
errorMessage := ""
for _, err := range response.Errors {
for _, path := range err.Path {
errorMessage += path.(string) + " - "
}
errorMessage += err.Message + "\n"
}
if errorMessage != "" {
go sendErrorMailToAdmin("GraphQL error\n\n" + errorMessage)
}
responseJSON, err := json.Marshal(response)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(responseJSON)
}