90 lines
2.2 KiB
Go
90 lines
2.2 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
|
|
"git.0x0001f346.de/andreas/Datetime-API/apiTime"
|
|
"git.0x0001f346.de/andreas/api"
|
|
"github.com/gorilla/mux"
|
|
)
|
|
|
|
const apiTitle string = "Datetime-API"
|
|
const apiDescription string = "A simple API to get the current time for a given time zone."
|
|
const apiVersion string = "0.1"
|
|
const portToListenOn int = 9100
|
|
|
|
func main() {
|
|
API := buildAPI()
|
|
router := mux.NewRouter()
|
|
|
|
router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Language", "en")
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
fmt.Fprint(w, API.ToJSONString())
|
|
}).Methods("GET")
|
|
|
|
for route, path := range API.Paths {
|
|
if path.DELETE != nil {
|
|
if path.DELETE.Function == nil {
|
|
continue
|
|
}
|
|
router.HandleFunc(route, path.DELETE.Function).Methods("DELETE")
|
|
}
|
|
if path.GET != nil {
|
|
if path.GET.Function == nil {
|
|
continue
|
|
}
|
|
router.HandleFunc(route, path.GET.Function).Methods("GET")
|
|
}
|
|
if path.PATCH != nil {
|
|
if path.PATCH.Function == nil {
|
|
continue
|
|
}
|
|
router.HandleFunc(route, path.PATCH.Function).Methods("PATCH")
|
|
}
|
|
if path.POST != nil {
|
|
if path.POST.Function == nil {
|
|
continue
|
|
}
|
|
router.HandleFunc(route, path.POST.Function).Methods("POST")
|
|
}
|
|
if path.PUT != nil {
|
|
if path.PUT.Function == nil {
|
|
continue
|
|
}
|
|
router.HandleFunc(route, path.PUT.Function).Methods("PUT")
|
|
}
|
|
}
|
|
|
|
printStartupBanner()
|
|
|
|
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", portToListenOn), router))
|
|
}
|
|
|
|
func buildAPI() api.API {
|
|
fullAPI := api.GetAPIPrototype(apiTitle, apiDescription, apiVersion)
|
|
|
|
for route, path := range apiTime.GetPaths() {
|
|
fullAPI.Paths[route] = path
|
|
}
|
|
|
|
for name, schema := range apiTime.GetComponents().Schemas {
|
|
fullAPI.Components.Schemas[name] = schema
|
|
}
|
|
|
|
return fullAPI
|
|
}
|
|
|
|
func printStartupBanner() {
|
|
fmt.Println("********************************************")
|
|
fmt.Println("* *")
|
|
fmt.Println("* git.0x0001f346.de/andreas/Datetime-API *")
|
|
fmt.Println("* *")
|
|
fmt.Println("********************************************")
|
|
fmt.Println("")
|
|
fmt.Printf("Listening: http://localhost:%d\n", portToListenOn)
|
|
fmt.Println("")
|
|
}
|