golang

Fiber APP 시작하기

우주개발자42 2023. 11. 9. 00:29

Fiber Basics

파이버는 웹 개발을 위한 API 구축을 돕는 go 패키지.

이 것은 Node.js의 Express Framework에서 영감을 받아 https://github.com/valyala/fasthttp 위에 구축

node.js로 API를 구축한 경험이 있다면, Fiber in Go로 시작하는 것이 더 쉬울 것으로 보임

 

What are we building?

  1. Create a API server to run local
  2. Connect to database (sqlite3)
  3. Send Get request
  4. Send Post request
type Camping struct {
	Id             uint          	  `json:"id"`
	Title          string         	  `json:"title"`
	Address        string       	  `json:"address"`
	Description    string   	  `json:"description"`
	CreatedAt      time.Time 	  `json:"created_at"`
	UpdatedAt      time.Time 	  `json:"updated_at"`
}

 

Creating the first endpoint

Go mod 명령어로 Go 어플리케이션 설정

새로운 폴더를 만들고 Go 프로젝트를 초기화

fiber package 설치

go mod init <repo_name>
go get -u github.com/gofiber/fiber/v2

 

이제 서버를 구동시켜 보자

package main

import "github.com/gofiber/fiber/v2"

func main() {
    app := fiber.New() // 인스턴스 생성

    app.Get("/", func(c *fiber.Ctx) error {
        return c.SendString("Hello, World 👋!")
    })

    app.Listen(":3000")
}

 

http://localhost:3000 으로 접속하면 "Hello, World 👋!" 화면이 뜨면 성공