Created
March 1, 2016 21:26
-
-
Save hectorgool/a6d6d7cd296861b4a42a to your computer and use it in GitHub Desktop.
API Rest
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
| /* | |
| go ftm postgresql_api.go | |
| go build postgresql_api.go | |
| install: | |
| go get github.com/lib/pq | |
| CREATE DATABASE "social_network" WITH ENCODING 'UTF8'; | |
| GRANT ALL ON DATABASE "social_network" TO santo; | |
| CREATE SEQUENCE users_id_seq | |
| START WITH 1 | |
| INCREMENT BY 1 | |
| NO MAXVALUE | |
| NO MINVALUE | |
| CACHE 1; | |
| DROP TABLE users; | |
| CREATE TABLE users ( | |
| user_id smallint NOT NULL DEFAULT nextval('users_id_seq'), | |
| user_nickname text, | |
| user_first text, | |
| user_last text, | |
| user_email text | |
| ); | |
| ALTER TABLE public.users_id_seq OWNER TO santo; | |
| ALTER TABLE public.users OWNER TO santo; | |
| http://localhost:8080/api/user/create?user=santo&first=Rodolfo&last=Guzman&email=santo@gmail.com | |
| http://localhost:8080/api/users | |
| */ | |
| package main | |
| import ( | |
| "database/sql" | |
| "encoding/json" | |
| "fmt" | |
| //_ "github.com/go-sql-driver/mysql" | |
| "github.com/gorilla/mux" | |
| _ "github.com/lib/pq" | |
| "log" | |
| "net/http" | |
| ) | |
| var database *sql.DB | |
| type Users struct { | |
| Users []User `json:"users"` | |
| } | |
| type User struct { | |
| ID int "json:id" | |
| Name string "json:username" | |
| Email string "json:email" | |
| First string "json:first" | |
| Last string "json:last" | |
| } | |
| func UserCreate(w http.ResponseWriter, r *http.Request) { | |
| NewUser := User{} | |
| NewUser.Name = r.FormValue("user") | |
| NewUser.Email = r.FormValue("email") | |
| NewUser.First = r.FormValue("first") | |
| NewUser.Last = r.FormValue("last") | |
| output, err := json.Marshal(NewUser) | |
| fmt.Println(string(output)) | |
| if err != nil { | |
| fmt.Println("Something went wrong!") | |
| } | |
| q, err := database.Exec("INSERT INTO users(user_nickname,user_first,user_last,user_email) VALUES($1,$2,$3,$4)", NewUser.Name, NewUser.First, NewUser.Last, NewUser.Email) | |
| if err != nil { | |
| fmt.Println(err) | |
| } | |
| fmt.Println(q) | |
| } | |
| func UsersRetrieve(w http.ResponseWriter, r *http.Request) { | |
| log.Println("starting retrieval") | |
| start := 0 | |
| limit := 10 | |
| next := start + limit | |
| w.Header().Set("Pragma", "no-cache") | |
| w.Header().Set("Link", "<http://localhost:8080/api/users?start="+string(next)+"; rel=\"next\"") | |
| rows, _ := database.Query("select * from users LIMIT 10") | |
| Response := Users{} | |
| for rows.Next() { | |
| user := User{} | |
| rows.Scan(&user.ID, &user.Name, &user.First, &user.Last, &user.Email) | |
| Response.Users = append(Response.Users, user) | |
| } | |
| output, _ := json.Marshal(Response) | |
| fmt.Fprintln(w, string(output)) | |
| } | |
| func main() { | |
| //db, err := sql.Open("mysql", "root@/social_network") | |
| //db, err := sql.Open("postgres", "user=hector dbname=social_network passwd=asdfasdf sslmode=verify-full") | |
| db, err := sql.Open("postgres", "postgres://santo:asdfadsf@localhost/social_network") | |
| if err != nil { | |
| } | |
| database = db | |
| routes := mux.NewRouter() | |
| routes.HandleFunc("/api/user/create", UserCreate).Methods("POST") | |
| routes.HandleFunc("/api/users", UsersRetrieve).Methods("GET") | |
| http.Handle("/", routes) | |
| http.ListenAndServe(":8080", nil) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment