Last active
February 15, 2017 17:11
-
-
Save EmperorEarth/3dc27b7c68ded57684964bc485d9110c to your computer and use it in GitHub Desktop.
global db in newssite
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use `newssite`; | |
| CREATE TABLE `post` ( | |
| `id` BINARY(16) NOT NULL, | |
| `title` CHAR(50) NOT NULL, | |
| `body` TEXT NOT NULL, | |
| PRIMARY KEY (`id`) | |
| ) | |
| COLLATE='latin1_general_cs' | |
| ENGINE=InnoDB; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- -------------------------------------------------------- | |
| -- Host: 127.0.0.1 | |
| -- Server version: 10.1.16-MariaDB - mariadb.org binary distribution | |
| -- Server OS: Win64 | |
| -- HeidiSQL Version: 9.3.0.4984 | |
| -- -------------------------------------------------------- | |
| /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; | |
| /*!40101 SET NAMES utf8mb4 */; | |
| /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; | |
| /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; | |
| -- Dumping database structure for newssite | |
| DROP DATABASE IF EXISTS `newssite`; | |
| CREATE DATABASE IF NOT EXISTS `newssite` /*!40100 DEFAULT CHARACTER SET latin1 COLLATE latin1_general_cs */; | |
| USE `newssite`; | |
| -- Dumping structure for table newssite.user | |
| DROP TABLE IF EXISTS `user`; | |
| CREATE TABLE IF NOT EXISTS `user` ( | |
| `id` binary(16) NOT NULL, | |
| `username` char(50) CHARACTER SET latin2 NOT NULL | |
| ) ENGINE=InnoDB DEFAULT CHARSET=latin1; | |
| -- Data exporting was unselected. | |
| -- Dumping structure for table newssite.user_optional_field | |
| DROP TABLE IF EXISTS `user_optional_field`; | |
| CREATE TABLE IF NOT EXISTS `user_optional_field` ( | |
| `id` binary(16) NOT NULL, | |
| `optional_field` char(50) COLLATE latin1_general_cs NOT NULL | |
| ) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_cs; | |
| -- Data exporting was unselected. | |
| /*!40101 SET SQL_MODE=IFNULL(@OLD_SQL_MODE, '') */; | |
| /*!40014 SET FOREIGN_KEY_CHECKS=IF(@OLD_FOREIGN_KEY_CHECKS IS NULL, 1, @OLD_FOREIGN_KEY_CHECKS) */; | |
| /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "database/sql" | |
| "io/ioutil" | |
| "log" | |
| "net/http" | |
| "os" | |
| _ "github.com/go-sql-driver/mysql" | |
| graphql "github.com/neelance/graphql-go" | |
| "github.com/neelance/graphql-go/relay" | |
| ) | |
| var db *sql.DB | |
| func main() { | |
| defer db.Close() | |
| rawSchema, err := ioutil.ReadFile("./../dev/schema.graphql") | |
| if err != nil { | |
| log.Fatalf("Failed to read in raw schema: %s", err) | |
| } | |
| parsedSchema, err := graphql.ParseSchema(string(rawSchema), NewResolver()) | |
| if err != nil { | |
| log.Fatalf("Failed to parse raw schema: %s", err) | |
| } | |
| // http.Handle("/graphql", injectViewerToContext(&relay.Handler{Schema: parsedSchema})) | |
| http.Handle("/graphql", &relay.Handler{Schema: parsedSchema}) | |
| if os.Getenv("production") != "true" { | |
| http.HandleFunc("/", graphiqlHandler) | |
| } else { | |
| http.Handle("/", http.HandlerFunc(SNFHandler)) | |
| } | |
| http.ListenAndServe(":8080", nil) | |
| } | |
| func init() { | |
| dbUser := os.Getenv("dbUser") | |
| dbPass := os.Getenv("dbPass") | |
| if len(dbUser) == 0 || len(dbPass) == 0 { | |
| log.Fatalln("Incorrect database env vars") | |
| } | |
| var err error | |
| db, err = sql.Open("mysql", dbUser+":"+dbPass+"@/newssite?parseTime=true") | |
| if err != nil { | |
| log.Fatalf("Failed to connect to MariaDB: %s", err) | |
| } | |
| err = db.Ping() | |
| if err != nil { | |
| db.Close() | |
| log.Fatalf("Failed to ping MariaDB: %s", err) | |
| } | |
| } | |
| func SNFHandler(w http.ResponseWriter, r *http.Request) { | |
| w.WriteHeader(http.StatusBadRequest) | |
| return | |
| } | |
| // func injectViewerToContext(next http.Handler) http.Handler { | |
| // return func(w http.ResponseWriter, r *http.Request) { | |
| // next.ServeHTTP(w, r.WithContext(context.WithValue(r.Context(), "Authorization", r.Header.Get("Authorization")))) | |
| // } | |
| // } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| graphql "github.com/neelance/graphql-go" | |
| uuid "github.com/satori/go.uuid" | |
| ) | |
| type PostResolver struct { | |
| id uuid.UUID | |
| } | |
| func NewPostResolver(idString string) *PostResolver { | |
| id, _ := uuid.FromString(idString) | |
| return &PostResolver{ | |
| id: id, | |
| } | |
| } | |
| func (r *PostResolver) ID() graphql.ID { | |
| return graphql.ID(r.id.String()) | |
| } | |
| func (r *PostResolver) Title() string { | |
| var title string | |
| _ = db.QueryRow(fmt.Sprintf("SELECT title FROM post WHERE id='%s'", r.id.Bytes())).Scan(&title) | |
| return title | |
| } | |
| func (r *PostResolver) Body() string { | |
| var body string | |
| _ = db.QueryRow(fmt.Sprintf("SELECT body FROM post WHERE id='%s'", r.id.Bytes())).Scan(&body) | |
| return body | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| uuid "github.com/satori/go.uuid" | |
| ) | |
| type Resolver struct{} | |
| func NewResolver() *Resolver { | |
| return &Resolver{} | |
| } | |
| func (r *Resolver) User(args *struct{ ID string }) *UserResolver { | |
| return NewUserResolver(args.ID) | |
| } | |
| func (r *Resolver) UserByUsername(args *struct{ Username string }) *UserByUsernameResolver { | |
| return NewUserByUsernameResolver(args.Username) | |
| } | |
| func (r *Resolver) CreateUser(args *struct { | |
| Username string | |
| OptionalField *string | |
| }) *UserResolver { | |
| tx, _ := db.Begin() | |
| id := uuid.NewV1() | |
| _, _ = tx.Exec(fmt.Sprintf("INSERT INTO user (id, username) VALUES ('%s', '%s')", id.Bytes(), args.Username)) | |
| if args.OptionalField != nil { | |
| _, _ = tx.Exec(fmt.Sprintf("INSERT INTO user_optional_field (id, optional_field) VALUES ('%s', '%s')", id.Bytes(), *args.OptionalField)) | |
| } | |
| _ = tx.Commit() | |
| return NewUserResolver(id.String()) | |
| } | |
| func (r *Resolver) Post(args *struct{ ID string }) *PostResolver { | |
| return NewPostResolver(args.ID) | |
| } | |
| func (r *Resolver) CreatePost(args *struct{ Title, Body string }) *PostResolver { | |
| id := uuid.NewV1() | |
| _, _ = db.Exec(fmt.Sprintf("INSERT INTO post (id, title, body) VALUES ('%s', '%s', '%s')", id.Bytes(), args.Title, args.Body)) | |
| return NewPostResolver(id.String()) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| schema { | |
| query: Query | |
| mutation: Mutation | |
| } | |
| # The query type, represents all of the entry points into our object graph | |
| type Query { | |
| user(id: ID!): User | |
| userByUsername(username: String!): User | |
| post(id: ID!): Post | |
| } | |
| # The mutation type, represents all updates we can make on our data | |
| type Mutation { | |
| createUser(username: String!, optionalField: String): User | |
| # createUser(userName: String!, password: String!, firstName: String!, lastName: String!, email: String!): User | |
| createPost(title: String!, body: String!): Post | |
| } | |
| type User { | |
| id: ID! | |
| username: String! | |
| # userName: String! | |
| optionalField: String | |
| # firstName: String! | |
| # lastName: String! | |
| # email: String! | |
| # password: String! | |
| } | |
| type Post { | |
| id: ID! | |
| title: String! | |
| body: String! | |
| # creator: User! | |
| #comments: [Comment!] | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| graphql "github.com/neelance/graphql-go" | |
| uuid "github.com/satori/go.uuid" | |
| ) | |
| // // User is a site user | |
| // type User struct { | |
| // ID uint | |
| // Username string | |
| // FirstName string | |
| // LastName string | |
| // Email string | |
| // Password string | |
| // Active bool | |
| // Role string | |
| // } | |
| // // SetPassword creates a password and updates the user | |
| // func (u *User) SetPassword(password string) { | |
| // } | |
| // // CheckPassword checks a password against the password hash stored | |
| // // on the user object | |
| // func (u *User) CheckPassword(password string) { | |
| // } | |
| type UserResolver struct { | |
| id uuid.UUID | |
| } | |
| func NewUserResolver(idString string) *UserResolver { | |
| id, _ := uuid.FromString(idString) | |
| return &UserResolver{ | |
| id: id, | |
| } | |
| } | |
| func (r *UserResolver) ID() graphql.ID { | |
| return graphql.ID(r.id.String()) | |
| } | |
| func (r *UserResolver) Username() string { | |
| var username string | |
| _ = db.QueryRow(fmt.Sprintf("SELECT username FROM user WHERE id='%s'", r.id.Bytes())).Scan(&username) | |
| return username | |
| } | |
| func (r *UserResolver) OptionalField() (*string, error) { | |
| var optionalField string | |
| err := db.QueryRow(fmt.Sprintf("SELECT optional_field FROM user_optional_field WHERE id='%s'", r.id.Bytes())).Scan(&optionalField) | |
| return &optionalField, err | |
| } | |
| type UserByUsernameResolver struct { | |
| username string | |
| } | |
| func NewUserByUsernameResolver(username string) *UserByUsernameResolver { | |
| return &UserByUsernameResolver{ | |
| username: username, | |
| } | |
| } | |
| func (r *UserByUsernameResolver) ID() graphql.ID { | |
| var id uuid.UUID | |
| _ = db.QueryRow(fmt.Sprintf("SELECT id FROM user WHERE username='%s'", r.username)).Scan(&id) | |
| return graphql.ID(id.String()) | |
| } | |
| func (r *UserByUsernameResolver) Username() string { | |
| return r.username | |
| } | |
| func (r *UserByUsernameResolver) OptionalField() (*string, error) { | |
| var id uuid.UUID | |
| var optionalField string | |
| _ = db.QueryRow(fmt.Sprintf("SELECT id FROM user WHERE username='%s'", r.username)).Scan(&id) | |
| err := db.QueryRow(fmt.Sprintf("SELECT optional_field FROM user_optional_field WHERE id='%s'", id.Bytes())).Scan(&optionalField) | |
| return &optionalField, err | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment