34 lines
No EOL
639 B
Go
34 lines
No EOL
639 B
Go
package main
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"blue-backend/db"
|
|
"blue-backend/models"
|
|
"fmt"
|
|
)
|
|
|
|
func main() {
|
|
fmt.Println("Welcome to blueddit!")
|
|
fmt.Println("Initializing the backend...")
|
|
db.InitDB()
|
|
r := gin.Default()
|
|
|
|
// post routes
|
|
r.GET("/posts", func(c *gin.Context) {
|
|
var posts []models.Post
|
|
db.DB.Find(&posts)
|
|
c.JSON(200, posts)
|
|
})
|
|
|
|
r.POST("/posts", func(c *gin.Context) {
|
|
var post models.Post
|
|
if err := c.ShouldBindJSON(&post); err != nil {
|
|
c.JSON(400, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
db.DB.Create(&post)
|
|
c.JSON(200, post)
|
|
})
|
|
fmt.Println("blueddit is up and running")
|
|
r.Run(":8080")
|
|
} |