sw-jobs-api/main.go
2024-12-06 22:57:47 +01:00

53 lines
1.1 KiB
Go

package main
import (
"log"
"net/http"
)
const PORT = "8080"
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/thehub", func(w http.ResponseWriter, r *http.Request) {
if !setHeader(w, r){
return
}
http.ServeFile(w, r, "/app/data/thehub.json")
})
mux.HandleFunc("/itjobbank", func(w http.ResponseWriter, r *http.Request) {
if !setHeader(w, r) {
return
}
http.ServeFile(w, r, "/app/data/itjobbank.json")
})
mux.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
return
})
server := &http.Server{
Addr: ":" + PORT,
Handler: mux,
}
log.Printf("Server starting on port %s", PORT)
log.Fatal(server.ListenAndServe())
}
func setHeader(w http.ResponseWriter, r *http.Request) bool {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS")
w.Header().Set("Content-Type", "application/json")
if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK)
return false
}
return true
}