2019-01-27 09:53:34 +08:00
package main
import (
2019-02-24 04:01:34 +08:00
"context"
2019-01-27 09:53:34 +08:00
"database/sql"
2019-02-24 04:01:34 +08:00
"encoding/json"
2019-01-27 09:53:34 +08:00
"log"
"net/http"
2019-02-18 23:43:00 +08:00
"os"
2019-01-27 09:53:34 +08:00
2019-02-18 23:43:00 +08:00
"github.com/joho/godotenv"
2019-01-27 09:53:34 +08:00
"github.com/julienschmidt/httprouter"
_ "github.com/lib/pq"
)
var listen string
var postgres string
func main ( ) {
2019-02-18 23:43:00 +08:00
// Load .env
err := godotenv . Load ( )
if err != nil {
log . Fatal ( "Error loading .env file" )
}
listen = os . Getenv ( "LISTEN" )
postgres = os . Getenv ( "POSTGRES" )
2019-01-27 09:53:34 +08:00
// Open postgres
log . Printf ( "connecting to postgres %s" , postgres )
db , err := sql . Open ( "postgres" , postgres )
if err != nil {
log . Fatal ( err )
}
defer db . Close ( )
// Handler
h := NewHandler ( db )
// Routes
router := httprouter . New ( )
// Users
2019-03-16 02:21:41 +08:00
router . POST ( "/user" , h . CreateUser )
2019-07-04 05:37:38 +08:00
router . GET ( "/user" , h . GetUserByPhone )
2019-02-24 04:01:34 +08:00
router . GET ( "/user/id/:user" , h . GetUser )
2019-06-18 18:53:04 +08:00
router . GET ( "/user/username/:username" , h . GetUserByUsername )
2019-06-21 11:14:07 +08:00
router . PATCH ( "/user" , h . UpdateUser )
2019-01-27 09:53:34 +08:00
// Conversations
2019-03-16 02:21:41 +08:00
router . POST ( "/user/conversation" , AuthMiddleware ( h . CreateConversation ) )
router . GET ( "/user/conversation" , AuthMiddleware ( h . GetConversations ) ) // USER MEMBER CONVERSATION
2019-02-24 04:01:34 +08:00
router . DELETE ( "/user/conversation/:conversation" , AuthMiddleware ( h . DeleteConversation ) )
2019-01-27 09:53:34 +08:00
//router.GET("/user/:user/conversation/bymembers/", h.GetConversationsByMembers) // TODO
2019-02-24 04:01:34 +08:00
router . GET ( "/user/conversation/:conversation" , AuthMiddleware ( h . GetConversation ) ) // USER MEMBER CONVERSATION
router . PATCH ( "/user/conversation/:conversation" , AuthMiddleware ( h . UpdateConversation ) ) // USER MEMBER CONVERSATION ADMIN=true -> update conversation title
2019-01-27 09:53:34 +08:00
//router.DELETE("/user/:user/conversation/:conversation", h.DeleteConversation) // USER MEMBER CONVERSATION -> delete membership
2019-03-16 02:21:41 +08:00
router . POST ( "/user/conversation/:conversation/member" , AuthMiddleware ( h . CreateConversationMember ) ) // USER MEMBER CONVERSATION ADMIN=true -> create new membership
router . GET ( "/user/conversation/:conversation/member" , AuthMiddleware ( h . GetConversationMembers ) ) // USER MEMBER CONVERSATION
2019-01-27 09:53:34 +08:00
//router.DELETE("/user/:user/conversation/:conversation/member/:member", h.DeleteConversationMember) // USER MEMBER CONVERSATION ADMIN=true -> delete membership
// Last heard
//router.GET("/user/:user/lastheard/:conversation", h.GetLastheard)
//router.PUT("/user/:user/lastheard/:conversation", h.SetLastheard)
// Contacts
2019-03-16 02:21:41 +08:00
router . POST ( "/user/contact" , AuthMiddleware ( h . CreateContact ) )
router . GET ( "/user/contact" , AuthMiddleware ( h . GetContacts ) )
2019-01-27 09:53:34 +08:00
//router.GET("/user/:user/contact/:contact", h.GetContact)
//router.DELETE("/user/:user/contact/:contact", h.DeleteContact)
//router.GET("/user/:user/contact/:contact/conversation/", h.GetContactConversations)
log . Printf ( "starting server on %s" , listen )
log . Fatal ( http . ListenAndServe ( listen , router ) )
}
2019-02-24 04:01:34 +08:00
type RawClient struct {
UserId string ` json:"userid" `
ClientId string ` json:"clientid" `
}
func AuthMiddleware ( next httprouter . Handle ) httprouter . Handle {
return func ( w http . ResponseWriter , r * http . Request , p httprouter . Params ) {
ua := r . Header . Get ( "X-User-Claim" )
if ua == "" {
http . Error ( w , http . StatusText ( http . StatusBadRequest ) , http . StatusBadRequest )
return
}
var client RawClient
err := json . Unmarshal ( [ ] byte ( ua ) , & client )
if err != nil {
http . Error ( w , http . StatusText ( http . StatusBadRequest ) , http . StatusBadRequest )
return
}
context := context . WithValue ( r . Context ( ) , "user" , client . UserId )
next ( w , r . WithContext ( context ) , p )
}
}