- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 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
- 59
- 60
- 61
// GostServer is the type that contains all of the relevant information to set
// up the GOST HTTP Server
type GostServer struct {
host string // Hostname for example "localhost" or "192.168.1.14"
port int // Port number where you want to run your http server on
api *models.API // SensorThings api to interact with from the HttpServer
https bool
httpsCert string
httpsKey string
httpServer *http.Server
}
// CreateServer initialises a new GOST HTTPServer based on the given parameters
func CreateServer(host string, port int, api *models.API, https bool, httpsCert, httpsKey string) Server {
setupLogger()
a := *api
router := CreateRouter(api)
return &GostServer{
host: host,
port: port,
api: api,
https: https,
httpsCert: httpsCert,
httpsKey: httpsKey,
httpServer: &http.Server{
Addr: fmt.Sprintf("%s:%s", host, strconv.Itoa(port)),
Handler: PostProcessHandler(RequestErrorHandler(LowerCaseURI(router)), a.GetConfig().Server.ExternalURI),
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
},
}
}
// Start command to start the GOST HTTPServer
func (s *GostServer) Start() {
t := "HTTP"
if s.https {
t = "HTTPS"
}
logger.Infof("Started GOST %v Server on %v:%v", t, s.host, s.port)
var err error
if s.https {
err = s.httpServer.ListenAndServeTLS(s.httpsCert, s.httpsKey)
} else {
err = s.httpServer.ListenAndServe()
}
if err != nil {
logger.Panicf("GOST server not properly stopped: %v", err)
}
}
// Stop command to stop the GOST HTTP server
func (s *GostServer) Stop() {
if s.httpServer != nil {
logger.Info("Stopping HTTP(S) Server")
s.httpServer.Shutdown(context.Background())
}
}