Last active
May 4, 2022 19:19
-
-
Save campoy/6a53fa10c331b0a4518ac83b160f1667 to your computer and use it in GitHub Desktop.
Electron-like html+golang app
This file contains hidden or 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
<html> | |
<head> | |
<title>Hello, from Go</title> | |
</head> | |
<body> | |
<h1>Hello</h1> | |
<p> | |
This is Go code running, {{.}} | |
</p> | |
</body> | |
</html> |
This file contains hidden or 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
// Copyright 2016 Google Inc. All rights reserved. | |
// Licensed under the Apache License, Version 2.0 (the "License"); | |
// you may not use this file except in compliance with the License. | |
// You may obtain a copy of the License at | |
// http://www.apache.org/licenses/LICENSE-2.0 | |
// | |
// Unless required by applicable law or agreed to writing, software distributed | |
// under the License is distributed on a "AS IS" BASIS, WITHOUT WARRANTIES OR | |
// CONDITIONS OF ANY KIND, either express or implied. | |
// | |
// See the License for the specific language governing permissions and | |
// limitations under the License. | |
package main | |
import ( | |
"html/template" | |
"log" | |
"net/http" | |
"os" | |
"os/exec" | |
) | |
const ( | |
listen = "localhost:2016" | |
chrome = "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" | |
) | |
func main() { | |
http.HandleFunc("/", handler) | |
errc := make(chan error) | |
go func() { errc <- http.ListenAndServe(listen, nil) }() | |
if err := exec.Command(chrome, "--app=http://"+listen).Run(); err != nil { | |
log.Fatal(err) | |
} | |
log.Fatal(<-errc) | |
} | |
var tmpl = template.Must(template.ParseFiles("home.tmpl")) | |
func handler(w http.ResponseWriter, r *http.Request) { | |
if err := tmpl.Execute(w, os.Getenv("USER")); err != nil { | |
log.Printf("could not render home: %v", err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment