Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Syed Areeb Hussain
servmgmt-ss22
Commits
680b665c
Commit
680b665c
authored
May 07, 2022
by
Syed Areeb Hussain
Browse files
Upload New File
parent
dfa41f9b
Changes
1
Hide whitespace changes
Inline
Side-by-side
mini server/main.go
0 → 100644
View file @
680b665c
package
main
import
(
"encoding/hex"
"fmt"
"html/template"
"log"
"net"
"net/http"
"os"
)
type
ViewData
struct
{
Hostname
string
IpAddresses
[]
string
BackgroundColor
string
AmountCalled
int
}
var
amountCalled
=
0
func
webHandlerFactory
(
tmpl
*
template
.
Template
)
http
.
HandlerFunc
{
return
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
if
r
.
URL
.
Path
!=
"/"
{
w
.
WriteHeader
(
404
)
return
// 404
}
amountCalled
++
log
.
Println
(
"request received"
)
hostname
,
err
:=
os
.
Hostname
()
addrs
,
err
:=
net
.
InterfaceAddrs
()
ipAddresses
:=
make
([]
string
,
len
(
addrs
))
for
i
,
addr
:=
range
addrs
{
ipAddresses
[
i
]
=
addr
.
String
()
}
if
err
!=
nil
{
log
.
Println
(
err
)
http
.
Error
(
w
,
err
.
Error
(),
500
)
return
}
hexStr
:=
hex
.
EncodeToString
([]
byte
(
hostname
))
// make sure, that the hexStr is not longer than 6 digits
if
len
(
hexStr
)
>
6
{
hexStr
=
hexStr
[
:
6
]
}
err
=
tmpl
.
Execute
(
w
,
ViewData
{
Hostname
:
hostname
,
BackgroundColor
:
fmt
.
Sprintf
(
"#%s"
,
hexStr
),
AmountCalled
:
amountCalled
,
IpAddresses
:
ipAddresses
,
})
if
err
!=
nil
{
http
.
Error
(
w
,
err
.
Error
(),
500
)
log
.
Println
(
err
.
Error
())
}
}
}
func
main
()
{
// parse the template
tmpl
,
err
:=
template
.
ParseFiles
(
"index.gohtml"
)
if
err
!=
nil
{
panic
(
err
)
}
http
.
HandleFunc
(
"/"
,
webHandlerFactory
(
tmpl
))
log
.
Println
(
"started web server"
)
http
.
ListenAndServe
(
":80"
,
nil
)
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment