Created
October 2, 2019 02:49
-
-
Save anta40/26c589234c3ab68c5530c76adeb85387 to your computer and use it in GitHub Desktop.
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" | |
"encoding/json" | |
"fmt" | |
"log" | |
"net/http" | |
"github.com/gorilla/handlers" | |
"github.com/gorilla/mux" | |
_ "github.com/lib/pq" | |
) | |
const ( | |
host = "localhost" | |
port = 2019 | |
user = "postgres" | |
password = "pg2019" | |
dbname = "dbdev01" | |
) | |
type NullString struct { | |
sql.NullString | |
} | |
func (ns *NullString) MarshalJSON() ([]byte, error) { | |
if !ns.Valid { | |
return []byte("null"), nil | |
} | |
return json.Marshal(ns.String) | |
} | |
type LogItem struct { | |
UserId string `form:"userid" json:"userid"` | |
UserDate string `form:"date" json:"date"` | |
CheckinTime NullString `form:"checkintime" json:"checkintime"` | |
CheckinLocation NullString `form:"checkinlocation" json:"checkinlocation"` | |
CheckoutTime NullString `form:"checkouttime" json:"checkouttime"` | |
CheckoutLocation NullString `form:"checkoutlocation" json:"checkoutlocation"` | |
CheckinDistance int `form:"checkindistance" json:"checkindistance"` | |
CheckoutDistance int `form:"checkoutdistance" json:"checkoutdistance"` | |
} | |
type Response struct { | |
Status int `json:"status"` | |
Message string `json:"message"` | |
Data []LogItem | |
} | |
func main() { | |
headersOk := handlers.AllowedHeaders([]string{"X-Requested-With", "Content-Type"}) | |
originsOk := handlers.AllowedOrigins([]string{"*"}) | |
methodsOk := handlers.AllowedMethods([]string{"GET", "HEAD", "POST", "PUT", "OPTIONS"}) | |
router := mux.NewRouter() | |
router.HandleFunc("/all", getAllUsers).Methods("GET") | |
http.Handle("/", router) | |
fmt.Println("Connected to port 1234...") | |
log.Fatal(http.ListenAndServe(":1234", handlers.CORS(originsOk, headersOk, methodsOk)(router))) | |
} | |
func DBconnect() *sql.DB { | |
psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+ | |
"password=%s dbname=%s sslmode=disable", | |
host, port, user, password, dbname) | |
db, err := sql.Open("postgres", psqlInfo) | |
if err != nil { | |
log.Fatal(err) | |
} | |
return db | |
} | |
func getAllUsers(w http.ResponseWriter, r *http.Request) { | |
var logItem LogItem | |
var arrLogItem []LogItem | |
var response Response | |
db := DBconnect() | |
defer db.Close() | |
rows, err := db.Query("SELECT * FROM log") | |
if err != nil { | |
log.Print(err) | |
} | |
for rows.Next() { | |
if err := rows.Scan(&logItem.UserId, &logItem.UserDate, &logItem.CheckinTime, &logItem.CheckinLocation, &logItem.CheckoutTime, &logItem.CheckoutLocation, &logItem.CheckinDistance, &logItem.CheckoutDistance); err != nil { | |
log.Fatal(err.Error()) | |
} else { | |
arrLogItem = append(arrLogItem, logItem) | |
} | |
} | |
response.Status = 200 | |
response.Message = "Success" | |
response.Data = arrLogItem | |
w.Header().Set("Content-Type", "application/json") | |
json.NewEncoder(w).Encode(response) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment