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

[ImgBot] Optimize images #7

Open
wants to merge 27 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
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# EditorConfig is awesome: https://EditorConfig.org

# top-most EditorConfig file
root = true

[*]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = false
insert_final_newline = false
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
.next
3 changes: 3 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "next/core-web-vitals"
}
23 changes: 0 additions & 23 deletions .gitignore

This file was deleted.

395 changes: 395 additions & 0 deletions CC-BY-4.0

Large diffs are not rendered by default.

5 changes: 0 additions & 5 deletions README.md

This file was deleted.

8 changes: 8 additions & 0 deletions app/api/posts/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { NextResponse } from "next/server";
import posts from "@/app/blog/posts.json";

export const dynamic = "force-dynamic";

export function GET() {
return NextResponse.json(posts);
}
20 changes: 20 additions & 0 deletions app/api/views/[slug]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { getViews, increment } from '@/app/turso';
import { type NextRequest, NextResponse } from 'next/server'

interface Options {
params: {
slug: string;
}
}

export const GET = async (req: NextRequest, { params }: Options) => {
return NextResponse.json({
views: await getViews(params.slug)
})
};

export const POST = async (req: NextRequest, { params }: Options) => {
return NextResponse.json({
views: await increment(params.slug)
})
};
103 changes: 103 additions & 0 deletions app/blog/(post)/create-a-rest-api-with-golang/page.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
export const metadata = {
title: "Create a Rest API with Golang - Gaurish's Blog",
description: "Learn to create a rest api with golang",
openGraph: {
title: "Create a Rest API with Golang - Gaurish's Blog",
description: "Learn to create a rest api with golang",
images: [{ url: "/og/create-a-rest-api-with-golang" }],
},
};

This tutorial would explain you about how you're going to code a basic rest api in golang,
We'd be using [gin](https://github.com/gin-gonic/gin) a http web server framework.

Let's begin.

# Getting Started

Open up your shiny Terminal,
Run the Commands

```sh
go mod init gin-tutorial
touch main.go
go get -u github.com/gin-gonic/gin
```

![Terminal](/assets/blog/js2km281eaavgctg29wl.png)

Now, Let's Navigate to our main.go and start writing code with [vscode](https://code.visualstudio.com/)

On top of the file, Describe the package

```go
package main
```

Now, We'd be importing gin,

```go
package main

import "github.com/gin-gonic/gin"
```

Make sure you install gin via the command `go get -u github.com/gin-gonic/gin`

Now, We'll start making our router,

```go
package main

import "github.com/gin-gonic/gin"

func main() {
r := gin.Default()
r.SetTrustedProxies([]string{"192.168.1.2"})
r.Run()
}
```

Read more about setTrustedProxies [here](https://github.com/gin-gonic/gin#dont-trust-all-proxies)

So, If we run `go run main.go`, This is what we're gonna get...

```
[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.

[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
- using env: export GIN_MODE=release
- using code: gin.SetMode(gin.ReleaseMode)

[GIN-debug] Environment variable PORT is undefined. Using port :8080 by default
[GIN-debug] Listening and serving HTTP on :8080

```

Now, Open up `localhost:8080` on your browser, You'd see a `404 page not found` response, Yes, Because we haven't created any routes yet.

# Creating Routes

## Get Requests

- Get requests are handled as Router.GET, Same applies for all types of requests, Just replace GET with the type of Request,

Let's create a test route:

```
r.GET("/get", func(c *gin.Context) {
c.JSON(200, gin.H{
"status": "healthy",
})
})
```

So, If you now navigate to `localhost:8080/get`, You see a JSON Response

```
{"status":"healthy"}
```

Perfect!

Congratulations, You Just Learned to Create Your First Rest API With Golang.
Loading