Giter Site home page Giter Site logo

ccpaging / gosrv Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jcasts/gosrv

0.0 2.0 0.0 408 KB

Thin wrapper around Go's HTTP server to provide simple command line functionality, config loading, request logging, graceful connection shutdown, and daemonization.

Go 100.00%

gosrv's Introduction

Go Walker githalytics.com alpha

GoSrv

GoSrv is a thin wrapper around Go's HTTP server, to provide basic command-line functionality, env-specific configuration, request logging, graceful shutdowns, and daemonization.

Command Line

$ myserver -h

Usage: myserver [options]

-a: ":9000"  Server address
-pid: "myserver.pid" Server PID File
-c: "myserver.cfg"  Config file
-e: "dev" Environment to run server in
-d: false Run server as daemon
-stop: false  Stop running server and exit
-restart: false Stop running server and boot daemon
-kill: false Force kill running server and exit

Config File

[DEFAULT]
addr=":9000"
pidFile=path/to/file.pid
readTimeout=5s
writeTimeout=500ms
certFile=path/to/myserver.cert
keyFile=path/to/myserver.key

timeFormat=(02/01/2006 15:04:05)
logFormat=$RemoteAddr - $RemoteUser $Time "$Request" $Status $BodyBytes
logFile=path/to/myserver.log

customThing=foobar

[dev]
customThing=baz

[prod]
readTimeout=2s

Go Code

package main

import (
  "fmt"
  "gosrv"
  "net/http"
)


func main() {
  s, err := gosrv.NewFromFlag()
  if err != nil { panic(err) }

  customThing, err := s.Config.String("customThing")
  if err != nil { fmt.Println("Custom thing is "+customThing+" for environment "+s.Config.Env) }

  s.HandleFunc("/", handler)

  err = s.ListenAndServe()
  if err != nil { panic(err) }
}


func handler(w http.ResponseWriter, r *http.Request) {
  w.Write([]byte("Hello World!\n"))

  res := w.(*gosrv.Response)

  for i := 0; i < 100; i++ {
    // Some long running request
    time.Sleep(50 * time.Millisecond)

    // Check to see if the server is trying to shut down
    if res.StopSignal() {
      break
    }
  }
}

gosrv's People

Contributors

jcasts avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.