构建可在桌面和移动设备的任何地方运行的服务器

我有以下简单的服务器,在我的笔记本电脑上运行(Mac / Windows / Linux):


package main


import (

    "fmt"

    "log"

    "net/http"

)


func handler(w http.ResponseWriter, r *http.Request) {

    fmt.Fprintf(w, "Hi there %s!", r.URL.Path[1:])

}


func main() {

    http.HandleFunc("/", handler)

    log.Println(http.ListenAndServe("localhost:6060", nil))

}

enter image description here


我是否可以使用相同的代码库在移动设备上运行我的应用程序,而无需使用gomobile或其他软件包,以便将我的代码作为通用应用程序?webview


万千封印
浏览 74回答 1
1回答

一只斗牛犬

答案是“是”,但需要对文件本身进行一些轻微的修改。从 中删除所有内容,因为我们会将最终结果构建为共享库,而不是可执行二进制文件。func main() {}在函数中运行服务器。//export从 as 运行服务器,以便它不会阻塞移动应用程序的主线程。anonymous goroutinego func() {}()为了保持服务器戈鲁丁的运行,我们需要使用一个通道来防止戈鲁廷退出。<-c通过添加 来使用,因此主文件将如下所示:cgoimport "C"package mainimport "C"// other imports should be seperated from the special Cgo importimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "log"&nbsp; &nbsp; "net/http")//export serverfunc server() {&nbsp; &nbsp; c := make(chan bool)&nbsp; &nbsp; go func() {&nbsp; &nbsp; &nbsp; &nbsp; log.Println(http.ListenAndServe("localhost:6060", nil))&nbsp; &nbsp; &nbsp; &nbsp; <-c&nbsp; &nbsp; }()&nbsp; &nbsp; http.HandleFunc("/", handler)}func handler(w http.ResponseWriter, r *http.Request) {&nbsp; &nbsp; fmt.Fprintf(w, "Hi there %s!", r.URL.Path[1:])}func main() {}确保安装了安卓系统,并且您知道它的浴缸NDK生成输出名称为 的输出,以生成以供使用:c-sharedlibxxxAndroid&nbsp; &nbsp; CGO_ENABLED=1 \&nbsp; &nbsp; GOOS=android \&nbsp; &nbsp; GOARCH=arm \&nbsp; &nbsp; GOARM=7 \&nbsp; &nbsp; CC=$(NDK_BIN)/armv7a-linux-androideabi21-clang \&nbsp; &nbsp; go build -buildmode=c-shared -o libfoo.so http.go等由于Android具有多个架构,我们需要单独编译每个架构,因此我们可以在创建Android应用程序后,通过从项目模板中进行选择,在输出库名称下方,所有过程都可以自动执行,并且每个文件夹中将生成2个文件,并且:MakefileNative C++libfoolibfoo.solibfoo.henter image description here#Filename: Makefile# To compile run:# make androidIOS_OUT=lib/iosANDROID_OUT=../android_app/app/src/main/jniLibsANDROID_SDK=$(HOME)/Library/Android/sdkNDK_BIN=$(ANDROID_SDK)/ndk/23.0.7599858/toolchains/llvm/prebuilt/darwin-x86_64/binandroid-armv7a:&nbsp; &nbsp; CGO_ENABLED=1 \&nbsp; &nbsp; GOOS=android \&nbsp; &nbsp; GOARCH=arm \&nbsp; &nbsp; GOARM=7 \&nbsp; &nbsp; CC=$(NDK_BIN)/armv7a-linux-androideabi21-clang \&nbsp; &nbsp; go build -buildmode=c-shared -o $(ANDROID_OUT)/armeabi-v7a/libfoo.so ./cmd/libfooandroid-arm64:&nbsp; &nbsp; CGO_ENABLED=1 \&nbsp; &nbsp; GOOS=android \&nbsp; &nbsp; GOARCH=arm64 \&nbsp; &nbsp; CC=$(NDK_BIN)/aarch64-linux-android21-clang \&nbsp; &nbsp; go build -buildmode=c-shared -o $(ANDROID_OUT)/arm64-v8a/libfoo.so ./cmd/libfooandroid-x86:&nbsp; &nbsp; CGO_ENABLED=1 \&nbsp; &nbsp; GOOS=android \&nbsp; &nbsp; GOARCH=386 \&nbsp; &nbsp; CC=$(NDK_BIN)/i686-linux-android21-clang \&nbsp; &nbsp; go build -buildmode=c-shared -o $(ANDROID_OUT)/x86/libfoo.so ./cmd/libfooandroid-x86_64:&nbsp; &nbsp; CGO_ENABLED=1 \&nbsp; &nbsp; GOOS=android \&nbsp; &nbsp; GOARCH=amd64 \&nbsp; &nbsp; CC=$(NDK_BIN)/x86_64-linux-android21-clang \&nbsp; &nbsp; go build -buildmode=c-shared -o $(ANDROID_OUT)/x86_64/libfoo.so ./cmd/libfooandroid: android-armv7a android-arm64 android-x86 android-x86_64转到并执行以下操作:8.1。文件 ,将其设置为:android_app/app/src/main/cppCMakeLists.txtcmake_minimum_required(VERSION 3.10.2)project("android")add_library( # Sets the name of the library.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;native-lib&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# Sets the library as a shared library.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SHARED&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# Provides a relative path to your source file(s).&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;native-lib.cpp )add_library(lib_foo SHARED IMPORTED)set_property(TARGET lib_foo PROPERTY IMPORTED_NO_SONAME 1)set_target_properties(lib_foo PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/../jniLibs/${CMAKE_ANDROID_ARCH_ABI}/libfoo.so)include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../jniLibs/${CMAKE_ANDROID_ARCH_ABI}/)find_library( # Sets the name of the path variable.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log-lib&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Specifies the name of the NDK library that&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # you want CMake to locate.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log )target_link_libraries( # Specifies the target library.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;native-lib&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;lib_foo&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# Links the target library to the log library&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# included in the NDK.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;${log-lib} )8.2. 文件设置为:native-lib.cpp#include <jni.h>#include <string>#include "libfoo.h" // our library headerextern "C" {&nbsp; &nbsp; void&nbsp; &nbsp; Java_tk_android_MainActivity_serverJNI() {&nbsp; &nbsp; &nbsp; &nbsp; // Running the server&nbsp; &nbsp; &nbsp; &nbsp; server();&nbsp; &nbsp; }}将网页视图添加到 中,如下所示:layout/activity_main<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"&nbsp; &nbsp; xmlns:app="http://schemas.android.com/apk/res-auto"&nbsp; &nbsp; xmlns:tools="http://schemas.android.com/tools"&nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; android:layout_height="match_parent"&nbsp; &nbsp; tools:context=".MainActivity">&nbsp; &nbsp; <WebView&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/wv"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; android:isScrollContainer="false"&nbsp; &nbsp; &nbsp; &nbsp; app:layout_constraintBottom_toBottomOf="parent"&nbsp; &nbsp; &nbsp; &nbsp; app:layout_constraintHorizontal_bias="0.0"&nbsp; &nbsp; &nbsp; &nbsp; app:layout_constraintLeft_toLeftOf="parent"&nbsp; &nbsp; &nbsp; &nbsp; app:layout_constraintRight_toRightOf="parent" /></androidx.constraintlayout.widget.ConstraintLayout>更新如下:MainActivitypackage tk.androidimport android.os.Bundleimport android.webkit.WebViewimport android.webkit.WebViewClientimport androidx.appcompat.app.AppCompatActivityclass MainActivity : AppCompatActivity() {&nbsp; &nbsp; override fun onCreate(savedInstanceState: Bundle?) {&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState)&nbsp; &nbsp; &nbsp; &nbsp; setContentView(R.layout.activity_main)&nbsp; &nbsp; &nbsp; &nbsp; var wv = findViewById<WebView>(R.id.web_view)&nbsp; &nbsp; &nbsp; &nbsp; serverJNI()&nbsp; &nbsp; &nbsp; &nbsp; wv.loadUrl("http://127.0.0.1:6060/")&nbsp; &nbsp; &nbsp; &nbsp; wv.webViewClient = object : WebViewClient() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; override fun shouldOverrideUrlLoading(viewx: WebView, urlx: String): Boolean {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; viewx.loadUrl(urlx)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; private external fun serverJNI(): Void&nbsp; &nbsp; companion object {&nbsp; &nbsp; &nbsp; &nbsp; // Used to load the 'native-lib' library on application startup.&nbsp; &nbsp; &nbsp; &nbsp; init {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.loadLibrary("native-lib")&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}更新为:AndroidManifest<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"&nbsp; &nbsp; package="tk.android">&nbsp; &nbsp; <!-- Mandatory:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:usesCleartextTraffic="true"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Optional:&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:hardwareAccelerated="true"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Depending on the action bar required:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:theme="@style/Theme.AppCompat.NoActionBar"&nbsp; &nbsp; -->&nbsp; &nbsp; <application&nbsp; &nbsp; &nbsp; &nbsp; android:hardwareAccelerated="true"&nbsp; &nbsp; &nbsp;// <- Optional&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; android:usesCleartextTraffic="true"&nbsp; &nbsp; &nbsp;// <- A must to be added&nbsp; &nbsp; &nbsp; &nbsp; android:allowBackup="true"&nbsp; &nbsp; &nbsp; &nbsp; android:icon="@mipmap/ic_launcher"&nbsp; &nbsp; &nbsp; &nbsp; android:label="@string/app_name"&nbsp; &nbsp; &nbsp; &nbsp; android:roundIcon="@mipmap/ic_launcher_round"&nbsp; &nbsp; &nbsp; &nbsp; android:supportsRtl="true"&nbsp; &nbsp; &nbsp; &nbsp; android:theme="@style/Theme.AppCompat.NoActionBar">&nbsp; &nbsp;// <- If do not want action bar&nbsp; &nbsp; &nbsp; &nbsp; <activity android:name=".MainActivity"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:configChanges="orientation|screenSize">&nbsp; &nbsp;// <- A must to avoid crashing at rotation&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <intent-filter>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <action android:name="android.intent.action.MAIN" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <category android:name="android.intent.category.LAUNCHER" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </intent-filter>&nbsp; &nbsp; &nbsp; &nbsp; </activity>&nbsp; &nbsp; </application></manifest>enter image description here奖金使用 Go,所有静态文件都可以嵌入到同一库中,包括 、,因此您可以使用 GUI 构建 API 或完整的应用程序embedcssjavascripttemplates
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go