Get basic user profiles set up
This commit is contained in:
parent
5d2e3f9f28
commit
017861422b
3 changed files with 58 additions and 2 deletions
4
main.go
4
main.go
|
@ -9,6 +9,7 @@ import (
|
|||
|
||||
"git.minhas.io/asara/sudoscientist/packages/auth"
|
||||
"git.minhas.io/asara/sudoscientist/packages/database"
|
||||
"git.minhas.io/asara/sudoscientist/packages/users"
|
||||
"github.com/dgrijalva/jwt-go"
|
||||
"github.com/go-chi/chi"
|
||||
"github.com/go-chi/chi/middleware"
|
||||
|
@ -22,12 +23,13 @@ func main() {
|
|||
defer db.Close()
|
||||
auth.DB = db
|
||||
auth.Init()
|
||||
users.DB = db
|
||||
users.Init()
|
||||
|
||||
// initiate jwt token
|
||||
auth.TokenAuth = jwtauth.New("HS256", []byte("secret"), nil)
|
||||
_, tokenString, _ := auth.TokenAuth.Encode(jwt.MapClaims{"asara": 123})
|
||||
log.Printf("DEBUG: a sample jwt is %s\n\n", tokenString)
|
||||
|
||||
// initiate the routes
|
||||
router := Routes()
|
||||
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"fmt"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"git.minhas.io/asara/sudoscientist/packages/users"
|
||||
"github.com/dgrijalva/jwt-go"
|
||||
"github.com/go-chi/chi"
|
||||
"github.com/go-chi/jwtauth"
|
||||
|
@ -18,8 +19,8 @@ var (
|
|||
)
|
||||
|
||||
type Credentials struct {
|
||||
Password string `json:"password", db:"password"`
|
||||
Username string `json:"username", db:"username"`
|
||||
Password string `json:"password", db:"password"`
|
||||
}
|
||||
|
||||
func Init() {
|
||||
|
@ -48,6 +49,7 @@ func Signup(w http.ResponseWriter, r *http.Request) {
|
|||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
users.CreateProfile(creds.Username)
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
}
|
||||
|
||||
|
|
52
packages/users/users.go
Normal file
52
packages/users/users.go
Normal file
|
@ -0,0 +1,52 @@
|
|||
package users
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"database/sql"
|
||||
"github.com/go-chi/chi"
|
||||
"github.com/go-chi/render"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
var (
|
||||
DB *sql.DB
|
||||
)
|
||||
|
||||
type User struct {
|
||||
Username string `json:"username,string"`
|
||||
Country string `json:"country,string"`
|
||||
Bio string `json:"bio,string"`
|
||||
}
|
||||
|
||||
func Init() {
|
||||
dbCreateStatement := `
|
||||
CREATE TABLE IF NOT EXISTS user_profiles
|
||||
(id SERIAL PRIMARY KEY,
|
||||
username text REFERENCES users (username),
|
||||
country text,
|
||||
bio text)`
|
||||
DB.Exec(dbCreateStatement)
|
||||
}
|
||||
|
||||
func Routes() *chi.Mux {
|
||||
router := chi.NewRouter()
|
||||
router.Get("/{username}", GetUser)
|
||||
return router
|
||||
}
|
||||
|
||||
func GetUser(w http.ResponseWriter, r *http.Request) {
|
||||
username := chi.URLParam(r, "username")
|
||||
user := User{
|
||||
Username: username,
|
||||
}
|
||||
render.JSON(w, r, user) // A chi router helper for serializing and returning json
|
||||
}
|
||||
|
||||
func CreateProfile(username string) {
|
||||
fmt.Println("CREATING PROFILE")
|
||||
blankProfileStatement := `
|
||||
INSERT INTO user_profiles (username, country, bio)
|
||||
VALUES ($1, $2, $3)`
|
||||
DB.Exec(blankProfileStatement, username, "", "")
|
||||
fmt.Println("CREATED")
|
||||
}
|
Reference in a new issue