Skip to content

Commit d9bb0d2

Browse files
authored
Working on API Endpoints (#4)
* README updated, docker-compose and env added
1 parent 77176be commit d9bb0d2

File tree

10 files changed

+120
-16
lines changed

10 files changed

+120
-16
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pgadmin/
2+
postgres/
3+
.env

README.md

+37
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,45 @@ What it should support:
2727
<a name="install"></a>
2828
## 1. Installation
2929

30+
>Make a .env file and config it accordingly. A sample is provided
31+
32+
### Starting the database
33+
34+
Make sure you have docker installed.
35+
36+
To verify the docker config, check the output of the following command.
37+
38+
```bash
39+
$ docker-compose config
40+
```
41+
42+
Once verified run
43+
44+
```bash
45+
$ docker-compose up
46+
```
47+
This will fire up a docker container with postgress and pg-admin.
48+
49+
### Run Migrations
50+
51+
Make sure your postgres container is running. Now run the following commands
52+
53+
```bash
54+
$ go run migrations/*.go init
55+
```
56+
This will print something like `version is 0`. If no error then continue
57+
58+
```bash
59+
$ go run migrations/*.go
60+
```
61+
62+
This will create the required tables, and migrate the version.
63+
64+
### Start the go server
65+
3066
``` go build -o build/server```
3167

68+
3269
<a name="depend"></a>
3370
## 2. Dependencies
3471

docker-compose.yml

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
version: '3.5'
2+
3+
services:
4+
postgres:
5+
container_name: postgres_container
6+
image: "postgres:latest"
7+
environment:
8+
POSTGRES_USER: ${DB_USER}
9+
POSTGRES_PASSWORD: ${DB_PASSWD}
10+
PGDATA: /data/postgres
11+
volumes:
12+
- ./postgres:/data/postgres
13+
ports:
14+
- "127.0.0.1:5432:5432"
15+
networks:
16+
- postgres
17+
restart: unless-stopped
18+
19+
pgadmin:
20+
container_name: pgadmin_container
21+
image: dpage/pgadmin4
22+
environment:
23+
PGADMIN_DEFAULT_EMAIL: ${PGADMIN_EMAIL}
24+
PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_PASSWD}
25+
volumes:
26+
- ./pgadmin:/root/.pgadmin
27+
ports:
28+
- "5050:80"
29+
networks:
30+
- postgres
31+
restart: unless-stopped
32+
33+
networks:
34+
postgres:
35+
driver: bridge

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ require (
99
github.com/gofiber/fiber v1.14.6
1010
github.com/gofiber/fiber/v2 v2.1.3
1111
github.com/gofiber/jwt/v2 v2.0.1
12+
github.com/joho/godotenv v1.3.0
1213
)

go.sum

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03
44
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
55
github.com/andybalholm/brotli v1.0.0 h1:7UCwP93aiSfvWpapti8g88vVVGp2qqtGyePsSuDafo4=
66
github.com/andybalholm/brotli v1.0.0/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y=
7+
github.com/arnabsen1729/onlinejudge-server v0.0.0-20201105181312-77176bec178b h1:nStlYO7/pkFh7prbLVnHYWlH9KBAFKsWCM/7/qVwbsY=
78
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
89
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
910
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -60,6 +61,8 @@ github.com/gorilla/schema v1.1.0/go.mod h1:kgLaKoK1FELgZqMAVxx/5cbj0kT+57qxUrAlI
6061
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
6162
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
6263
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
64+
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
65+
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
6366
github.com/klauspost/compress v1.10.7 h1:7rix8v8GpI3ZBb0nSozFRgbtXKv+hOe+qfEpZqybrAg=
6467
github.com/klauspost/compress v1.10.7/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
6568
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=

migrations/migrations.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/go-pg/migrations/v8"
99
"github.com/go-pg/pg/v10"
10+
"github.com/joho/godotenv"
1011
)
1112

1213
const usageText = `This program runs command on the db. Supported commands are:
@@ -20,12 +21,17 @@ const usageText = `This program runs command on the db. Supported commands are:
2021
`
2122

2223
func main() {
24+
//loading env variables
25+
godotenv.Load()
26+
2327
flag.Usage = usage
2428
flag.Parse()
2529

2630
db := pg.Connect(&pg.Options{
27-
User: "postgres",
28-
Database: "db",
31+
Addr: ":5432",
32+
User: os.Getenv("DB_USER"),
33+
Password: os.Getenv("DB_PASSWD"),
34+
Database: os.Getenv("DB_NAME"),
2935
})
3036

3137
oldVersion, newVersion, err := migrations.Run(db, flag.Args()...)

sample.env

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
DB_NAME=
2+
DB_USER=
3+
DB_PASSWD=
4+
PGADMIN_EMAIL=
5+
PGADMIN_PASSWD=
6+
7+
SECRET_KEY=

server/handler/user_handler.go

+10-12
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
package handler
22

33
import (
4+
"os"
45
"time"
56

67
jwt "github.com/dgrijalva/jwt-go"
78
"github.com/gofiber/fiber/v2"
9+
"github.com/joho/godotenv"
10+
"github.com/raydwaipayan/onlinejudge-server/server/types"
811
)
912

10-
// User user object
11-
type User struct {
12-
Username string `json:"username" form:"name"`
13-
Password string `json:"password" form:"password"`
14-
}
15-
16-
//Register user registration handler
13+
//Register types.User registration handler
1714
func Register(c *fiber.Ctx) error {
18-
u := new(User)
15+
u := new(types.User)
1916

2017
if err := c.BodyParser(u); err != nil {
2118
return c.SendStatus(fiber.StatusBadRequest)
@@ -24,9 +21,10 @@ func Register(c *fiber.Ctx) error {
2421
return c.SendStatus(fiber.StatusOK)
2522
}
2623

27-
//Login user login handler
24+
//Login types.User login handler
2825
func Login(c *fiber.Ctx) error {
29-
u := new(User)
26+
godotenv.Load()
27+
u := new(types.User)
3028

3129
if err := c.BodyParser(u); err != nil {
3230
return c.SendStatus(fiber.StatusBadRequest)
@@ -35,11 +33,11 @@ func Login(c *fiber.Ctx) error {
3533
token := jwt.New(jwt.SigningMethodHS256)
3634

3735
claims := token.Claims.(jwt.MapClaims)
38-
claims["name"] = u.Username
36+
claims["email"] = u.Email
3937
claims["admin"] = false
4038
claims["exp"] = time.Now().Add(time.Hour * 96).Unix()
4139

42-
t, err := token.SignedString([]byte("secret"))
40+
t, err := token.SignedString([]byte(os.Getenv("SECRET_KEY")))
4341
if err != nil {
4442
return c.SendStatus(fiber.StatusInternalServerError)
4543
}

server/router/routes.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
package router
22

33
import (
4+
"os"
5+
46
"github.com/gofiber/fiber/v2"
57
"github.com/gofiber/fiber/v2/middleware/limiter"
68
"github.com/gofiber/fiber/v2/middleware/logger"
9+
"github.com/joho/godotenv"
710

811
jwtware "github.com/gofiber/jwt/v2"
912
handler "github.com/raydwaipayan/onlinejudge-server/server/handler"
1013
)
1114

1215
//SetupRoutes initiates the fiber router
1316
func SetupRoutes(app *fiber.App) {
17+
godotenv.Load()
1418
user := app.Group("/user", logger.New(), limiter.New())
15-
1619
user.Post("/register", handler.Register)
1720
user.Post("/login", handler.Login)
1821
user.Use(jwtware.New(jwtware.Config{
1922
SigningMethod: "HS256",
20-
SigningKey: "test",
23+
SigningKey: []byte(os.Getenv("SECRET_KEY")),
2124
}))
2225

2326
}

server/types/types.go

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package types
2+
3+
// User user object
4+
type User struct {
5+
Handle string `json:"handle" form:"handle"`
6+
FirstName string `json:"firstname" form:"firstname"`
7+
LastName string `json:"lastname" form:"lastname"`
8+
Email string `json:"email" form:"email"`
9+
Password string `json:"password" form:"password"`
10+
Rating int32 `json:"rating" form:"rating"`
11+
}

0 commit comments

Comments
 (0)