2023-01-26 20:58:13 +00:00
package main
import (
"io"
"net/http"
"strconv"
"strings"
"time"
)
func handler ( ) {
//Pages
2023-01-26 22:47:23 +00:00
httpHandleFunc ( "" , "./web/pages/home.html" , "text/html" )
httpHandleFunc ( "home" , "./web/pages/home.html" , "text/html" )
httpHandleFunc ( "treff" , "./web/pages/treff.html" , "text/html" )
httpHandleFunc ( "events" , "./web/pages/events.html" , "text/html" )
httpHandleFunc ( "about" , "./web/pages/about.html" , "text/html" )
2023-01-26 20:58:13 +00:00
2023-01-26 22:47:23 +00:00
httpHandleFunc ( "kontakt" , "./web/pages/kontakt.html" , "text/html" )
httpHandleFunc ( "kontakt/adresse" , "./web/pages/kontakt/adresse.html" , "text/html" )
httpHandleFunc ( "kontakt/irc" , "./web/pages/kontakt/irc.html" , "text/html" )
httpHandleFunc ( "kontakt/mail" , "./web/pages/kontakt/mail.html" , "text/html" )
httpHandleFunc ( "kontakt/tel" , "./web/pages/kontakt/tel.html" , "text/html" )
2023-01-26 20:58:13 +00:00
2023-01-26 22:47:23 +00:00
httpHandleFunc ( "verein" , "./web/pages/verein.html" , "text/html" )
httpHandleFunc ( "support" , "./web/pages/support.html" , "text/html" )
2023-01-26 20:58:13 +00:00
2023-01-26 22:47:23 +00:00
httpHandleFunc ( "impressum" , "./web/pages/impressum.html" , "text/html" )
httpHandleFunc ( "datenschutz" , "./web/pages/datenschutz.html" , "text/html" )
2023-01-26 20:58:13 +00:00
//Styles
2023-01-26 22:47:23 +00:00
httpHandleFunc ( "style/main.css" , "./web/styles/main.css" , "text/css" )
httpHandleFunc ( "style/kontakt.css" , "./web/styles/kontakt.css" , "text/css" )
httpHandleFunc ( "style/home.css" , "./web/styles/home.css" , "text/css" )
2023-01-28 13:23:31 +00:00
httpHandleFunc ( "style/events.css" , "./web/styles/events.css" , "text/css" )
2023-01-26 20:58:13 +00:00
//Images
2023-01-26 22:47:23 +00:00
httpHandleFunc ( "image/logo_ctdo.svg" , "./web/images/logo_ctdo.svg" , "image/svg+xml" )
httpHandleFunc ( "image/header.jpg" , "./web/images/header.jpg" , "image/jpeg" )
httpHandleFunc ( "image/adresse_knopf.webp" , "./web/images/adresse_knopf.webp" , "image/webp" )
httpHandleFunc ( "image/chat_knopf.webp" , "./web/images/chat_knopf.webp" , "image/webp" )
httpHandleFunc ( "image/mail_knopf.webp" , "./web/images/mail_knopf.webp" , "image/webp" )
httpHandleFunc ( "image/tel_knopf.webp" , "./web/images/tel_knopf.webp" , "image/webp" )
2023-01-26 20:58:13 +00:00
}
func getPages ( ) [ ] [ ] string {
output := [ ] [ ] string { }
output = append ( output , [ ] string { "home" , "/home" } )
output = append ( output , [ ] string { "zeiten & location" , "/treff" } )
output = append ( output , [ ] string { "events" , "/events" } )
output = append ( output , [ ] string { "über uns" , "/about" } )
output = append ( output , [ ] string { "kontakt" , "/kontakt" } )
output = append ( output , [ ] string { "verein" , "/verein" } )
output = append ( output , [ ] string { "unterstützung" , "/support" } )
return output
}
func getFooterPages ( ) [ ] [ ] string {
output := [ ] [ ] string { }
output = append ( output , [ ] string { "impressum" , "/impressum" } )
output = append ( output , [ ] string { "datenschutzerklärung" , "/datenschutz" } )
return output
}
func getRoomState ( ) status {
c := & http . Client { Timeout : 10 * time . Second }
r , err := c . Get ( "https://status.ctdo.de/api/simple/v2" )
if err != nil {
2023-01-28 17:44:11 +00:00
panic ( err . Error ( ) )
2023-01-26 20:58:13 +00:00
}
defer r . Body . Close ( )
body , _err := io . ReadAll ( r . Body )
if _err != nil {
2023-01-28 17:44:11 +00:00
panic ( _err . Error ( ) )
2023-01-26 20:58:13 +00:00
}
bodyString := string ( body )
temp := [ ] string { }
bodyString = strings . ReplaceAll ( bodyString , "{" , "" )
bodyString = strings . ReplaceAll ( bodyString , "}" , "" )
_temp := strings . Split ( bodyString , "," )
for _ , element := range _temp {
__temp := strings . Split ( element , ":" )
temp = append ( temp , __temp [ 1 ] )
}
roomState := new ( status )
roomState . state = temp [ 0 ] == "true"
var __err error
roomState . power , __err = strconv . ParseInt ( temp [ 2 ] , 0 , 64 )
if __err != nil {
2023-01-28 17:44:11 +00:00
panic ( __err . Error ( ) )
2023-01-26 20:58:13 +00:00
}
return * roomState
}
2023-01-26 22:13:44 +00:00
2023-01-28 13:23:31 +00:00
func htmlNewBanner ( text string , link string ) string {
output := ""
output += htmlElement ( "div" , htmlLinkElement ( text , link , true , "" ) , "class=\"newBanner\"" )
return output
}
2023-01-27 14:30:42 +00:00
func htmlReplacer ( input string , activePage string ) string {
output := strings . ReplaceAll ( input , "!NAV" , htmlNav ( getPages ( ) , activePage ) )
2023-01-26 22:13:44 +00:00
if getRoomState ( ) . state {
output = strings . ReplaceAll ( output , "!RAUMSTATUS" , "<p>Raumstatus: <b class=\"green-text\">offen</b></p>" )
} else {
output = strings . ReplaceAll ( output , "!RAUMSTATUS" , "<p>Raumstatus: <b class=\"red-text\">geschlossen</b></p>" )
}
2023-01-27 14:30:42 +00:00
output = strings . ReplaceAll ( output , "!FOOTERNAV" , htmlNav ( getFooterPages ( ) , activePage ) )
2023-01-26 22:13:44 +00:00
2023-01-28 13:23:31 +00:00
if getNextTopic ( ) . days == 0 {
output = strings . ReplaceAll ( output , "!TOPICTREFF" , htmlElement ( "h3" , "Nächster Topictreff findet Heute statt!" , "" ) )
} else if getNextTopic ( ) . days == 1 {
output = strings . ReplaceAll ( output , "!TOPICTREFF" , htmlElement ( "h3" , "Nächster Topictreff findet Morgen statt!" , "class=\"topic\"" ) + htmlElement ( "p" , "Am " + getNextTopic ( ) . date , "class=\"topic\"" ) )
} else if getNextTopic ( ) . days < 10 {
output = strings . ReplaceAll ( output , "!TOPICTREFF" , htmlElement ( "h3" , "Nächster Topictreff findet in " + strconv . FormatInt ( int64 ( getNextTopic ( ) . days ) , 10 ) + " Tagen statt!" , "class=\"topic\"" ) + htmlElement ( "p" , "Am " + getNextTopic ( ) . date , "class=\"topic\"" ) )
} else {
output = strings . ReplaceAll ( output , "!TOPICTREFF" , htmlElement ( "h3" , "Nächster Topictreff findet in " + string ( getNextTopic ( ) . days ) + " Tagen statt!" , "class=\"topic\"" ) + htmlElement ( "p" , "Am " + getNextTopic ( ) . date , "class=\"topic\"" ) )
}
2023-01-28 17:21:21 +00:00
events := getEvents ( )
if len ( events ) == 0 {
output = strings . ReplaceAll ( output , "!EVENTS" , htmlNewBanner ( "Rundgang" , "https://www.chaostreff-dortmund.de/rundgang/" ) )
} else {
output = strings . ReplaceAll ( output , "!EVENTS" , htmlNewBanner ( "Rundgang" , "https://www.chaostreff-dortmund.de/rundgang/" ) )
}
2023-01-28 13:23:31 +00:00
output = strings . ReplaceAll ( output , "!NEWBANNER" , htmlNewBanner ( "Rundgang" , "https://www.chaostreff-dortmund.de/rundgang/" ) )
return output
}
func ifFloatRange ( variable float64 , min float64 , max float64 , includeMin bool , includeMax bool ) bool {
a , b := false , false
if includeMin {
a = variable >= min
} else {
a = variable > min
}
if includeMax {
b = variable <= max
} else {
b = variable < max
}
return a && b
}
func stringSplit ( input string , sep string ) [ ] string {
output := * new ( [ ] string )
for _ , element := range strings . Split ( input , sep ) {
if element != "" {
output = append ( output , element )
}
}
return output
}
// jeden ersten donnerstag und dritten dienstag
func getNextTopic ( ) topic {
date := time . Now ( )
var output topic
for i := 0 ; i < 31 ; i ++ {
newDate := stringSplit ( date . AddDate ( 0 , 0 , 1 * i ) . Format ( time . UnixDate ) , " " )
if newDate [ 0 ] == "Thu" || newDate [ 0 ] == "Tue" {
dayA , errA := strconv . Atoi ( newDate [ 2 ] )
if errA != nil {
2023-01-28 17:44:11 +00:00
panic ( errA . Error ( ) )
2023-01-28 13:23:31 +00:00
}
dayB , errB := strconv . Atoi ( newDate [ 2 ] )
if errB != nil {
2023-01-28 17:44:11 +00:00
panic ( errB . Error ( ) )
2023-01-28 13:23:31 +00:00
}
if ifFloatRange ( float64 ( dayA ) / 7 , 0 , 1 , false , true ) || ( ifFloatRange ( float64 ( dayB ) / 7 , 2 , 3 , false , true ) && newDate [ 0 ] == "Tue" ) {
output . date = date . AddDate ( 0 , 0 , 1 * i ) . Format ( "02.01.2006" )
output . days = i
break
}
}
}
2023-01-26 22:13:44 +00:00
return output
}