Skip to content

Commit

Permalink
chore: use http.Status* instead of hard code (#1482)
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkerou authored and appleboy committed Aug 14, 2018
1 parent cf26dc7 commit 6a6415f
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 14 deletions.
10 changes: 6 additions & 4 deletions basic/main.go
@@ -1,6 +1,8 @@
package main

import (
"net/http"

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

Expand All @@ -13,17 +15,17 @@ func setupRouter() *gin.Engine {

// Ping test
r.GET("/ping", func(c *gin.Context) {
c.String(200, "pong")
c.String(http.StatusOK, "pong")
})

// Get user value
r.GET("/user/:name", func(c *gin.Context) {
user := c.Params.ByName("name")
value, ok := DB[user]
if ok {
c.JSON(200, gin.H{"user": user, "value": value})
c.JSON(http.StatusOK, gin.H{"user": user, "value": value})
} else {
c.JSON(200, gin.H{"user": user, "status": "no value"})
c.JSON(http.StatusOK, gin.H{"user": user, "status": "no value"})
}
})

Expand All @@ -49,7 +51,7 @@ func setupRouter() *gin.Engine {

if c.Bind(&json) == nil {
DB[user] = json.Value
c.JSON(200, gin.H{"status": "ok"})
c.JSON(http.StatusOK, gin.H{"status": "ok"})
}
})

Expand Down
2 changes: 1 addition & 1 deletion basic/main_test.go
Expand Up @@ -15,6 +15,6 @@ func TestPingRoute(t *testing.T) {
req, _ := http.NewRequest("GET", "/ping", nil)
router.ServeHTTP(w, req)

assert.Equal(t, 200, w.Code)
assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, "pong", w.Body.String())
}
4 changes: 3 additions & 1 deletion favicon/main.go
@@ -1,6 +1,8 @@
package main

import (
"net/http"

"github.com/gin-gonic/gin"
"github.com/thinkerou/favicon"
)
Expand All @@ -9,7 +11,7 @@ func main() {
app := gin.Default()
app.Use(favicon.New("./favicon.ico"))
app.GET("/ping", func(c *gin.Context) {
c.String(200, "Hello favicon.")
c.String(http.StatusOK, "Hello favicon.")
})
app.Run(":8080")
}
3 changes: 2 additions & 1 deletion http2/main.go
Expand Up @@ -3,6 +3,7 @@ package main
import (
"html/template"
"log"
"net/http"
"os"

"github.com/gin-gonic/gin"
Expand All @@ -27,7 +28,7 @@ func main() {
r.SetHTMLTemplate(html)

r.GET("/welcome", func(c *gin.Context) {
c.HTML(200, "https", gin.H{
c.HTML(http.StatusOK, "https", gin.H{
"status": "success",
})
})
Expand Down
11 changes: 6 additions & 5 deletions realtime-advanced/routes.go
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"html"
"io"
"net/http"
"strings"
"time"

Expand All @@ -21,12 +22,12 @@ func rateLimit(c *gin.Context) {
fmt.Println("ip blocked")
}
c.Abort()
c.String(503, "you were automatically banned :)")
c.String(http.StatusServiceUnavailable, "you were automatically banned :)")
}
}

func index(c *gin.Context) {
c.Redirect(301, "/room/hn")
c.Redirect(http.StatusMovedPermanently, "/room/hn")
}

func roomGET(c *gin.Context) {
Expand All @@ -38,7 +39,7 @@ func roomGET(c *gin.Context) {
if len(nick) > 13 {
nick = nick[0:12] + "..."
}
c.HTML(200, "room_login.templ.html", gin.H{
c.HTML(http.StatusOK, "room_login.templ.html", gin.H{
"roomid": roomid,
"nick": nick,
"timestamp": time.Now().Unix(),
Expand All @@ -55,7 +56,7 @@ func roomPOST(c *gin.Context) {
validMessage := len(message) > 1 && len(message) < 200
validNick := len(nick) > 1 && len(nick) < 14
if !validMessage || !validNick {
c.JSON(400, gin.H{
c.JSON(http.StatusBadRequest, gin.H{
"status": "failed",
"error": "the message or nickname is too long",
})
Expand All @@ -68,7 +69,7 @@ func roomPOST(c *gin.Context) {
}
messages.Add("inbound", 1)
room(roomid).Submit(post)
c.JSON(200, post)
c.JSON(http.StatusOK, post)
}

func streamRoom(c *gin.Context) {
Expand Down
5 changes: 3 additions & 2 deletions realtime-chat/main.go
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"
"math/rand"
"net/http"

"github.com/gin-gonic/gin"
)
Expand Down Expand Up @@ -34,7 +35,7 @@ func stream(c *gin.Context) {
func roomGET(c *gin.Context) {
roomid := c.Param("roomid")
userid := fmt.Sprint(rand.Int31())
c.HTML(200, "chat_room", gin.H{
c.HTML(http.StatusOK, "chat_room", gin.H{
"roomid": roomid,
"userid": userid,
})
Expand All @@ -46,7 +47,7 @@ func roomPOST(c *gin.Context) {
message := c.PostForm("message")
room(roomid).Submit(userid + ": " + message)

c.JSON(200, gin.H{
c.JSON(http.StatusOK, gin.H{
"status": "success",
"message": message,
})
Expand Down

0 comments on commit 6a6415f

Please sign in to comment.