-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
58 lines (47 loc) · 1.55 KB
/
main.go
File metadata and controls
58 lines (47 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package main
import (
"fmt"
"log"
"net/http"
"os"
_ "api.nhn.no/admin/docs" // docs is generated by Swag CLI, you have to import it.
"github.com/gorilla/mux"
httpSwagger "github.com/swaggo/http-swagger"
)
// @title NHN API Admin Service
// @description Various administrative functions for the API-gateway and k8s cluster
// @version 1.0
// @host localhost:80
// @BasePath /admin/
func main() {
if !GitSetup() {
os.Exit(1)
}
router := mux.NewRouter()
router.StrictSlash(true)
router.NotFoundHandler = http.HandlerFunc(NotFound)
router.HandleFunc("/", SwaggerRedirect)
router.HandleFunc("/api/new", NewAPIRegistration).Methods("POST")
router.HandleFunc("/docs/", SwaggerRedirect).Methods("GET")
router.PathPrefix("/docs").Handler(httpSwagger.WrapHandler).Methods("GET")
var portNumber = 80
fmt.Printf("Starting http listener on port %d ...\n", portNumber)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", portNumber), router))
}
// SwaggerRedirect redirects HTTP request to Swagger docs
func SwaggerRedirect(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/docs/index.html", http.StatusPermanentRedirect)
}
// NotFound ...
func NotFound(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
fmt.Fprint(w, "<html>404 not found 🙈 <br /><a href=\"/admin/docs/index.html\">API docs</a> 📜</html>")
}
// PanicIfError should be used to naively panics if an error is not nil.
func PanicIfError(err error) {
if err == nil {
return
}
fmt.Printf("\x1b[31;1m%s\x1b[0m\n", fmt.Sprintf("error: %s", err))
os.Exit(1)
}