generated from Lerking/golang-repo-template
51 lines
1.0 KiB
Go
51 lines
1.0 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
"os/exec"
|
|
"runtime"
|
|
)
|
|
|
|
func openBrowser(url string) bool {
|
|
var args []string
|
|
switch runtime.GOOS {
|
|
case "darwin":
|
|
args = []string{"open"}
|
|
case "windows":
|
|
args = []string{"cmd", "/c", "start", "chrome", "--new-window"}
|
|
default:
|
|
args = []string{"xdg-open"}
|
|
}
|
|
cmd := exec.Command(args[0], append(args[1:], url)...)
|
|
return cmd.Start() == nil
|
|
}
|
|
|
|
func getRoot(w http.ResponseWriter, r *http.Request) {
|
|
fmt.Printf("got / request\n")
|
|
io.WriteString(w, "Welcome to TimeCard!\n")
|
|
}
|
|
func getHello(w http.ResponseWriter, r *http.Request) {
|
|
fmt.Printf("got /hello request\n")
|
|
io.WriteString(w, "Hello, HTTP!\n")
|
|
}
|
|
|
|
func main() {
|
|
openBrowser("127.0.0.10:3333")
|
|
|
|
mux := http.NewServeMux()
|
|
mux.HandleFunc("/", getRoot)
|
|
mux.HandleFunc("/hello", getHello)
|
|
|
|
err := http.ListenAndServe("127.0.0.10:3333", mux)
|
|
if errors.Is(err, http.ErrServerClosed) {
|
|
fmt.Printf("server closed\n")
|
|
} else if err != nil {
|
|
fmt.Printf("error starting server: %s\n", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|