godog/_examples/api/api.go
Softonik ffcc5b10ae
Некоторые проверки провалились
test / test (1.16.x) (push) Has been cancelled
test / test (1.17.x) (push) Has been cancelled
test / test (oldstable) (push) Has been cancelled
test / test (stable) (push) Has been cancelled
Модуль переименован для публикации
2025-06-01 02:41:39 +03:00

52 строки
1,1 КиБ
Go

// Example - demonstrates REST API server implementation tests.
package main
import (
"encoding/json"
"net/http"
"git.golang1.ru/softonik/godog"
)
func getVersion(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
fail(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
data := struct {
Version string `json:"version"`
}{Version: godog.Version}
ok(w, data)
}
// fail writes a json response with error msg and status header
func fail(w http.ResponseWriter, msg string, status int) {
w.WriteHeader(status)
data := struct {
Error string `json:"error"`
}{Error: msg}
resp, _ := json.Marshal(data)
w.Header().Set("Content-Type", "application/json")
w.Write(resp)
}
// ok writes data to response with 200 status
func ok(w http.ResponseWriter, data interface{}) {
resp, err := json.Marshal(data)
if err != nil {
fail(w, "Oops something evil has happened", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(resp)
}
func main() {
http.HandleFunc("/version", getVersion)
http.ListenAndServe(":8080", nil)
}