-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from williamchanrico/first_release
Change go app structure and tag first release
- Loading branch information
Showing
11 changed files
with
230 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package debugapp | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"time" | ||
|
||
"log" | ||
) | ||
|
||
// Response is the structure returned by the echo server. | ||
type Response struct { | ||
Host string `json:"host"` | ||
Method string `json:"method"` | ||
URI string `json:"uri"` | ||
HTTPVersion string `json:"httpVersion"` | ||
Time time.Time `json:"time"` | ||
RemoteAddr string `json:"remoteAddr"` | ||
TLS bool `json:"tls"` | ||
Header map[string][]string `json:"header"` | ||
Msg string `json:"message"` | ||
} | ||
|
||
// RunHTTPServer runs HTTP and HTTPS goroutines and blocks. | ||
func RunHTTPServer(ctx context.Context) { | ||
http.HandleFunc("/", debugapp) | ||
|
||
go func() { | ||
if AppFlag.HTTPSPort == 0 { | ||
return | ||
} | ||
|
||
server := &http.Server{Addr: fmt.Sprintf(":%d", AppFlag.HTTPSPort)} | ||
cert, key := createCert() | ||
err := server.ListenAndServeTLS(cert, key) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
<-ctx.Done() | ||
server.Shutdown(ctx) | ||
}() | ||
|
||
go func() { | ||
if AppFlag.HTTPPort == 0 { | ||
return | ||
} | ||
|
||
server := &http.Server{Addr: fmt.Sprintf(":%d", AppFlag.HTTPPort)} | ||
err := server.ListenAndServe() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
<-ctx.Done() | ||
server.Shutdown(ctx) | ||
}() | ||
|
||
<-ctx.Done() | ||
} | ||
|
||
func debugapp(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set("Content-Type", "application/json") | ||
|
||
resp := Response{ | ||
Host: r.Host, | ||
Method: r.Method, | ||
URI: r.RequestURI, | ||
HTTPVersion: fmt.Sprintf("%d.%d", r.ProtoMajor, r.ProtoMinor), | ||
Time: time.Now(), | ||
RemoteAddr: r.RemoteAddr, | ||
Header: r.Header, | ||
TLS: r.TLS != nil, | ||
Msg: "Hello, you've reached DebugApp!", | ||
} | ||
|
||
respJSON, err := json.MarshalIndent(resp, "", " ") | ||
if err != nil { | ||
log.Printf("failed to marshal the resp: %v", err) | ||
return | ||
} | ||
|
||
w.WriteHeader(http.StatusOK) | ||
w.Write(respJSON) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package debugapp | ||
|
||
import ( | ||
"flag" | ||
"os" | ||
) | ||
|
||
// AppFlag contains variables from command-line flags | ||
var AppFlag struct { | ||
HTTPPort int | ||
HTTPSPort int | ||
} | ||
|
||
// AppEnv contains variables from env | ||
var AppEnv struct { | ||
Dummy string | ||
} | ||
|
||
func init() { | ||
AppEnv.Dummy = mustGetenv("DUMMY_NOT_USED", "DUMMY_DEFAULT_VALUE") | ||
} | ||
|
||
// RegisterFlags creates flags. | ||
func RegisterFlags() { | ||
flag.IntVar(&AppFlag.HTTPPort, "http-port", 8080, "Port for HTTP, 0 will disable this protocol") | ||
flag.IntVar(&AppFlag.HTTPSPort, "https-port", 8443, "Port for HTTPS, 0 will disable this protocol") | ||
} | ||
|
||
// mustGetenv gets env value with default fallback | ||
func mustGetenv(env, defaultValue string) string { | ||
if v := os.Getenv(env); v != "" { | ||
return v | ||
} | ||
return defaultValue | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package debugapp | ||
|
||
import ( | ||
"bytes" | ||
"crypto/rand" | ||
"crypto/rsa" | ||
"crypto/x509" | ||
"crypto/x509/pkix" | ||
"encoding/pem" | ||
"io/ioutil" | ||
"log" | ||
"math/big" | ||
"time" | ||
) | ||
|
||
// createCert creates a certificate and key in temporary files and returns their paths. | ||
func createCert() (certFilePath string, keyFilepath string) { | ||
cert, key, err := generateInsecureCertAndKey("debugapp", time.Now(), 24*time.Hour*1825) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
tmpCert, err := ioutil.TempFile("", "server.crt") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
if err := ioutil.WriteFile(tmpCert.Name(), cert, 0644); err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
tmpKey, err := ioutil.TempFile("", "server.key") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
if err := ioutil.WriteFile(tmpKey.Name(), key, 0644); err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
return tmpCert.Name(), tmpKey.Name() | ||
} | ||
|
||
const rsaBits = 2048 | ||
|
||
// https://golang.org/src/crypto/tls/generate_cert.go | ||
func generateInsecureCertAndKey(organization string, validFrom time.Time, validFor time.Duration) (cert, key []byte, err error) { | ||
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128) | ||
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit) | ||
if err != nil { | ||
log.Fatalf("failed to generate serial number: %s", err) | ||
} | ||
|
||
validUntill := validFrom.Add(validFor) | ||
|
||
priv, err := rsa.GenerateKey(rand.Reader, rsaBits) | ||
if err != nil { | ||
log.Fatalf("failed to generate private key: %s", err) | ||
} | ||
|
||
template := x509.Certificate{ | ||
SerialNumber: serialNumber, | ||
Subject: pkix.Name{ | ||
Organization: []string{organization}, | ||
}, | ||
NotBefore: validFrom, | ||
NotAfter: validUntill, | ||
|
||
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature, | ||
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, | ||
BasicConstraintsValid: true, | ||
} | ||
|
||
derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, &priv.PublicKey, priv) | ||
if err != nil { | ||
log.Fatalf("Failed to create certificate: %s", err) | ||
} | ||
var certBytes bytes.Buffer | ||
pem.Encode(&certBytes, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes}) | ||
|
||
var keyBytes bytes.Buffer | ||
pb := &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(priv)} | ||
pem.Encode(&keyBytes, pb) | ||
|
||
return certBytes.Bytes(), keyBytes.Bytes(), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,3 @@ | ||
# | ||
# If you only want a single pod | ||
# | ||
|
||
--- | ||
apiVersion: v1 | ||
kind: Pod | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,17 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
"net/http" | ||
"log" | ||
|
||
"github.com/williamchanrico/debugapp/debugapp" | ||
) | ||
|
||
func main() { | ||
port := flag.String("port", "80", "Port to listen on") | ||
debugapp.RegisterFlags() | ||
flag.Parse() | ||
|
||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | ||
fmt.Fprintf(w, "Welcome to DebugApp!\n") | ||
}) | ||
|
||
http.ListenAndServe("0.0.0.0:"+*port, nil) | ||
log.Println("starting debugapp server!") | ||
debugapp.RunHTTPServer(context.Background()) | ||
} |