Skip to content

Commit

Permalink
Split the Server class from the testing main.go file
Browse files Browse the repository at this point in the history
  • Loading branch information
alobbs committed Oct 15, 2015
1 parent e45648e commit 23b812e
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 43 deletions.
50 changes: 50 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package main

import (
"fmt"

log "github.com/Sirupsen/logrus"
"github.com/spf13/viper"
)

func handleConfig() error {
viper.SetConfigName("http2d")
viper.SetConfigType("yaml")

viper.AddConfigPath("./")
viper.AddConfigPath("$HOME")
viper.AddConfigPath("/etc")

if err := viper.ReadInConfig(); err != nil {
panic(fmt.Errorf("Fatal error config file: %s \n", err))
}
return nil
}

func setupLogging() error {
log.Info("Starting")
return nil
}

func main() {
var srv Server
var err error

// _ = "breakpoint"

setupLogging()

if err = handleConfig(); err != nil {
log.Fatal(err)
}

if err = srv.Init(); err != nil {
log.Fatal(err)
}

if err = srv.Run(); err != nil {
log.Fatal(err)
}

srv.Quit()
}
78 changes: 35 additions & 43 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,70 +5,62 @@ import (
"html"
"net/http"

log "github.com/Sirupsen/logrus"
"github.com/spf13/viper"
"golang.org/x/net/http2"
)

func main() {
var srv http.Server
// Server implements the HTTP2d Server class
type Server struct {
srvInternal http.Server
}

log.Info("Starting")
handleConfig()
func testResponse(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
fmt.Fprintf(w, "Hi tester %q\n", html.EscapeString(r.URL.Path))
fmt.Fprintf(w, "Method: %s\n", r.Method)
fmt.Fprintf(w, "Protocol: %s\n", r.Proto)
fmt.Fprintf(w, "Host: %s\n", r.Host)
fmt.Fprintf(w, "RemoteAddr: %s\n", r.RemoteAddr)
fmt.Fprintf(w, "RequestURI: %q\n", r.RequestURI)
fmt.Fprintf(w, "URL: %#v\n", r.URL)
fmt.Fprintf(w, "Body.ContentLength: %d (-1 means unknown)\n", r.ContentLength)
fmt.Fprintf(w, "Close: %v (relevant for HTTP/1 only)\n", r.Close)
fmt.Fprintf(w, "TLS: %#v\n", r.TLS)
fmt.Fprintf(w, "\nHeaders:\n")
r.Header.Write(w)
}

srv.Addr = ":8080"
// Init initializes the server
func (srv *Server) Init() error {
srv.srvInternal.Addr = ":8080"
http2.VerboseLogs = true

http2.ConfigureServer(&srv, nil)
http2.ConfigureServer(&srv.srvInternal, nil)

// Handlers
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hi tester %q\n", html.EscapeString(r.URL.Path))
showRequestInfoHandler(w, r)
})
http.HandleFunc("/", testResponse)
return nil
}

// Listen loop
// Run runs the server (forever)
func (srv *Server) Run() error {
var err error

tlsKey := viper.GetString("server.http2.tls.key")
tlsCrt := viper.GetString("server.http2.tls.cert")

if tlsKey != "" && tlsCrt != "" {
err = srv.ListenAndServeTLS(tlsCrt, tlsKey)
err = srv.srvInternal.ListenAndServeTLS(tlsCrt, tlsKey)
} else {
err = srv.ListenAndServe()
err = srv.srvInternal.ListenAndServe()
}

log.Fatal(err)
return err
}

func handleConfig() {
_ = "breakpoint"

viper.SetConfigName("http2d")
viper.SetConfigType("yaml")

viper.AddConfigPath("./")
viper.AddConfigPath("$HOME")
viper.AddConfigPath("/etc")

if err := viper.ReadInConfig(); err != nil {
panic(fmt.Errorf("Fatal error config file: %s \n", err))
// Quit cleans up before destroying the object
func (srv *Server) Quit() {
if srv == nil {
return
}
}

func showRequestInfoHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
fmt.Fprintf(w, "Method: %s\n", r.Method)
fmt.Fprintf(w, "Protocol: %s\n", r.Proto)
fmt.Fprintf(w, "Host: %s\n", r.Host)
fmt.Fprintf(w, "RemoteAddr: %s\n", r.RemoteAddr)
fmt.Fprintf(w, "RequestURI: %q\n", r.RequestURI)
fmt.Fprintf(w, "URL: %#v\n", r.URL)
fmt.Fprintf(w, "Body.ContentLength: %d (-1 means unknown)\n", r.ContentLength)
fmt.Fprintf(w, "Close: %v (relevant for HTTP/1 only)\n", r.Close)
fmt.Fprintf(w, "TLS: %#v\n", r.TLS)
fmt.Fprintf(w, "\nHeaders:\n")
fmt.Fprintf(w, "hola\n")
r.Header.Write(w)
}

0 comments on commit 23b812e

Please sign in to comment.