├── log └── user.db ├── model └── model.go ├── .github └── workflows │ └── go.yml ├── database └── database.go ├── main.go ├── .gitignore ├── controller └── controller.go └── README.md /log/user.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/saffetblt/rest-api/HEAD/log/user.db -------------------------------------------------------------------------------- /model/model.go: -------------------------------------------------------------------------------- 1 | package model 2 | 3 | import "github.com/jinzhu/gorm" 4 | 5 | type User struct { 6 | gorm.Model 7 | Username string `json:"username"` 8 | Name string `json:"name"` 9 | Surname string `json:"surname"` 10 | Password string `json:"password"` 11 | Email string `json:"email"` 12 | Phone string `json:"phone"` 13 | Age int `json:"age"` 14 | Country string `json:"country"` 15 | City string `json:"city"` 16 | } 17 | -------------------------------------------------------------------------------- /.github/workflows/go.yml: -------------------------------------------------------------------------------- 1 | name: Go 2 | on: [push] 3 | jobs: 4 | 5 | build: 6 | name: Build 7 | runs-on: ubuntu-latest 8 | steps: 9 | 10 | - name: Set up Go 1.13 11 | uses: actions/setup-go@v1 12 | with: 13 | go-version: 1.13 14 | id: go 15 | 16 | - name: Check out code into the Go module directory 17 | uses: actions/checkout@v1 18 | 19 | - name: Get dependencies 20 | run: | 21 | go get ./... 22 | - name: Build 23 | run: go build -v . 24 | -------------------------------------------------------------------------------- /database/database.go: -------------------------------------------------------------------------------- 1 | package database 2 | 3 | import ( 4 | "fmt" 5 | "rest-api/model" 6 | 7 | "github.com/jinzhu/gorm" 8 | ) 9 | 10 | type User = model.User 11 | type Database struct { 12 | *gorm.DB 13 | } 14 | 15 | var DB *gorm.DB 16 | 17 | func InitDB() *gorm.DB { 18 | db, err := gorm.Open("sqlite3", "log/user.db") 19 | if err != nil { 20 | fmt.Println(err.Error()) 21 | panic("failed to connect database") 22 | } 23 | // defer db.Close() 24 | db.LogMode(true) 25 | db.AutoMigrate(&model.User{}) 26 | DB = db 27 | return DB 28 | } 29 | 30 | func GetDB() *gorm.DB { 31 | return DB 32 | } 33 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "rest-api/controller" 5 | "rest-api/database" 6 | 7 | "github.com/gin-gonic/gin" 8 | 9 | _ "github.com/mattn/go-sqlite3" 10 | ) 11 | 12 | func main() { 13 | port := "8085" 14 | url := "192.168.0.12" 15 | database.InitDB() 16 | 17 | r := gin.Default() 18 | user := r.Group("/user") 19 | { 20 | user.GET("/", controller.GetUsers) 21 | user.GET("/:id", controller.GetUser) 22 | user.POST("/", controller.CreateUser) 23 | user.DELETE("/:id", controller.DeleteUser) 24 | user.PUT("/:id", controller.UpdateUser) 25 | } 26 | r.Run(url + ":" + port) 27 | } 28 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ### Go ### 2 | # Binaries for programs and plugins 3 | *.exe 4 | *.exe~ 5 | *.dll 6 | *.so 7 | *.dylib 8 | 9 | # Test binary, built with `go test -c` 10 | *.test 11 | 12 | # Output of the go coverage tool, specifically when used with LiteIDE 13 | *.out 14 | 15 | ### Go Patch ### 16 | /vendor/ 17 | /Godeps/ 18 | 19 | ### VisualStudioCode ### 20 | .vscode/* 21 | !.vscode/settings.json 22 | !.vscode/tasks.json 23 | !.vscode/launch.json 24 | !.vscode/extensions.json 25 | 26 | ### VisualStudioCode Patch ### 27 | # Ignore all local history of files 28 | .history 29 | 30 | ##DS STORE## 31 | .DS_Store 32 | .DS_Store? 33 | 34 | ## ENVIRONMENT FILE ## 35 | 36 | .env -------------------------------------------------------------------------------- /controller/controller.go: -------------------------------------------------------------------------------- 1 | package controller 2 | 3 | import ( 4 | "rest-api/database" 5 | "rest-api/model" 6 | 7 | "github.com/gin-gonic/gin" 8 | ) 9 | 10 | type User = model.User 11 | 12 | func GetUsers(c *gin.Context) { 13 | db := database.GetDB() 14 | users := []User{} 15 | 16 | db.Find(&users) 17 | c.JSON(200, users) 18 | } 19 | 20 | func GetUser(c *gin.Context) { 21 | db := database.GetDB() 22 | id := c.Params.ByName("id") 23 | var user User 24 | 25 | if db.First(&user, id).Error != nil { 26 | c.AbortWithStatus(404) 27 | } else { 28 | c.JSON(200, user) 29 | } 30 | 31 | } 32 | 33 | func CreateUser(c *gin.Context) { 34 | db := database.GetDB() 35 | var user User 36 | 37 | c.BindJSON(&user) 38 | db.Create(&user) 39 | c.JSON(200, user) 40 | } 41 | 42 | func DeleteUser(c *gin.Context) { 43 | db := database.GetDB() 44 | id := c.Params.ByName("id") 45 | var user User 46 | 47 | if db.Delete(&user, id).Error != nil { 48 | c.AbortWithStatus(404) 49 | } else { 50 | c.JSON(200, gin.H{"id#" + id: "deleted"}) 51 | } 52 | } 53 | 54 | func UpdateUser(c *gin.Context) { 55 | db := database.GetDB() 56 | id := c.Params.ByName("id") 57 | var user User 58 | if db.First(&user, id).Error != nil { 59 | c.AbortWithStatus(404) 60 | } else { 61 | c.BindJSON(&user) 62 | db.Save(&user) 63 | c.JSON(200, user) 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # TANIM 2 | Uygulama REST API ile endpointlere gelen HTTP isteklerine database üzerinde işlem yaparak cevap vermektedir. 3 | 4 | ## Kullanılan Teknolojiler 5 | Bu uygulamada **Gin Web Framework** ile **Gorm ORM Kütüphanesi** kullanılmaktadır. 6 | 7 | ## Bağımlılıklar 8 | ``` 9 | go get -u github.com/gin-gonic/gin 10 | go get -u github.com/jinzhu/gorm 11 | go get github.com/mattn/go-sqlite3 12 | 13 | ``` 14 | ## Uygulamanın başlatılması 15 | Uygulama /backend klasörü içinde aşağıdaki komut ile başlatılır. 16 | ``` 17 | go run main.go 18 | ``` 19 | 20 | ## Uygulamanın Çalıştığı Port 21 | Uygulama **8085** portunu dinlemektedir. 22 | 23 | ## Uygulamanın Dinlediği Uç Noktalar 24 | 25 | | Method | URI | Fonksiyon | 26 | |--------|-------------|------------------| 27 | | GET | /user/ | main.GetUsers | 28 | | GET | /user/:id | main.GetUser | 29 | | POST | /user | main.CreateUser | 30 | | DELETE | /user/:id | main.DeleteUser | 31 | | PUT | /user/:id | main.UpdateUser | 32 | 33 | ## Örnek bir JSON çıktısı 34 | 35 | ``` 36 | { 37 | "ID": 1, 38 | "CreatedAt": "2019-04-20T22:02:04.648969408+03:00", 39 | "UpdatedAt": "2019-04-20T22:02:04.648969408+03:00", 40 | "DeletedAt": null, 41 | "username": "saffetblt", 42 | "name": "Saffet", 43 | "surname": "BULUT", 44 | "password": "12345", 45 | "email": "saffetblt@gmail.com", 46 | "phone": "555 444 33 22", 47 | "age": 25, 48 | "country": "Turkey", 49 | "city": "Ankara" 50 | } 51 | ``` 52 | 53 | ## Örnek HTTP istekleri 54 | GET 55 | ``` 56 | 192.168.0.12:8085/user/ 57 | 192.168.0.12:8085/user/5 58 | ``` 59 | POST 60 | ``` 61 | 192.168.0.12:8085/user/ 62 | ``` 63 | DELETE 64 | ``` 65 | 192.168.0.12:8085/user/4 66 | ``` 67 | PUT 68 | ``` 69 | 192.168.0.12:8085/user/3 70 | ``` 71 | 72 | ## Öneriler 73 | Http istekleri için Postman programı kullanılmaktadır. 74 | 75 | .db uzantılı database dosyasını görüntülemek için DB Browser for SQLite kullanılmaktadır. 76 | 77 | 78 | --------------------------------------------------------------------------------