Datetime-API/main.go

90 lines
2.2 KiB
Go
Raw Normal View History

2023-10-07 16:32:53 +02:00
package main
import (
"fmt"
"log"
"net/http"
"git.0x0001f346.de/andreas/Datetime-API/apiTime"
"git.0x0001f346.de/andreas/api"
"github.com/gorilla/mux"
)
2023-10-07 16:32:53 +02:00
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
2023-10-07 16:32:53 +02:00
func main() {
API := buildAPI()
router := mux.NewRouter()
2023-10-07 16:32:53 +02:00
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
2023-10-07 16:32:53 +02:00
}
router.HandleFunc(route, path.DELETE.Function).Methods("DELETE")
2023-10-07 16:32:53 +02:00
}
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")
}
}
2023-10-07 16:32:53 +02:00
printStartupBanner()
2023-10-07 16:32:53 +02:00
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", portToListenOn), router))
2023-10-07 16:32:53 +02:00
}
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
2023-10-07 16:32:53 +02:00
}
return fullAPI
2023-10-07 16:32:53 +02:00
}
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("")
2023-10-07 16:32:53 +02:00
}