saving interim
This commit is contained in:
parent
edaa63364d
commit
e2adbd1629
2
Makefile
2
Makefile
@ -13,7 +13,7 @@ GOFLAGS = -ldflags "$(GOLDFLAGS)"
|
|||||||
default: run
|
default: run
|
||||||
|
|
||||||
run: build
|
run: build
|
||||||
./pooteeweet
|
DEBUG=1 ./pooteeweet
|
||||||
|
|
||||||
build: ./pooteeweet
|
build: ./pooteeweet
|
||||||
|
|
||||||
|
25
main.go
25
main.go
@ -1,10 +1,11 @@
|
|||||||
//3456789112345676892123456789312345678941234567895123456789612345678971234567898
|
//3456789112345676892123456789312345678941234567895123456789612345678971234567898
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import "golang.org/x/crypto/ssh/terminal"
|
import "os"
|
||||||
|
import "time"
|
||||||
import "github.com/rs/zerolog"
|
import "github.com/rs/zerolog"
|
||||||
import "github.com/rs/zerolog/log"
|
import "github.com/rs/zerolog/log"
|
||||||
import "flag"
|
import "golang.org/x/crypto/ssh/terminal"
|
||||||
|
|
||||||
var Version string
|
var Version string
|
||||||
var Buildtime string
|
var Buildtime string
|
||||||
@ -13,9 +14,7 @@ var Buildarch string
|
|||||||
var Appname string = "pooteeweet"
|
var Appname string = "pooteeweet"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
debug := flag.Bool("debug", false, "sets log level to debug")
|
initLogging()
|
||||||
flag.Parse()
|
|
||||||
initLogging(*debug)
|
|
||||||
identify()
|
identify()
|
||||||
serve()
|
serve()
|
||||||
}
|
}
|
||||||
@ -30,20 +29,16 @@ func identify() {
|
|||||||
Msg("starting")
|
Msg("starting")
|
||||||
}
|
}
|
||||||
|
|
||||||
func initLogging(debug bool) {
|
func initLogging() {
|
||||||
log.Logger = log.With().Caller().Logger()
|
log.Logger = log.With().Caller().Stack().Logger()
|
||||||
|
|
||||||
if terminal.IsTerminal(int(os.Stdout.Fd())) {
|
if terminal.IsTerminal(int(os.Stdout.Fd())) {
|
||||||
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
|
output := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339}
|
||||||
|
log.Logger = zerolog.New(output).With().Caller().Stack().Logger().With().Timestamp().Logger()
|
||||||
}
|
}
|
||||||
|
|
||||||
zerolog.SetGlobalLevel(zerolog.InfoLevel)
|
zerolog.SetGlobalLevel(zerolog.InfoLevel)
|
||||||
if debug {
|
if os.Getenv("DEBUG") != "" {
|
||||||
zerolog.SetGlobalLevel(zerolog.DebugLevel)
|
zerolog.SetGlobalLevel(zerolog.DebugLevel)
|
||||||
t
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func serve() {
|
|
||||||
server := setupHttpServer()
|
|
||||||
log.Fatal(server.ListenAndServe())
|
|
||||||
}
|
|
||||||
|
79
server.go
79
server.go
@ -1,62 +1,35 @@
|
|||||||
//3456789112345676892123456789312345678941234567895123456789612345678971234567898
|
//3456789112345676892123456789312345678941234567895123456789612345678971234567898
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import "flag"
|
import "fmt"
|
||||||
import "net/http"
|
import "os"
|
||||||
import "time"
|
import "github.com/rs/zerolog/log"
|
||||||
import "github.com/hoisie/web"
|
import "github.com/hoisie/web"
|
||||||
import "golang.org/x/crypto/ssh/terminal"
|
|
||||||
|
|
||||||
func hello(ctx *web.Context, val string) {
|
/* func initDb() {
|
||||||
|
db, err := sql.Open("postgres", os.Getenv("POSTGRES_DB_URL"))
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal().Msg(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
const defaultPort int = 8080
|
||||||
|
|
||||||
|
func serve() {
|
||||||
|
port := os.Getenv("PORT")
|
||||||
|
if port == "" {
|
||||||
|
port = fmt.Sprintf("%d", defaultPort)
|
||||||
|
}
|
||||||
|
// FIXME web.SetLogger(log.Logger)
|
||||||
|
web.Get(`/dweet/for/([A-Za-z0-9\-\_\.]+)`, dweetHandler)
|
||||||
|
web.Run(fmt.Sprintf("0.0.0.0:%s", port))
|
||||||
|
}
|
||||||
|
|
||||||
|
func dweetHandler(ctx *web.Context, val string) string {
|
||||||
|
log.Info().Msg("in hello()")
|
||||||
for k, v := range ctx.Params {
|
for k, v := range ctx.Params {
|
||||||
println(k, v)
|
println(k, v)
|
||||||
}
|
}
|
||||||
|
return ("hello " + val)
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
|
||||||
web.Get("/(.*)", hello)
|
|
||||||
web.Run("0.0.0.0:9999")
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
func LoggerMiddleware(h http.Handler) http.Handler {
|
|
||||||
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
|
||||||
startTime := time.Now().UTC()
|
|
||||||
h.ServeHTTP(writer, request)
|
|
||||||
log.WithFields(logrus.Fields{
|
|
||||||
"method": request.Method,
|
|
||||||
"path": request.URL.Path,
|
|
||||||
"duration_ms": time.Since(startTime).Milliseconds(),
|
|
||||||
}).Info("pageload")
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func setupHttpRouter(staticdir string) http.Handler {
|
|
||||||
r := mux.NewRouter()
|
|
||||||
|
|
||||||
// This will serve files under
|
|
||||||
// http://localhost:8000/static/<filename>
|
|
||||||
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir(staticdir))))
|
|
||||||
|
|
||||||
return r
|
|
||||||
}
|
|
||||||
|
|
||||||
func setupHttpServer() *http.Server {
|
|
||||||
var dir string
|
|
||||||
|
|
||||||
flag.StringVar(&dir, "dir", ".", "the directory to serve files from. Defaults to the current dir")
|
|
||||||
flag.Parse()
|
|
||||||
|
|
||||||
router := setupHttpRouter(dir)
|
|
||||||
|
|
||||||
server := alice.New(LoggerMiddleware).Then(router)
|
|
||||||
|
|
||||||
listener := &http.Server{
|
|
||||||
Handler: server,
|
|
||||||
Addr: ":8000",
|
|
||||||
WriteTimeout: 15 * time.Second,
|
|
||||||
ReadTimeout: 15 * time.Second,
|
|
||||||
}
|
|
||||||
return listener
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
Loading…
Reference in New Issue
Block a user