Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add endpoints for OSM oauth2 #36

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 45 additions & 5 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -1,5 +1,45 @@
HOST_IP=localhost OR example.com OR beforeafter.baato.io
HOST_PROTOCOL=http: OR https:
GMAIL_USERNAME=EMAIL_ADDRESS
GMAIL_PASSWORD=EMAIL_PASSWORD
MAIL_CC=EMAIL_ADDRESS_TO_ADD_IN_CC
# Before-After configuration file

# Copy to `before-after.env` and adjust to make it work!
#
HOST_IP=localhost
HOST_PROTOCOL=http:
API_BASE_URL=0.0.0.0:8848

# Secret used to create JWT token. Must be at least 32 char
APP_SECRET=rnpNmYdj7P12t8VzcNh8IxfAE2YdFiNr

# Comment below variable to use in development environment (optional)
GIN_MODE=release
####################################################
#
# DATABASE CONNECTION PARAMETRS
#
####################################################
# The connection to the postgres database (required)
POSTGRES_HOST=postgres
POSTGRES_PORT=5432
POSTGRES_DB=before-after
POSTGRES_USER=baato
POSTGRES_PASSWORD=baato
POSTGRES_SSL_MODE=disable
DB_DRIVER=postgres

# Email sending server configuration
# This is required in order to send out messages.
#
SMTP_HOST=smtp.gmail.com
SMTP_PORT=587
# Should be address from which mail is sent
[email protected]
# Should be app password instead of password if you are using gmail smtp,
SMTP_PASSWORD=foo
[email protected]

# OpenStreetMap OAuth2 configuration for osm login (required)
#
OAUTH_CLIENT_ID=foo
OAUTH_CLIENT_SECRET=foo
OAUTH_SCOPES=read_prefs write_api
OAUTH_REDIRECT_URL=http://127.0.0.1:8080/authorized
OAUTH_SERVER_URL=https://www.openstreetmap.org
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
.env
before-after.env
23 changes: 14 additions & 9 deletions backend/Dockerfile → Dockerfile.backend
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,36 @@ RUN apt-get install -y osmosis
RUN apt-get install -y python3-pip python-dev build-essential
RUN pip install mbutil

RUN curl -s https://packagecloud.io/install/repositories/golang-migrate/migrate/script.deb.sh | bash
RUN apt-get update
RUN apt-get install -y migrate

COPY tilemaker-configs/config.json /
COPY tilemaker-configs/process.lua /
COPY backend/tilemaker-configs/config.json /
COPY backend/tilemaker-configs/process.lua /

COPY index.html /index.html
COPY backend/index.html /index.html

RUN mkdir -p /server/backend/server
COPY server/ /server/backend/server
COPY backend/server/ /server/backend/server
RUN chmod +x /server/backend/server/main.go

COPY start.sh /start.sh
COPY backend/start.sh /start.sh
COPY backend/migrate.sh /migrate.sh

RUN mkdir -p /provisioning-scripts
COPY provisioning-scripts/ /provisioning-scripts
COPY backend/provisioning-scripts/ /provisioning-scripts

RUN mkdir -p /map-styles
COPY map-styles/ /map-styles
COPY backend/map-styles/ /map-styles

RUN chmod +x /start.sh
RUN chmod +x /migrate.sh
RUN chmod +x /provisioning-scripts/*.sh

WORKDIR /server/backend/server

RUN go build

WORKDIR /server
COPY before-after.env .

ENTRYPOINT ["/start.sh"]
WORKDIR /server
30 changes: 30 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
backend:
docker build -t before-after_backend:latest . -f Dockerfile.backend

frontend:
docker build -t before-after_frontend:latest ./frontend

up:
docker-compose up -d

down:
docker-compose down

sqlc:
sqlc generate

initmigrate:
migrate create -ext sql -dir backend/server/db/migration -seq init_schema

dbupgrade:
migrate -path db/migration -database "postgresql://baato:baato@localhost:5432/beforeafter?sslmode=disable" -verbose up

dbdowngrade:
migrate -path db/migration -database "postgresql://baato:baato@localhost:5432/beforeafter?sslmode=disable" -verbose down

server:
cd backend/server/
go run main.go


.PHONY: backend frontend up down initmigrate dbupgrade dbdowngrade sqlc server
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Technical stack for generating before-after map (with vector tiles).
# Installation and Setup

- Clone the repository
- ` cp .env.sample .env`
- `cp .env.sample before-after.env`
- Make required changes in .env file
- `docker-compose build`
- `docker-compose up`
Expand All @@ -19,11 +19,16 @@ Technical stack for generating before-after map (with vector tiles).
- Frontend: It houses the main provisioning interface.

# Developing the Backend

- `docker build` after making changes
- `docker up` to test the changes
- `make build` after making changes
- `make up` to test the changes

# Developing the Frontend
#### With Docker
- `make frontend` after making changes
- `make up`

#### Without Docker
- `yarn install` inside frontend/src/app
- `yarn serve`

For detailed development setup instructions have a look at our [development setup guide](development-setup.md)
2 changes: 2 additions & 0 deletions backend/migrate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
migrate -path ./backend/server/db/migration -database ${DBSource} -verbose up
62 changes: 62 additions & 0 deletions backend/server/api/oauth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package api

import (
"fmt"
"net/http"

"github.com/baato/before-after/services"
"github.com/baato/before-after/util"
"github.com/gin-gonic/gin"
"golang.org/x/oauth2"
"google.golang.org/appengine/log"
)

func (server *Server) login(ctx *gin.Context) {
// TODO: Generate random state
state := util.RandomState()
url := server.config.OAUTHConfig.AuthCodeURL(state)
ctx.JSON(
http.StatusOK,
gin.H{"url": url, "state": state},
)
}

func (server *Server) callback(ctx *gin.Context) {

// Exchange the authorization code for an access token.
code := ctx.Query("code")
token, err := server.config.OAUTHConfig.Exchange(oauth2.NoContext, code)
if err != nil {
ctx.JSON(
http.StatusInternalServerError,
gin.H{"error": "error while exchanging code with access token"},
)
return
}

// Fetch user details from OSM API using the access token.
user_info, err := services.GetOSMUser(token.AccessToken)
if err != nil {
ctx.JSON(
http.StatusInternalServerError,
gin.H{"error": fmt.Sprintf("error getting user details: %v", err)},
)
log.Errorf(ctx, "error getting user details: %v", err)
return
}

// Create/update user in database and return logged in user details.
loggedInUserResponse, err := services.LoginUser(server.query, &user_info)
if err != nil {
ctx.JSON(
http.StatusInternalServerError,
gin.H{"error": fmt.Sprintf("error logging in %v", err)},
)
log.Errorf(ctx, "error logging in: %v", err)
return
}
ctx.JSON(
http.StatusOK,
loggedInUserResponse,
)
}
14 changes: 12 additions & 2 deletions backend/server/api/server.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package api

import (
db "github.com/baato/before-after/db/sqlc"
"github.com/baato/before-after/util"
"github.com/gin-gonic/gin"
)

Expand All @@ -18,10 +20,15 @@ var dispatcher = NewDispatcher(jobQueue, maxWorkers)

type Server struct {
router *gin.Engine
query *db.Queries
config *util.Config
}

func NewServer() *Server {
server := &Server{}
func NewServer(query *db.Queries, config *util.Config) *Server {
server := &Server{
config: config,
query: query,
}

server.setupRouter()

Expand All @@ -30,6 +37,9 @@ func NewServer() *Server {

func (server *Server) setupRouter() {
router := gin.Default()

router.GET("/api/v1/login", server.login)
router.GET("/api/v1/callback", server.callback)
router.POST("/api/v1/instance", server.RequestHandlerForAPI)

server.router = router
Expand Down
20 changes: 11 additions & 9 deletions backend/server/api/workerpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ package api
import (
"fmt"
"net/http"
"os"
"os/exec"

Mailer "github.com/baato/before-after/mailer"
"github.com/baato/before-after/services"
"github.com/gin-gonic/gin"
)

Expand All @@ -28,19 +27,22 @@ type Job struct {
type JobStatus struct{ status, err string }

func provision(year, bbox, style, name, uuid, country, continent, fullName, email string) JobStatus {
scripts_to_run := [5]string{"/provisioning-scripts/prepare-provision.sh", "/provisioning-scripts/download-data.sh", "/provisioning-scripts/generate-extracts.sh", "/provisioning-scripts/generate-tiles.sh", "/provisioning-scripts/provision.sh"}
scripts_to_run := [5]string{
"/provisioning-scripts/prepare-provision.sh",
"/provisioning-scripts/download-data.sh",
"/provisioning-scripts/generate-extracts.sh",
"/provisioning-scripts/generate-tiles.sh",
"/provisioning-scripts/provision.sh",
}

for i, s := range scripts_to_run {
// Websocket.NotifyClient(s, ws)
cmd := exec.Command("/bin/bash", s, year, bbox, style, uuid, country, name, continent)
_, err := cmd.Output()
if err != nil {
fmt.Println(fmt.Sprint(err))
return JobStatus{"error", s}
}
// Print the output
// fmt.Print(string(stdout))
fmt.Println(i, s)
fmt.Println(i+1, s)
}
return JobStatus{"done", "nil"}
}
Expand Down Expand Up @@ -73,9 +75,9 @@ func (w Worker) start() {
// Dispatcher has added a job to my jobQueue.
var jobstatus JobStatus = provision(job.Year, job.Bbox, job.Style, job.Name, job.Uuid, job.Country, job.Continent, job.FullName, job.Email)
if jobstatus.status == "done" {
Mailer.SendMail([]string{job.Email, os.Getenv("MAIL_CC")}, job.FullName, job.Uuid, job.Name)
services.SendSuccessMail(job.Email, job.FullName, job.Uuid, job.Name)
} else if jobstatus.status == "error" {
Mailer.SendErrorMail([]string{job.Email, os.Getenv("MAIL_CC")}, job.FullName, job.Uuid, jobstatus.err, job.Year, job.Bbox, job.Name, job.Country, job.Continent, job.Email)
services.SendErrorMail(job.Email, job.FullName, job.Uuid, jobstatus.err, job.Year, job.Bbox, job.Name, job.Country, job.Continent, job.Email)
}

case <-w.quitChan:
Expand Down
1 change: 1 addition & 0 deletions backend/server/db/migration/000001_init_schema.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE IF EXISTS users;
9 changes: 9 additions & 0 deletions backend/server/db/migration/000001_init_schema.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CREATE TABLE "users" (
"id" int PRIMARY KEY,
"username" varchar UNIQUE NOT NULL,
"role" int NOT NULL DEFAULT 0,
"created_at" timestamptz NOT NULL DEFAULT (now()),
"email_address" varchar,
"is_email_verified" boolean DEFAULT false,
"picture_url" varchar
);
35 changes: 35 additions & 0 deletions backend/server/db/query/user.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
-- name: CreateUser :one
INSERT INTO users (
id, username, picture_url
) VALUES (
$1, $2, $3
)
RETURNING *;

-- name: GetUser :one
SELECT * FROM users
WHERE id = $1 LIMIT 1;


-- name: GetUserByUsername :one
SELECT * FROM users
WHERE username = $1 LIMIT 1;

-- name: ListUsers :many
SELECT * FROM users
ORDER BY username ASC
LIMIT $1
OFFSET $2;

-- name: UpdateUser :one
UPDATE users SET
username = COALESCE($2, username),
role = COALESCE($3, role),
email_address = COALESCE($4, email_address),
is_email_verified = COALESCE($5, is_email_verified),
picture_url = COALESCE($6, picture_url)
WHERE id = $1
RETURNING *;

-- name: DeleteUser :exec
DELETE FROM users WHERE id = $1;
31 changes: 31 additions & 0 deletions backend/server/db/sqlc/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading