当前位置:网站首页>Go language library management restful API development practice
Go language library management restful API development practice
2022-06-25 06:26:00 【InfoQ】
1. preparation
- browser
- gorilla/handlers
- gorilla/mux
2. Application structure
├── main.go
└── src
├── app.go
├── data.go
├── handlers.go
├── helpers.go
└── middlewares.go
Go Toolkits and modules
srcgo mod init bookstore
go.mod3. structure API
main.gopackage main
import"bookstore/src"
func main() {
src.Start()
}
package mainsrcbookstoremain()srcStart()main.goRoutes and handlers
Muxapp.gopackage src
import (
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"log"
"net/http"
"os"
)
func Start() {
router := mux.NewRouter()
router.Use(commonMiddleware)
router.HandleFunc("/book", getAllBooks).Methods(http.MethodGet)
router.HandleFunc("/book", addBook).Methods(http.MethodPost)
router.HandleFunc("/book/{book_id:[0-9]+}", getBook).Methods(http.MethodGet)
router.HandleFunc("/book/{book_id:[0-9]+}", updateBook).Methods(http.MethodPut)
router.HandleFunc("/book/{book_id:[0-9]+}", deleteBook).Methods(http.MethodDelete)
log.Fatal(http.ListenAndServe("localhost:5000", handlers.LoggingHandler(os.Stdout, router)))
}
app.gomain.goStart()muxhandlersgo get github.com/gorilla/handlers
go get github.com/gorilla/mux
go.modmodule bookstore
go1.17
require (
github.com/gorilla/handlers v1.5.1
github.com/gorilla/mux v1.8.0
)
require github.com/felixge/httpsnoop v1.0.1// indirect
Start()MuxMuxrouter.Use(commonMiddleware)
router.HandleFunc("/book/{book_id:[0-9]+}", updateBook).Methods(http.MethodPut)
/book/123PUTupdateBookbook_idmiddleware
Content-Typeapp.goStart()router.Use(commonMiddleware)
middlewares.gopackage src
import"net/http"
func commonMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("content-type", "application/json; charset=utf-8")
w.Header().Set("x-content-type-options", "nosniff")
next.ServeHTTP(w, r)
})
}
Start()commonMiddlewareStatic data
srcdata.gopackage src
type Book struct {
Id int`json:"id"`
Title string`json:"title"`
Author string`json:"author"`
Genre string`json:"genre"`
}
var booksDB = []Book{
{Id: 123, Title: "The Hobbit", Author: "J. R. R. Tolkien", Genre: "Fantasy"},
{Id: 456, Title: "Harry Potter and the Philosopher's Stone", Author: "J. K. Rowling", Genre: "Fantasy"},
{Id: 789, Title: "The Little Prince", Author: "Antoine de Saint-Exupéry", Genre: "Novella"},
}
type CustomResponse struct {
Code int `json:"code"`
Message string `json:"message"`
Description string `json:"description,omitempty"`
}
var responseCodes = map[int]string {
400: "Bad Request",
401: "Unauthorized",
403: "Forbidden",
404: "Not Found",
409: "Conflict",
422: "Validation Error",
429: "Too Many Requests",
500: "Internal Server Error",
}
Auxiliary tool
helpers.gopackage src
import (
"encoding/json"
"net/http"
)
func removeBook(s []Book, i int) []Book {
if i != len(s)-1 {
s[i] = s[len(s)-1]
}
return s[:len(s)-1]
}
func checkDuplicateBookId(s []Book, id int) bool {
for _, book := range s {
if book.Id == id {
return true
}
}
return false
}
func JSONResponse(w http.ResponseWriter, code int, desc string) {
w.WriteHeader(code)
message, ok := responseCodes[code]
if !ok {
message = "Undefined"
}
r := CustomResponse{
Code: code,
Message: message,
Description: desc,
}
_ = json.NewEncoder(w).Encode(r)
}
removeBookBookcheckDuplicateBookIdBookJSONResponseCustomResponseresponseCodesThe handler
handlers.gopackage src
import (
"encoding/json"
"github.com/gorilla/mux"
"net/http"
"strconv"
)
Get a single book
func getBook(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
bookId, _ := strconv.Atoi(vars["book_id"])
for _, book := range booksDB {
if book.Id == bookId {
_ = json.NewEncoder(w).Encode(book)
return
}
}
JSONResponse(w, http.StatusNotFound, "")
}
404: Not FoundGet all the books
func getAllBooks(w http.ResponseWriter, r *http.Request) {
_ = json.NewEncoder(w).Encode(booksDB)
}
Add a new book
func addBook(w http.ResponseWriter, r *http.Request) {
decoder := json.NewDecoder(r.Body)
var b Book
err := decoder.Decode(&b)
if err != nil {
JSONResponse(w, http.StatusBadRequest, "")
return
}
if checkDuplicateBookId(booksDB, b.Id) {
JSONResponse(w, http.StatusConflict, "")
return
}
booksDB = append(booksDB, b)
w.WriteHeader(201)
_ = json.NewEncoder(w).Encode(b)
}
{
"id": 999,
"title": "SomeTitle",
"author": "SomeAuthor",
"genre": "SomeGenre"
}
400: Bad Request error409: Conflict error backUpdate existing books
func updateBook(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
bookId, _ := strconv.Atoi(vars["book_id"])
decoder := json.NewDecoder(r.Body)
var b Book
err := decoder.Decode(&b)
if err != nil {
JSONResponse(w, http.StatusBadRequest, "")
return
}
for i, book := range booksDB {
if book.Id == bookId {
booksDB[i] = b
_ = json.NewEncoder(w).Encode(b)
return
}
}
JSONResponse(w, http.StatusNotFound, "")
}
404: Not FoundDelete existing books
func deleteBook(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
bookId, _ := strconv.Atoi(vars["book_id"])
for i, book := range booksDB {
if book.Id == bookId {
booksDB = removeBook(booksDB, i)
_ = json.NewEncoder(w).Encode(book)
return
}
}
JSONResponse(w, http.StatusNotFound, "")
}
removeBook404: Not Found4. Run and test API
go run main.go
边栏推荐
- Asemi fast recovery diode us1m parameters, us1m recovery time, us1m voltage drop
- Explain @builder usage
- Understand what MTU is
- ctfshow-misc
- Tail command – view the contents at the end of the file
- IQ debugging of Hisilicon platform ISP and image (1)
- Location object
- BigDecimal. Summary of setscale usage
- Gavin's insight on transformer live class - line by line analysis and field experiment analysis of insurance BOT microservice code of insurance industry in the actual combat of Rasa dialogue robot pro
- Global and Chinese medical protective clothing market supply and demand research and investment value proposal report 2022-2028
猜你喜欢

Folding mobile phones are expected to explode, or help Samsung compete with apple and Chinese mobile phones

How to use asemi FET 7n80 and how to use 7n80

Understand what MTU is

IQ debugging of Hisilicon platform ISP and image (1)

Gavin's insight on transformer live class - line by line analysis and field experiment analysis of insurance BOT microservice code of insurance industry in the actual combat of Rasa dialogue robot pro

Viewing Chinese science and technology from the Winter Olympics (V): the Internet of things

【LeetCode】40. Combined summation II (2 strokes of wrong questions)
![[kicad image] download and installation](/img/88/cebf8cc55cb8904c91f9096312859a.jpg)
[kicad image] download and installation

CTFSHOW

Mongodb basic concept learning - set
随机推荐
Laravel8+ wechat applet generates QR code
Global and Chinese medical protective clothing market supply and demand research and investment value proposal report 2022-2028
The elephant turns around and starts the whole body. Ali pushes Maoxiang not only to Jingdong
Optimal Parking
Analysis report on demand scale and Supply Prospect of global and Chinese thermal insulation materials market during the 14th Five Year Plan period 2022-2028
Getting started with mongodb
Mongodb basic concept learning - Documentation
Global and China financial guarantee marketing strategy and channel dynamic construction report 2022
Research Report on demand and Competitive Prospect of global and Chinese welding personal protective equipment industry 2022-2027
Why study discrete mathematics
Observation configuring wmic
HCIP Day 16
RM command – remove file or directory
An easy problem
BigDecimal. Summary of setscale usage
Exercise: completion
RT thread i/o device model and layering
Research Report on global and Chinese vaccine market profit forecast and the 14th five year plan development strategy 2022-2028
Zhinai's database
@Principle of preauthorize permission control