Skip to content

Commit

Permalink
Merge pull request #21 from KunalSin9h/auto-migrate
Browse files Browse the repository at this point in the history
add automatically create folder and file for database and create table
  • Loading branch information
KunalSin9h authored May 1, 2023
2 parents 91d55e1 + 6843718 commit 5ebac42
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 72 deletions.
22 changes: 0 additions & 22 deletions Docker.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,6 @@ mkdir database
touch database/dev.db
```

4. Crete `images` table in the database file

```bash
sqlite3 database/dev.db
```

Execute the following SQL query in the sqlite3 shell

```sql
CREATE TABLE images (
id varchar(7) primary key,
title varchar(255),
image blob not null
);
```

Exit the sqlite3 shell

```bash
.exit
```

5. Run the server

```bash
Expand Down
97 changes: 47 additions & 50 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
<div align="center">

![My project-1](https://user-images.githubusercontent.com/82411321/212463201-06bf33cd-0665-42a5-8904-9a096dd10247.png)
</div>

</div>

## Tiddy is self hosted [image server](https://en.wikipedia.org/wiki/Image_server) written in Go


## Demo

Go to https://tiddi.kunalsin9h.dev to see a demo of the project.

## Features (API)

- [x] Upload images
- [x] View images
- [x] Delete images
Expand All @@ -22,59 +23,44 @@ Go to https://tiddi.kunalsin9h.dev to see a demo of the project.
> If you want to run the server inside a docker container, you can follow these [instructions](https://github.com/kunalsin9h/tiddi/blob/main/Docker.md)
### Prerequisites

- [Go](https://golang.org/dl/) (latest version recommended)
- [SQLite](https://www.sqlite.org/download.html) (latest version recommended)

### Steps

1. Clone the repo

```bash
git clone https://github.com/kunalsin9h/tiddi.git
```

2. Change the working directory

```bash
cd tiddi
```
3. Create a database folder in the root directory
```bash
mkdir database
```
4. Create a sqlite3 database file in the database folder
```bash
touch database/dev.db
```
5. Crete `images` table in the database file
```bash
sqlite3 database/dev.db
```
Execute the following SQL query in the sqlite3 shell
```sql
CREATE TABLE images (
id varchar(7) primary key,
title varchar(255),
image blob not null
);
```
Exit the sqlite3 shell
```bash
.exit
```

6. Run the server
4. Run the server

```bash
go run src/main.go
```

7. Open http://localhost:5656 in your browser

## Environment Variables

You can set the following environment variables to change the default settings of the server.

| Environment Variable | Default Value | Description |
| :------------------- | :------------ | :---------- |
| `PORT` | `5656` | Port on which the server will run |
| `DB` | `./database/dev.db` | Path to the database file |
| `HOST` | `localhost` | Host on which the server will run |
| Environment Variable | Default Value | Description |
| :------------------- | :------------------ | :-------------------------------- |
| `PORT` | `5656` | Port on which the server will run |
| `DB` | `./database/dev.db` | Path to the database file |
| `HOST` | `localhost` | Host on which the server will run |

#### Example

```bash
export PORT=8080
export DB=./database/prod.db
Expand All @@ -84,53 +70,62 @@ go run main.go
```

or alternative way is set the environment while running the server

```bash
PORT=8080 DB=./database/prod.db HOST=https://tiddi.kunalsin9h.dev go run main.go
```

> The `HOST` environment variable is used to generate the `unique image id` of the image, it is recommended to set it to the domain name of the server. `HOST` is also used in the sample frontend to fetch the image from the server.
## API Reference

### Sample Client

#### The Sample Frontend is available at `./src/frontend` directory, it is served by the server at `/` route.

```http
GET /
```

### Upload Image

```http
POST /upload-image/
```

| Parameter | Type | Description |
| :-------- | :------- | :-------------------------------- |
| `image` | `[]byte` | **Required**. Image bytes to upload |
| `title` | `string` | **Optional**. Title of the image |
| Parameter | Type | Description |
| :-------- | :------- | :---------------------------------- |
| `image` | `[]byte` | **Required**. Image bytes to upload |
| `title` | `string` | **Optional**. Title of the image |

Response:

```json
{
"url": "Unique URL of the image"
}
```


### View Image

```http
GET /{uiid}
```

> Image with `uiid` is served at this route
### Get Image Details

```http
POST /get-image/
```

| Parameter | Type | Description |
| :-------- | :------- | :-------------------------------- |
| `uiid` | `string` | **Required**. UIID of the image |
| Parameter | Type | Description |
| :-------- | :------- | :------------------------------ |
| `uiid` | `string` | **Required**. UIID of the image |

Response:

```json
{
"title": "Image Title",
Expand All @@ -141,24 +136,26 @@ Response:
> UIID is the `unique image id` of the image, it is generated by the server and is returned when the image is uploaded.
### Delete Image

```http
DELETE /delete-image/
```

| Parameter | Type | Description |
| :-------- | :------- | :-------------------------------- |
| `uiid` | `string` | **Required**. UIID of the image |
| Parameter | Type | Description |
| :-------- | :------- | :------------------------------ |
| `uiid` | `string` | **Required**. UIID of the image |

### Update Image

```http
PUT /update-image/
```

| Parameter | Type | Description |
| :-------- | :------- | :------------------------------------|
| `uiid` | `string` | **Required**. UIID of the image |
| `image` | `[]byte` | **Optional**. Image bytes to upload |
| `title` | `string` | **Optional**. Title of the image |
| Parameter | Type | Description |
| :-------- | :------- | :---------------------------------- |
| `uiid` | `string` | **Required**. UIID of the image |
| `image` | `[]byte` | **Optional**. Image bytes to upload |
| `title` | `string` | **Optional**. Title of the image |

## Tech Stack

Expand All @@ -176,8 +173,8 @@ PUT /update-image/

## Acknowledgements

- [Mattn's SQLite3 driver for Go](https://github.com/mattn/go-sqlite3)
- [matoous's go-nanoid](https://github.com/matoous/go-nanoid)
- [Mattn's SQLite3 driver for Go](https://github.com/mattn/go-sqlite3)
- [matoous's go-nanoid](https://github.com/matoous/go-nanoid)

## Support

Expand Down
19 changes: 19 additions & 0 deletions src/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"log"
"os"
"strings"

_ "github.com/mattn/go-sqlite3"
)
Expand All @@ -27,13 +28,31 @@ func SETUP_DB() {
os.Setenv("DB", DB)
}

foldersAndFile := strings.Split(DB, "/")
folders := strings.Join(foldersAndFile[0:len(foldersAndFile)-1], "/")
os.MkdirAll(folders, os.ModePerm)
os.Create(DB)

db, err := sql.Open("sqlite3", DB)

if err != nil {
log.Fatalf("[DB] Unable to open sqlite3 DB (%s): %v", DB, err)
}

DATABASE = db

/*
Creating Images Table if not exist
*/
DATABASE.Exec(
`CREATE TABLE IF NOT EXISTS
images (
id varchar(7) primary key,
title varchar(255),
image blob not null
);`,
)

}

func StoreImage(uiid, title string, imageData []byte) error {
Expand Down

0 comments on commit 5ebac42

Please sign in to comment.