Created
April 15, 2020 05:33
-
-
Save moemoe89/6bc0d434af484b60b7169b70b57441df to your computer and use it in GitHub Desktop.
Example Mux Swagger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"net/http" | |
"time" | |
"github.com/gorilla/mux" | |
"github.com/swaggo/http-swagger" | |
_ "swagger-mux/docs" | |
) | |
var starTime = time.Now() | |
type PingModel struct { | |
StartTime string `json:"start_time"` | |
} | |
// | |
// @Summary Ping | |
// @Description check connection | |
// @Produce json | |
// @Success 200 {object} PingModel | |
// @Router /ping [get] | |
func Ping(w http.ResponseWriter, r *http.Request) { | |
loc, _ := time.LoadLocation("Asia/Jakarta") | |
startTime := starTime.In(loc) | |
res := PingModel { | |
StartTime: startTime.Format("[02 January 2006] 15:04:05 MST"), | |
} | |
w.WriteHeader(http.StatusOK) | |
json.NewEncoder(w).Encode(res) | |
} | |
// @title Swagger Example API | |
// @version 1.0 | |
// @description This is a sample server Petstore server. | |
// @termsOfService http://swagger.io/terms/ | |
// @contact.name API Support | |
// @contact.url http://www.swagger.io/support | |
// @contact.email [email protected] | |
// @license.name Apache 2.0 | |
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html | |
// @host | |
// @BasePath / | |
func main() { | |
r := mux.NewRouter().StrictSlash(true) | |
r.HandleFunc("/ping", Ping).Methods("GET") | |
r.PathPrefix("/documentation/").Handler(httpSwagger.WrapHandler) | |
http.ListenAndServe(":8080", r) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment